Cheat sheets
SQL cheat sheet
Quick reference for the most common SQL clauses and patterns: queries, joins, aggregations, and window functions.
Basic queries
SELECT col1, col2 FROM table- Selects specific columns.
SELECT * FROM table WHERE condition- Filters rows before grouping.
SELECT DISTINCT col FROM table- Unique values of a column.
ORDER BY col DESC- Sorts the result (DESC = descending).
LIMIT 10- Limits the number of rows returned.
SELECT col AS alias FROM table- Renames a column in the result.
Joins
INNER JOIN other ON a.id = other.id- Only rows matching in both tables.
LEFT JOIN other ON a.id = other.id