SQL cheatsheet
The SQL you reach for most, from SELECT and WHERE to joins, groups, NULL, subqueries, window functions and transactions, one short query at a time.
Reading a table
SELECT title, year FROM songs;Pick columns from a table, in the order you list them.
SELECT * FROM songs;Every column. Good for a first look.
SELECT title, seconds / 60.0 AS minutes FROM songs;Work out a new column and name it with AS.
2 more: different values, comments
SELECT DISTINCT genre FROM artists;Each different value once.
-- a commentSQL ignores everything after two dashes on a line.
Filtering rows
WHERE country = 'UK' AND age >= 16Text in single quotes. AND needs both, OR needs one.
WHERE title LIKE '%rain%'% is any run of characters, _ is exactly one.
WHERE country IN ('UK', 'IE', 'US')Matches any value in the list.
2 more: brackets with OR, ranges
WHERE (genre = 'pop' OR genre = 'indie') AND year > 2022AND runs before OR, so add brackets when you mix them.
WHERE year BETWEEN 2020 AND 2022Includes both ends.
Sorting and limiting
ORDER BY score DESC, gamertagBiggest first, then alphabetical to break ties.
LIMIT 5Only the first 5 rows. After ORDER BY, a top 5.
1 more: skipping rows
LIMIT 5 OFFSET 10Skip 10 rows, then take 5: page 3 of a list.
NULL
WHERE critic_score IS NULLFinds missing values. = NULL never matches.
COALESCE(score, 0)The first value that is not NULL: a fallback.
2 more: NULL with !=, counting
WHERE note IS NULL OR note != 'gift'!= skips NULL rows too, so ask for them.
COUNT(*), COUNT(score)All rows, and only rows where score is not NULL.
Counting and grouping
SELECT COUNT(*), AVG(price), MAX(price) FROM products;Aggregates squash all rows into one.
SELECT category, COUNT(*) FROM products GROUP BY category;One row per category. Group or aggregate other columns.
GROUP BY customer_id HAVING COUNT(*) >= 3HAVING filters groups. WHERE filters rows, before grouping.
2 more: counting different values, counting matches
COUNT(DISTINCT listener_id)Count different people, not plays.
SUM(CASE WHEN status = 'paid' THEN 1 ELSE 0 END)Count only the rows that match a condition.
Joins
FROM songs AS s
JOIN artists AS a ON a.id = s.artist_idKeeps pairs where the keys match. Aliases save typing.
FROM players AS p
LEFT JOIN sessions AS s ON s.player_id = p.idEvery player, with NULLs where they have no sessions.
2 more: rows with no match, naming columns
LEFT JOIN sessions AS s ON s.player_id = p.id
WHERE s.id IS NULLRows with no partner: players who never played.
SELECT c.name, p.namePrefix a column with its table when both have that name.
Text and dates
first_name || ' ' || last_nameGlue text together.
UPPER(name), LOWER(name), LENGTH(name)Change case, count characters.
date('now'), date(joined_on, '+30 days')Today's date, and date maths.
2 more: part of a string, months and hours
substr(title, 1, 10)Part of a string: 10 characters from position 1.
strftime('%Y-%m', ordered_on), strftime('%H', sold_at)Pull out the month or hour. 'YYYY-MM-DD' text sorts right.
Locked
Subqueries and CTEs
Code 4 rows
Window functions
Code 5 rows
Changing data and transactions
Code 5 rows