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;| Function | Purpose |
|---|---|
COUNT(*) | Number of rows |
COUNT(col) | Rows where col is not NULL |
SUM / AVG | Total and average |
MIN / MAX | Smallest and largest |
DISTINCT | Remove duplicates |
COALESCE(a, b) | First value that is not NULL |
UPPER / LOWER | Change case |
CASE WHEN ... END | If/else inside a query |