SQL quick reference

Clause order and common functions on one page.

Clauses are always written in this order, and each one is optional except SELECT.

SQL
SELECT columns
FROM table
JOIN other ON condition
WHERE row_filter
GROUP BY columns
HAVING group_filter
ORDER BY columns
LIMIT n;
FunctionPurpose
COUNT(*)Number of rows
COUNT(col)Rows where col is not NULL
SUM / AVGTotal and average
MIN / MAXSmallest and largest
DISTINCTRemove duplicates
COALESCE(a, b)First value that is not NULL
UPPER / LOWERChange case
CASE WHEN ... ENDIf/else inside a query
โ† Tables, keys and normalisationSQL learning path โ†’