Vibe Engines
YouTube

Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path

SQL Quick Reference

Joins, aggregation, window functions and the query-shape patterns interviews and dashboards both live on.

SQL is declarative: you describe the rows you want and the planner decides how to fetch them. Two ideas make the rows below click. A query is evaluated in a fixed logical order (FROM and JOIN, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, LIMIT) whatever order you write it in. And NULL means unknown, so any comparison with it is unknown too, which is where most surprising results come from.

Syntax is standard SQL with PostgreSQL where dialects differ. MySQL, SQLite and SQL Server mostly agree, and the differences are marked.

logical order of evaluation, not the order you typeFROMWHEREGROUP BYHAVINGSELECTORDER BYLIMIT+ JOIN+ windows10,0006,200409993rowsrowsgroupsgroupsrowsrowsrowsa SELECT alias does not exist hereWHERE total > 100 failsaliases work from here onORDER BY total is fine
The clauses run in this order whatever order you write them in, and each step sees only what the step before produced. That is why WHERE cannot use a SELECT alias or a window result, while ORDER BY can.

Query shape

Order of evaluation
FROM/JOIN → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT — why a SELECT alias fails in WHERE
SELECT DISTINCT
dedupe whole result rows; Postgres DISTINCT ON (k) keeps the first row per key by ORDER BY
CASE WHEN … END
if/else inside an expression; the first true branch wins, and no ELSE means NULL
NULLS LAST
Postgres puts NULLs first under DESC; ORDER BY x DESC NULLS LAST moves them
LIMIT 10 OFFSET 20
the third page of 10; without ORDER BY the rows returned are arbitrary
IN (…) · BETWEEN
membership · a range that includes both ends: BETWEEN 1 AND 10
LIKE · ILIKE
% any run, _ one char; ILIKE ignores case (Postgres); a leading % defeats a B-tree index

Joins

INNER JOIN
only rows with a match on both sides
LEFT JOIN
all left rows, NULLs where the right is missing; a WHERE on right columns undoes it — filter in ON
Anti-join
LEFT JOIN … WHERE right.id IS NULL — "customers with no orders"
Self join
table joined to itself (employees → managers)
FULL OUTER JOIN
every row from both sides, NULLs where either is missing — reconciling two sources
CROSS JOIN
every pairing of both tables (rows × rows) — calendars and combinations; easy to blow up
JOIN … USING (id)
shorthand when both columns share a name; the output keeps a single id
JOIN LATERAL
a subquery that sees the outer row: JOIN LATERAL (… LIMIT 3) ON true = top-N per row
Join fan-out
joining two one-to-many tables multiplies rows; aggregate each side before joining

Aggregation

GROUP BY + HAVING
HAVING filters groups; WHERE filters rows (before grouping)
COUNT(DISTINCT x)
unique values; COUNT(*) counts rows, NULLs included
COUNT(col)
non-NULL values only, so it differs from COUNT(*) when the column has NULLs
Conditional agg
SUM(CASE WHEN status=1 THEN 1 ELSE 0 END) — pivot in one pass
FILTER (WHERE …)
cleaner conditional aggregate: COUNT(*) FILTER (WHERE paid) (Postgres, SQLite)
STRING_AGG(x, ', ')
join a group’s values into one string; MySQL calls it GROUP_CONCAT
GROUP BY ROLLUP (a)
per-group rows plus a grand total in one query; NULL in a marks the total row
NULL traps
NULL ≠ NULL; use IS NULL; aggregates skip NULLs; COALESCE defaults
5 / 2 = 2
integer division truncates (Postgres, SQLite, SQL Server); cast first: 5::numeric / 2

Window functions

ROW_NUMBER()
OVER (PARTITION BY grp ORDER BY x DESC) — top-N per group
RANK vs DENSE_RANK
RANK skips after ties (1,1,3); DENSE_RANK doesn’t (1,1,2)
LAG / LEAD
previous / next row — deltas, retention, sessionization
Running total
SUM(x) OVER (ORDER BY d); tied d values share one total unless you add ROWS UNBOUNDED PRECEDING
Moving average
AVG(x) OVER (ORDER BY d ROWS 6 PRECEDING) — 7-day window
NTILE(4)
split ordered rows into 4 near-equal buckets — quartiles
FIRST_VALUE · LAST_VALUE
LAST_VALUE returns the current row unless the frame runs to UNBOUNDED FOLLOWING
SUM(x) OVER ()
grand total on every row — share of total = x / SUM(x) OVER ()
Filter on a window
window results can’t appear in WHERE; compute them in a CTE and filter outside

