MULTIPLE CONDITIONS IN SQL DATABASES

MULTIPLE CONDITIONS IN SQL DATABASES

You can use more than one condition to filter. For that, we have two logical operators: OR, AND.
SELECT * FROM table_name WHERE column1 != ‘expression’ AND column3 LIKE ‘%xzy%’;
This query returns every column from table_name, but only those rows where the value in column1 is NOT ‘expression’ AND the value in column3 contains the ‘xyz’ string.
SELECT * FROM table_name WHERE column1 != ‘expression’ OR column3 LIKE ‘%xzy%’;
This query returns every column from table_name, but only those rows where the value in column1 is NOT ‘expression’ OR the value in column3 contains the ‘xyz’ string.

PROPER FORMATTING
You can use line breaks and indentations for nicer formatting. It won’t have any effect on your output. Be careful and put a semicolon at the end of the query though!
SELECT *
FROM table_name
WHERE column1 != ‘expression’
AND column3 LIKE ‘%xzy%’
LIMIT 10;

SORTING VALUES
SELECT * FROM table_name ORDER BY column1;
This query returns every row and column from table_name, ordered by column1, in ascending order (by default).
SELECT * FROM table_name ORDER BY column1 DESC;
This query returns every row and column from table_name, ordered by column1, in descending order.
UNIQUE VALUES
SELECT DISTINCT(column1) FROM table_name;
It returns every unique value from column1 from table_name.

CORRECT KEYWORD ORDER
SQL is extremely sensitive to keyword order.
So make sure you keep it right:
1. SELECT
2. FROM
3. WHERE
4. ORDER BY
5. LIMIT
SQL FUNCTIONS FOR AGGREGATION
In SQL, there are five important aggregate functions for data analysts/scientists:
• COUNT()
• SUM()
• AVG()
• MIN()
• MAX()
A few examples:
SELECT COUNT(*) FROM table_name WHERE column1 = ‘something’;
It counts the number of rows in the SQL table in which the value in column1 is ‘something’.
SELECT AVG(column1) FROM table_name WHERE column2 > 1000;
It calculates the average (mean) of the values in column1, only including rows in which the value in column2 is greater than 1000.
CREATED

(Visited 116 times, 1 visits today)
Share this:

Written by