SQL
CHEAT SHEETSQL CHEAT SHEET
BASE QUERY
SELECT * FROM table_name;
This query returns every column and every row of the table called table_name.
‘SELECT * FROM table_name LIMIT 10;
It returns every column and the first 10 rows from table_name.
SELECTING SPECIFIC COLUMNS
SELECT columni, column2, column3 FROM table_name;
This query returns every row of column1, column? and column3 from table_name.
DATA TYPES IN SQL
In SQL we have more than 40 different data types. But these seven are the most
important ones:
. Integer. A whole number without a fractional part. E.g. 1, 156, 2012412
. Decimal. A number with a fractional part. E.g. 3.14, 3.141592654, 961.1241250
. Boolean. A binary value. It can be either TRUE or FALSE.
Date. Speaks for itself. You can also choose the format. E.g. 2017-12-31
1e. You can decide the format of this, as well. E.g. 23:59:59
Timestamp. The date and the time together. E.g. 2017-12-31 23:59:59
Text. This is the most general data type. But it can be alphabetical letters only,
or a mix of letters and numbers and any other characters. E.g. hello, R2D2,
Tomi, 124.56.128.41
NOWPWNPE
CREATED BY TOMI MESTER | DATA36.COM 1‘SQL CHEAT SHEET
FILTERING (the WHERE CLAUSE)
SELECT * FROM table_name WHERE column1 = ‘expression’;
“Horizontal filtering." This query returns every column from table_name - but only
those rows where the value in column] is ‘expression’. Obviously this can be
something other than text: a number (integer or decimal), date or any other data
format, too.
ADVANCED FILTERING
Comparison operators help you compare two values. (Usually a value that you
define in your query and valves that exist in your SQL table.) Mostly, they are
mathematical symbols, with a few exceptions:
Toasts
= Equal to
<-> Not equal to
Ie Not equal to
< Less than
<= Less than or equal to
- Greater than
>= Greater than or equal to
LIKE "%expression%" Contains ‘expression’
IN Cexp1;, ‘exp2,'exp3')__ Contains any of 'exp1' 'exp2' or ‘exp3"
CREATED BY TOMI MESTER | DATA36.COM 2SQL CHEAT SHEET
A few examples:
SELECT * FROM table_name WHERE column1 != ‘expression’;
This query returns every column from table_name, but only those rows where the
value in column is NOT ‘expression’.
SELECT * FROM table_name WHERE column2 >=
It returns every column from table_name, but only those rows where the value in
column2 is greater or equal to 10.
SELECT * FROM table_name WHERE column3 LIKE '%xzy%';
It returns every column from table_name, but only those rows where the value in
column3 contains the 'xyz' string.
JULTIPLE CONDITIONS
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%'s
This query returns every column from table_name, but only those rows where the
value in column1 is NOT ‘expre:
string.
sion’ AND the value in column3 contains the 'xyz"
SELECT * FROM table_name WHERE column1 != ‘expression’ OR column3 LIKE
"pxzy%'s
This query returns every column from table_name, but only those rows where the
value in column’ is NOT ‘expression’ OR the value in column3 contains the 'xyz"
string.
CREATED BY TOMI MESTER | DATA36.COM 3SQL CHEAT SHEET
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 ES
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 column], in
ending order.
UNIQUE VALUES
SELECT DISTINCT(column1) FROM table_name;
Itreturns every unique value from column from table_name.
CREATED BY TOMI MESTER | DATA36.COM 4SQL CHEAT SHEET
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)
+ AVGO
+ MINO
» MAXQ.
A few examples:
SELECT COUNT(*) FROM table_name WHERE column
It mber of in the SQL table in which the value in column.
something
SELECT AVG(column1) FROM table_name WHERE column2 > 1000;
It ean) of th 1, only Jing rows i
which the mi ter than 1000
CREATED BY TOMI MESTER | DATA36.COM 5SQL CHEAT SHEET
SQL GROUP BY
The GROUP BY clause is usually used with an aggregate function (COUNT, SUM,
AVG, MIN, MAX). It groups the rows by a given column value (specified after
GROUP BY) then calculates the aggregate for each group and returns that to the
screen,
SELECT columni, COUNT(column2) FROM table_name GROUP BY column1;
This query count
column1 values.
s the number of values in colur
SELECT column1, SUM(column2) FROM table_name GROUP BY column;
This query sums the number of valves in column? - for each > of <
umn values
SELECT columni, MIN(column2) FROM table_name GROUP BY column;
This query finds the minimum valve in column2 - for each group of uni
SELECT columni, MAX(column2) FROM table_name GROUP BY column;
This query finds the maximum valve in c
1 group of u!
CREATED BY TOMI MESTER | DATA36.COM 6SQL CHEAT SHEET
SQL ALIASES
You can rename columns, tables, subqueries, anything
SELECT columni, COUNT(column2) AS number_of_values FROM table_name
GROUP BY column:
This query counts the number of values in column? - for each group of unique
t INT(colurr 1 to
column1 values. Then it renames
number_of_values.
SQL JOIN
You can JOIN two (or more) SQL tables based on column values.
SELECT*
FROM table1
JOIN table2
ON tablei.column1 = table2.column:
This joins table and t
nlues - for every row where t f col
equals the
Detailed explanation here: https:/data36.com/sal-join-data-analysis-tutorial-ep5/
CREATED BY TOMI MESTER | DATA36.COM 7SGL CHEAT SHEET
SQL HAVING
The execution order of the different SGL keywords doesn't allow you to filter with
the WHERE clause on the result of an aggregate function (COUNT, SUM, etc.). This
is because WHERE is executed before the aggregate functions. But that's what
HAVING is for:
SELECT columni, COUNT(column2)
FROM table_name
GROUP BY column1
HAVING COUNT(column2) > 100;
This query counts the number of values in column2
urns only those ts where the counted valu!
than 100.
Detailed explanation and examples here: https://data34 com/sql-data-analysis-
a ri
CORRECT KEYWORD ORDER AGAIN
SQL is extremely sensitive to keyword order.
So make sure you keep it right:
1. SELECT
2. FROM
3. JOIN (ON)
4. WHERE
5. GROUP BY
6. HAVING
7. ORDER BY
8. LIMIT
CREATED BY TOMI MESTER | DATA36.COM 8SQL CHEAT SHEET
SUBQUERIES
You can run SQL queries within SQL queries. (Called subqueries.) Even queries
within queries within queries. The point is to use the result of one query as an input
value of another query.
Example:
SELECT COUNT(*) FROM
(SELECT column1, COUNT(column2) AS inner_number_of_values
FROM table_name
GROUP BY columni) AS inner_query
WHERE inner_number_of_values > 100;
The inner query counts the number of values in column? - for each group of unique
column va
es. Then the outer query uses the inner query's results and counts the
number of values where inner_number_of_values are greater than 100. (The result
is one number)
Detailed explanation here: https://data36 com/sql-data-analysis-advanced=
tutorial-ep6/
CREATED BY TOMI MESTER | DATA36.COM 9