CTEs & subqueries

WITH x AS (…)
name a subquery and read top-down; Postgres 12+ inlines it when it is used once, unless marked MATERIALIZED
WITH RECURSIVE
hierarchies and series: an anchor query UNION ALL a step that references itself
NOT IN with NULLs
one NULL from the subquery makes NOT IN match nothing — use NOT EXISTS
Correlated subquery
runs once per outer row; often rewritable as a JOIN or LATERAL and much faster
UNION vs UNION ALL
UNION removes duplicates (a sort or hash); UNION ALL just appends — faster

Changing data

INSERT … RETURNING id
get generated values back in the same statement (Postgres; SQLite 3.35+)
ON CONFLICT … DO UPDATE
upsert: SET v = EXCLUDED.v takes the new value; needs a unique index on the key
UPDATE … FROM
update using a join (Postgres); forget the join condition and every row is updated
DELETE … USING
delete with a join condition (Postgres); RETURNING * shows what went
BEGIN · COMMIT · ROLLBACK
wrap risky changes and check the row count before committing
TRUNCATE
empty a table at once — no per-row deletes, no row triggers fired

NULLs, types & dates

COALESCE(a, b, 0)
the first non-NULL argument — defaults for missing values
NULLIF(x, 0)
NULL when x = 0, so a / NULLIF(b, 0) returns NULL instead of erroring
x::int · CAST(x AS int)
type conversion; :: is Postgres shorthand, CAST is standard SQL
date_trunc('day', ts)
bucket timestamps by day, week or month for time series (Postgres)
interval '7 days'
date arithmetic: ts >= now() - interval '7 days'
IS DISTINCT FROM
NULL-safe comparison: true when values differ, with two NULLs counted as equal

Performance instincts

EXPLAIN ANALYZE
read the plan before guessing; seq scan on big table = missing index
Index what you filter
WHERE/JOIN/ORDER BY columns; composite index order matters
Leftmost prefix
an index on (a, b) serves filters on a or on a and b, not b alone
Sargable predicates
WHERE f(col)=x kills index use — move the function to the constant
EXISTS vs IN
EXISTS short-circuits; prefer it for "has at least one" checks
N+1 queries
the app-side killer — batch with joins or = ANY(array)
Keyset pagination
WHERE id > :last with ORDER BY id LIMIT 50; OFFSET re-reads every skipped row
Partial index
an index with WHERE status = 'open': small and fast, covering only the rows you query
SELECT *
fetches unneeded columns and blocks index-only scans; name the columns

Worked example: a revenue report that counts twice

Three tables: orders, items (many per order) and payments (many per order, because customers can pay in parts). Ana has two orders. Order 1 has three items (12 + 20 + 8) paid as 25 + 15; order 2 is one 12 item paid in full. Her true totals are 52 billed and 52 paid. The obvious query disagrees:

SELECT o.customer, SUM(i.price) AS billed, SUM(p.amount) AS paid
FROM orders o
JOIN items i         ON i.order_id = o.id
LEFT JOIN payments p ON p.order_id = o.id
GROUP BY o.customer ORDER BY o.customer;

 customer | billed | paid
----------+--------+------
 ana      |     92 |  132
 ben      |     32 |
  1. Count the rows before grouping. Swap the SELECT list for COUNT(*) and drop the GROUP BY: 9 rows, though there are only 6 items. Order 1 matches 3 items and 2 payments, so the join emits 3 × 2 = 6 rows for it.
  2. Explain the numbers. Each of order 1’s items appears twice (40 × 2 + 12 = 92) and each of its payments three times (40 × 3 + 12 = 132).
  3. Collapse each child table to one row per order first. Every join is then one-to-one on order_id, so nothing multiplies.
