Skip to content

SQL cheat sheet

The first half is a method to keep in your head: the order in which SQL runs, seven questions that turn a task text into a query, and the checks before you say “done”. The second half is syntax by topic (dates, aggregation, joins, windows, NULL) to look up when you need it. Find each syntax item once in the PostgreSQL documentation, so that in the interview you know where it lives.

FROM → JOIN → WHERE → GROUP BY → HAVING → SELECT (window functions are computed here) → ORDER BY → LIMIT

You want to…What happensDo this instead
Filter by a SELECT alias in WHEREcolumn "…" does not exist: WHERE runs firstRepeat the expression, or compute it in a CTE and filter outside
Use an alias in HAVINGThe same error in PostgreSQLWrite the aggregate: HAVING COUNT(*) > 1000
Filter by a window functionwindow functions are not allowed in WHERECompute it in a CTE, then WHERE rn = 1 outside
Sort by an aliasWorks: ORDER BY runs after SELECTORDER BY revenue DESC, m.id
Show a column that is not groupedmust appear in the GROUP BY clauseAdd it to GROUP BY or wrap it in an aggregate

GROUP BY 1, 2 and GROUP BY an output alias both work in PostgreSQL.

Ask them in this order. Most wrong queries skip the first one.

  1. What is one row of the result? A row of a table: a plain SELECT … WHERE. A group (a merchant, a month): GROUP BY that group.
  2. Is everything in one table? If not, JOIN by the key: a transaction → products → merchants.
  3. Do I need every X, even without data? Then FROM x LEFT JOIN … and COALESCE(…, 0).
  4. Do I need a CTE, and which one? If a property comes from the rows of a group and is then counted one level up (“how many advances are still open”), build a chain: GROUP BY the advance, then GROUP BY the merchant from that CTE. If the data comes from two different tables, reduce each one to one row per key, then join.
  5. Do I need a row plus something about its group or its neighbour? That is a window. GROUP BY collapses rows into one; a window keeps every row and adds a column.
  6. How should it look? ORDER BY with an id for ties, ROUND at the very end.
  7. How do I check it? The number of rows, one row by hand, and every agreed answer found in the code.
Words in the taskTool
per X, by XGROUP BY x
for every X, including zeroFROM x LEFT JOIN … + COALESCE
never, without, no …NOT EXISTS or LEFT JOIN … WHERE b.id IS NULL
how many of them are … (several counters)COUNT(*) FILTER (WHERE …)
share of totalv / SUM(v) OVER ()
share within Xv / SUM(v) OVER (PARTITION BY x)
top N per X, latest, first, dedupeROW_NUMBER() OVER (PARTITION BY x ORDER BY … DESC, id), then WHERE rn = 1
keep tiesRANK()
previous, change vs last monthLAG(v) OVER (PARTITION BY x ORDER BY t)
running total, cumulativeSUM(v) OVER (PARTITION BY x ORDER BY t)
every day or month, even emptygenerate_series + LEFT JOIN
label, status, bucketCASE WHEN … THEN … ELSE … END
sales and refunds as one streamUNION ALL (not UNION)
compare two sources, missing on either sideFULL OUTER JOIN + COALESCE of the keys
  • Many rows per key on both sides of a JOIN multiply the rows. Reduce each side in its own CTE first.
  • A filter on the right table of a LEFT JOIN belongs in ON or in a CTE. In WHERE it turns the LEFT JOIN into an inner join.
  • GROUP BY or PARTITION BY with the same key as the previous step does nothing: every group has one row.
  • Dates are half-open: >= '2026-08-01' AND < '2026-09-01'. “Today” is a fixed date, not NOW().
  • A flow (revenue for a period) has a date filter. A balance (what a merchant owes, an account balance) is computed over all time.
  • SUM of no rows is NULL: wrap it in COALESCE. An integer divided by an integer is an integer (7 / 2 = 3); use NULLIF(b, 0) for the divisor.
  • DESC applies to one column: ORDER BY paid_at DESC, payout_id DESC.

August revenue per merchant, every merchant included, with the share of the total. Write it in an empty editor without looking, run it, then compare.

