SQL Cheat Sheet ?
SQL Cheat Sheet ?
CHEAT SHEET
created by Tomi Mester
SQL CHEAT SHEET
BASE QUERY
SELECT * FROM table_name;
This query returns every column and every row of the table called table_name.
[your notes]
important ones:
1.Integer. A whole number without a fractional part. E.g. 1, 156, 2012412
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 values that exist in your SQL table.) Mostly, they are
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 column1 is NOT 'expression'.
MULTIPLE CONDITIONS
You can use more than one condition to filter. For that, we have two
logical operators: OR, AND.
‘%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).
UNIQUE VALUES
SELECT DISTINCT(column1) FROM table_name;
It returns every unique value from column1 from table_name.
1.SELECT
2.FROM
3.WHERE
4.ORDER BY
5.LIMIT
•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'.
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.
This query sums the number of values in column2 - for each group of unique
column1 values.
SQL ALIASES
You can rename columns, tables, subqueries, anything.
SQL JOIN
You can JOIN two (or more) SQL tables based on column values.
SELECT *
FROM table1
JOIN table2
ON table1.column1 = table2.column1;
This joins table1 and table2 values - for every row where the value of column1
from table1 equals the value of column1 from table2.
SQL HAVING
The execution order of the different SQL 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:
This query counts the number of values in column2 - for each group of unique
column1 values. It returns only those results where the counted value is greater
than 100.
1.SELECT
2.FROM
3.JOIN (ON)
4.WHERE
5.GROUP BY
6.HAVING
7.ORDER BY
8.LIMIT
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:
The inner query counts the number of values in column2 - for each group of
unique column1 values. 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.)