WITH b AS (SELECT order_id, SUM(price)  AS billed FROM items    GROUP BY order_id),
     p AS (SELECT order_id, SUM(amount) AS paid   FROM payments GROUP BY order_id)
SELECT o.customer, SUM(b.billed) AS billed, COALESCE(SUM(p.paid), 0) AS paid
FROM orders o
JOIN b      ON b.order_id = o.id
LEFT JOIN p ON p.order_id = o.id
GROUP BY o.customer ORDER BY o.customer;

 customer | billed | paid
----------+--------+------
 ana      |     52 |   52
 ben      |     32 |    0

Ben’s paid was blank in the first result: he has no payments, and the SUM of nothing is NULL, not 0. COALESCE fixes the display. The habit worth keeping: whenever a query joins two one-to-many tables, check its row count before trusting a sum.

Recipes

Top 3 products by revenue in each category

WITH ranked AS (
  SELECT category, product, SUM(amount) AS revenue,
         ROW_NUMBER() OVER (PARTITION BY category ORDER BY SUM(amount) DESC) AS rn
  FROM sales
  GROUP BY category, product
)
SELECT category, product, revenue
FROM ranked
WHERE rn <= 3
ORDER BY category, rn;

Window functions run after GROUP BY, so they can rank aggregates; the CTE exists because a window result cannot be filtered in the same query’s WHERE. Use RANK() to keep ties.

Month-over-month revenue growth

WITH monthly AS (
  SELECT date_trunc('month', created_at) AS month, SUM(amount) AS revenue
  FROM orders
  GROUP BY 1
)
SELECT month, revenue,
       ROUND(100.0 * (revenue - LAG(revenue) OVER (ORDER BY month))
             / NULLIF(LAG(revenue) OVER (ORDER BY month), 0), 1) AS growth_pct
FROM monthly
ORDER BY month;

LAG reads the previous month’s row and NULLIF turns a zero baseline into NULL instead of an error; the first month’s growth is NULL by design.

Find duplicate rows and keep one of each

SELECT email, COUNT(*) FROM users GROUP BY email HAVING COUNT(*) > 1;

DELETE FROM users u
USING users d
WHERE u.email = d.email AND u.id > d.id;

The first query lists each duplicated email; the delete keeps the lowest id per email (Postgres syntax). Run it inside a transaction and check the count before committing.

Customers who never ordered, two safe ways

SELECT c.* FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.id IS NULL;

SELECT * FROM customers c
WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id);

Both handle NULLs correctly and usually get the same plan. Avoid NOT IN (SELECT customer_id …): a single NULL customer_id makes it return no rows at all.

Upsert a daily counter

INSERT INTO page_views (page, day, views)
VALUES ('/pricing', CURRENT_DATE, 1)
ON CONFLICT (page, day) DO UPDATE
SET views = page_views.views + EXCLUDED.views;

This needs a unique constraint on (page, day). Insert-or-increment happens atomically, so concurrent requests never lose a count.

When to use it, and when not

Use SQL whenever the data already lives in a relational database and the question is about sets of rows: filter, join, total per group, rank, find what is missing. Doing that work in the database beats pulling rows into application code, because the planner can use indexes and only the answer crosses the network.

  • Good fits: reports and dashboards, data checks before and after a migration, questions during an incident, and multi-row changes that must happen all or nothing inside a transaction.
  • Reach for something else for relevance-ranked full-text search at scale (a search engine), deep graph traversals (recursive CTEs work but get expensive as the depth grows), and per-row logic with many branches, which reads better in application code.
  • Scans over billions of rows belong in a columnar warehouse or DuckDB. The SQL is nearly the same; the storage is built for reading a few columns of every row.

Trade-offs and limits

ChoiceGives youCosts
An indexfast filters, joins and sorts on its columnsslower writes, disk space, one more structure to maintain
A CTEa query that reads top-downPostgres 12+ stores a CTE used twice instead of inlining it, which can block index use
OFFSET pagingjump straight to page Nreads and throws away every skipped row
Keyset pagingthe same cost for every pageno jumping to page 37; needs a unique sort key
UNIONduplicate-free outputa sort or hash over the whole result