WITH sales AS ( -- 1. filter, then reduce to the key
SELECT p.merchant_id, SUM(t.revenue_usd) AS revenue
FROM transactions t
JOIN products p ON p.id = t.product_id
WHERE NOT t.is_test
AND t.start_time >= TIMESTAMP '2026-08-01'
AND t.start_time < TIMESTAMP '2026-09-01'
GROUP BY p.merchant_id
)
SELECT m.id AS merchant_id,
COALESCE(s.revenue, 0) AS revenue, -- 2. every X
ROUND(100.0 * s.revenue / SUM(s.revenue) OVER (), 1) AS share_pct -- 3. window
FROM merchants m
LEFT JOIN sales s ON s.merchant_id = m.id
ORDER BY revenue DESC, m.id;

12 rows; the first three:

merchant_idrevenueshare_pct
mer_061299.5218.3
mer_12902.3612.7
mer_04885.5312.5

Why OVER () and not PARTITION BY. A window works on the rows that are left after JOIN, WHERE and GROUP BY. Here each merchant has one row, so OVER (PARTITION BY m.id) would give every merchant 100%. “Share of the total” means one group for all rows, and that is OVER (). PARTITION BY is for several rows per group: one row per product, and the share within its merchant.

SUM(revenue) OVER (PARTITION BY merchant_id) -- the share of each product inside its merchant

For mer_02 in August, with one row per product:

product_idrevenueshare_in_merchant_pct
prd_16362.6845.2
prd_02261.8532.6
prd_13177.8322.2
TIMESTAMP '2026-09-01' -- a typed literal
ts::date or CAST(ts AS DATE) -- the date of a timestamp
date_trunc('month', ts) -- start of the month, still a timestamp
ts + INTERVAL '30 days' -- a shift; also '1 month', '1 week'
date_a - date_b -- whole days (DATE - DATE is an integer)
EXTRACT(DOW FROM ts) -- day of the week, 0 = Sunday
EXTRACT(EPOCH FROM ts2 - ts1) -- seconds between two timestamps
generate_series(TIMESTAMP '2026-09-01', TIMESTAMP '2026-09-30', INTERVAL '1 day') AS g(d)
  • Type the literal. A quoted string has no type at first and takes the type of its neighbour. Next to a column it works: start_time >= '2026-08-01'. Next to an interval it does not: '2026-09-30' - INTERVAL '30 days' fails with invalid input syntax for type interval, while TIMESTAMP '2026-09-30' - INTERVAL '30 days' gives 2026-08-31 00:00:00. With plain strings, generate_series returns timestamp with time zone; with TIMESTAMP literals it returns a plain timestamp, like the columns.
  • Half-open interval: ts >= '2026-09-01' AND ts < '2026-10-01'. BETWEEN '2026-09-01' AND '2026-09-30' loses all of 30 September after 00:00.
  • A month as a column: CAST(date_trunc('month', ts) AS DATE). date_trunc always returns a timestamp, and EXTRACT(MONTH …) merges the years.
  • Active at moment m: start_time <= m AND end_time > m.
  • Two periods [a, b) and [c, d) overlap when a < d AND c < b.
  • Days without data: a calendar from generate_series, then LEFT JOIN.
  • Time zones. Ask which time zone defines “a day”. A TIMESTAMP column that stores UTC needs two steps; a timestamptz column needs one. One step on a TIMESTAMP shifts the wrong way:
SELECT TIMESTAMP '2026-09-01 02:00' AT TIME ZONE 'UTC' AT TIME ZONE 'America/New_York' AS two_steps,
TIMESTAMP '2026-09-01 02:00' AT TIME ZONE 'America/New_York' AS one_step;
two_stepsone_step
2026-08-31 22:00:002026-09-01 06:00:00+00
COUNT(*) -- all rows
COUNT(col) -- rows where col IS NOT NULL (after LEFT JOIN: matches only)
COUNT(DISTINCT customer_id) -- different values
COUNT(*) FILTER (WHERE is_trial) -- a conditional count (PostgreSQL)
SUM(CASE WHEN … THEN amount ELSE 0 END) -- the same, portable
COALESCE(SUM(x), 0) -- zero instead of NULL
  • Integer division: in PostgreSQL 3 / 4 = 0. Write 1.0 * a / b or a::numeric / b, and NULLIF(b, 0) against division by zero.
  • AVG and ELSE 0: AVG(CASE WHEN … THEN x END) skips the other rows, because NULL does not count. ELSE 0 adds zeros and lowers the average. For a rate, ELSE 0 is exactly what you want: AVG(CASE WHEN … THEN 1.0 ELSE 0 END).
  • ROUND(x, 2) exists for numeric only. For double precision, cast first: ROUND(x::numeric, 2).
  • WHERE filters rows before grouping, HAVING filters groups after it.
  • Duplicates: COUNT(DISTINCT id) only removes repeats inside its own group. If a copy can fall into another group (another type, another day), number the copies first with ROW_NUMBER() OVER (PARTITION BY id ORDER BY …), keep rn = 1, then group.
  • Sorting: always add a second key for ties, ORDER BY net_usd DESC, product_id. Without it, the order and the LIMIT are random.
  • LEFT JOIN t ON … AND t.flag: a condition on the right table goes in ON. In WHERE, rows with NULL on the right are dropped and the join becomes inner.
  • Row multiplication: a one-to-many join followed by SUM multiplies the sum. Aggregate in a CTE first, then join.
  • A plain JOIN or a CTE? A plain join when you attach one row from the right to each row (a transaction → its product → its merchant). A CTE when you must aggregate first (or the rows multiply), when the next step filters by an aggregate or a window, or when there are two or more steps and names make them easier to read.
  • Reconciliation of two sources: FULL OUTER JOIN and COALESCE(a.key, b.key).
  • Anti-join: LEFT JOIN … WHERE b.id IS NULL or NOT EXISTS (…). NOT IN with a NULL in the subquery returns nothing.
  • Two sets of rows with the same columns, one under the other: sales with + and refunds with − in one stream, or events from two tables.
  • UNION ALL, not UNION. UNION removes equal rows: two payments of 9.99 on the same day become one, and the total drops.
  • Columns match by position. Their number and types must agree; the names come from the first SELECT.

The first two lines are what most tasks need. The rest is for reference.

100.0 * x / SUM(x) OVER () -- share of the total
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY start_time DESC) -- latest row: WHERE rn = 1 outside
DISTINCT ON (customer_id) … ORDER BY customer_id, start_time DESC -- the same in PostgreSQL
LAG(end_time) OVER (PARTITION BY customer_id ORDER BY start_time) -- the previous row
SUM(amount) OVER (PARTITION BY merchant_id ORDER BY day) -- running total
AVG(x) OVER (ORDER BY day ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) -- moving average over 7 rows
RANK() / DENSE_RANK() -- equal values share a place
  • You cannot filter by a window function in the same SELECT. Wrap the query in a CTE, then WHERE rn = 1.
  • The default frame. With ORDER BY in the window, the frame is RANGE … CURRENT ROW: rows with equal ORDER BY values are added together. For an exact number of rows, use ROWS:
dayamountdefault frameROWS frame
2026-09-01101010
2026-09-0252215
2026-09-0272222
2026-09-0312323
  • A moving “last 7 days” with ROWS is right only if every day is present as one row. Build the calendar first.
  • NULL = NULL is NULL, not true. Test with IS NULL, or IS DISTINCT FROM for “not equal, counting NULL”.
  • NOT (x = 1 AND …) is NULL when x is NULL, and the row disappears from WHERE. Protect it with COALESCE(x = 1, false) or x IS DISTINCT FROM 1. Another way: compute a status in a subquery, then filter WHERE status <> 'ok'.
  • SUM over an empty set is NULL, not 0. So is SUM(x) FILTER (WHERE …) for a group without matches; COUNT returns 0.
  • NOT IN with a NULL in the list returns no rows. Use NOT EXISTS.
  • Duplicate webhooks: UNIQUE (event_id) plus INSERT … ON CONFLICT (event_id) DO NOTHING.
  • A job queue with several workers: SELECT … FOR UPDATE SKIP LOCKED.
  • Money: NUMERIC(12,2), never float.

Site content: CC BY 4.0. Code: MIT. The exercises run in your browser: PostgreSQL by PGlite, Python by Pyodide.