Queries

SELECT statement


-- Displaying selected columns from a given table
SELECT * 
FROM table_name;
                                    

We can use arithmetic operators in the SELECT statement to perform calculations, such as SELECT price, quantity, price * quantity FROM products;.

AS clause


-- Giving an alias to a column in the result set (useful for readability and in ORDER BY or GROUP BY clauses)
SELECT column_name AS 'c_name'
FROM table_name;
                                    

DISTINCT clause


-- Displaying every value from a given column only once
SELECT DISTINCT column_name 
FROM table_name;
                                    

WHERE clause


-- Displaying values from records in which a specified condition is met
SELECT column_name 
FROM table_name 
WHERE column_name = 2;
                                    

LIKE operator

A WHERE statement criterion used to search for a specific pattern.


SELECT column_name 
FROM table_name 
WHERE column_name LIKE 'ABC';
                                    

_ wildcard

One unspecified character in the “pattern” that the LIKE statement is looking for.


SELECT column_name 
FROM table_name 
WHERE column_name LIKE '_QL';
                                    

% wildcard

An unspecified number of characters of any type in the “pattern” that the LIKE statement is looking for.


SELECT column_name 
FROM table_name 
WHERE column_name LIKE 'S%';
                                    

NULL values

Checking if a field contains a value (NULL means no value).


SELECT column_name 
FROM table_name 
WHERE column_name IS NULL; 
                                    

SELECT column_name 
FROM table_name 
WHERE column_name IS NOT NULL;   
                                    

AND operator

An operator used to combine conditions in a WHERE statement. If all of them are true, the record is displayed.


SELECT column_name 
FROM table_name 
WHERE column1_name = 1 AND column2_name = 2; 
                                    

OR operator

An operator used to combine conditions in a WHERE statement. If any of them is true, the record is displayed.


SELECT column_name 
FROM table_name 
WHERE column1_name = 1 OR column2_name = 2; 
                                    

BETWEEN operator


-- Filtering values ​​according to the numerical range specified in the WHERE statement (we can also use ranges with letters)
SELECT column_name 
FROM table_name 
WHERE column_name BETWEEN 2 AND 7; 
                                    

ORDER BY clause

Sorting a table descending (DESC) or ascending (ASC) by the values of a specified column. Ascending sorting also means alphabetically. ASC is the default option.


SELECT column_name 
FROM table_name 
ORDER BY column_name DESC;   
                                    

-- Sorting by multiple columns
SELECT column_name 
FROM table_name 
ORDER BY column_name DESC, column2_name ASC, column3_name;   
                                    

CASE statement

The CASE statement is a conditional expression that allows us to evaluate a set of conditions and return specific values based on which condition is met.


SELECT column1_name, column2_name,
CASE
    WHEN column1_name > 10 THEN 'It is 10.'
    WHEN column1_name = 20 THEN 'It is 20.'
    ELSE 'It is 30.'
END AS case_example
FROM table_name;
                                    

IF statement

The IF statement is similar to the CASE statement but is used for executing specific SQL statements based on a condition. In contrast, the CASE statement evaluates multiple conditions within a single expression.


IF column1_name > 10
    THEN
        SELECT 'Value is greater than 10';
    ELSE
        SELECT 'Value is 10 or less';
END IF;