Being declarative is the deal: you say what, the planner picks how. The same query can get a different plan after the data grows or the statistics change, so a fast query today is not a promise. EXPLAIN ANALYZE shows what it actually did, and ANALYZE refreshes the statistics it chose from.

Common mistakes

  • Fixing duplicates with DISTINCT. Repeated rows usually mean a missing join condition or a fan-out. DISTINCT hides the repeats, but any SUM computed before it is still inflated.
  • Grouping by a name. GROUP BY customer_name merges two different customers called Ana. Group by the key; Postgres lets you select other columns of the table whose primary key you grouped by.
  • Comparing a timestamp with a date. created_at <= '2026-03-31' means up to midnight at the start of 31 March and drops that whole day. Use a half-open range: >= '2026-03-01' AND < '2026-04-01'.
  • COUNT(*) after a LEFT JOIN. A customer with no orders still produces one row, so COUNT(*) says 1. Count a column from the right-hand table, COUNT(o.id), to get 0.
  • Relying on ORDER BY in a subquery or view. Only the outermost ORDER BY guarantees the order of the result; one inside a subquery may be ignored.

Check yourself

Column x holds 1, 1, NULL and 2. What do COUNT(*), COUNT(x), COUNT(DISTINCT x) and AVG(x) return?

4, 3, 2 and 1.333… Every aggregate except COUNT(*) skips the NULL, so the average is 4 / 3, not 4 / 4.

Why does SELECT price * qty AS total FROM items WHERE total > 100 fail, when … ORDER BY total works?

WHERE runs before SELECT, so the alias does not exist yet; Postgres reports column "total" does not exist. ORDER BY runs after SELECT and can see it. Repeat the expression in WHERE, or filter in an outer query.

In PostgreSQL, what does SELECT 7 / 2, 7 / 2.0 return?

3 and 3.5000000000000000. Two integers divide as integers and truncate; one numeric operand makes the whole division numeric.

There is an index on (customer_id, created_at). Which filters can seek into it: (a) customer_id = 7, (b) created_at > now() - interval '1 day', (c) both together?

(a) and (c). The index is sorted by customer_id first, so a filter on created_at alone has no leading column to seek on. It needs its own index, or one that starts with created_at.

Questions about SQL Cheat Sheet

What is the difference between WHERE and HAVING in SQL?

WHERE filters individual rows before they are grouped, so it can only use columns from the tables in the FROM clause. HAVING filters whole groups after GROUP BY has run, so it can use aggregates such as COUNT or SUM. Total sales per customer with HAVING SUM(amount) over 1000 keeps only big customers, while WHERE amount over 1000 would keep only big individual orders before summing. Put every condition that does not need an aggregate in WHERE, because filtering early leaves less work for the grouping step.

What is the difference between INNER JOIN and LEFT JOIN?

An INNER JOIN returns only rows that have a match on both sides of the join condition. A LEFT JOIN returns every row from the left table and fills the right-hand columns with NULL where no match exists, which is how you find customers with no orders or report zero counts. A common mistake is a WHERE condition on a right-table column after a LEFT JOIN: rows without a match have NULL there, fail the condition and disappear, silently turning it back into an inner join. Put such conditions in the ON clause instead.

What is the difference between RANK, DENSE_RANK and ROW_NUMBER?

All three number rows within a window in ORDER BY order; they differ only on ties. ROW_NUMBER gives every row a unique number and breaks ties arbitrarily unless the ORDER BY makes rows unique. RANK gives tied rows the same number and then skips, producing 1, 1, 3. DENSE_RANK gives tied rows the same number without gaps, producing 1, 1, 2. Use ROW_NUMBER for exactly N rows per group, as in deduplication, RANK when ties should share a place, and DENSE_RANK for questions such as the second-highest salary.

Why does NOT IN return no rows when the list contains a NULL?

Because x NOT IN (a, b, NULL) means x differs from a, and x differs from b, and x differs from NULL, and a comparison with NULL is unknown rather than true. A chain of ANDs that contains an unknown can never be true, so no row passes the filter. This bites most often with NOT IN followed by a subquery over a nullable column. Use NOT EXISTS with a correlated subquery, or a LEFT JOIN with an IS NULL check; both behave as intended when NULLs are present and usually perform at least as well.