Skip to main content
Back to SQL Coding

SQL Coding · Cheat Sheet

Reading the Query

Tip: Use your browser's print function (Ctrl+P / Cmd+P) to save as PDF for offline study.

Cheatsheet: Reading the Query

One page for Topic 1. When a query lands on your desk, work down this page top to bottom.


Processing order (memorize this)

SQL is written SELECT-first but processed FROM-first. Read it the way the database runs it:

StepClauseQuestion it answers
1FROMWhich table(s) is this pulling from?
2WHEREWhich rows survive before anything is counted or summed?
3SELECTOf the surviving rows, what is actually returned or calculated?

Jumping to FROM first — not reading top to bottom — is the single habit that makes queries stop feeling like a wall of text.


Alias honesty

AS renames an output column. It changes no data — it's a label the author chose.

What you seeWhat it actually computesVerdict
SUM(order_total) AS total_spentSum of order totalsHonest
COUNT(DISTINCT session_id) AS total_usersCount of sessionsLying alias — verify the expression, not the name
... AS conversion_rateArithmetic done on the flyA formula wearing a friendly name — only as trustworthy as the math next to it

Two rules:

  • Calculated columns (sums, counts, ratios) exist only in this query's output — they are not facts stored in a table.
  • The fastest way to catch a misleading dashboard number: check whether the alias matches what's being computed.

WHERE operators

OperatorMeaningGotcha
=Equals—
!= or <>Not equalSilently excludes NULL rows — "not US" drops unknown-country rows too
BETWEEN 10 AND 2010 ≤ x ≤ 20Inclusive on both ends
IN ('A','B','C')Shorthand for chained OR equalitiesSame precedence risk as OR when mixed with AND
ANDBoth conditions must be trueEvaluated before OR
OREither condition sufficesUsually returns far more rows than intended if misplaced

AND/OR precedence — the parentheses rule

SQL evaluates AND before OR. "CA or NY customers who spent over $500":

-- Correct
WHERE (state = 'CA' OR state = 'NY') AND total_spent > 500

-- Wrong: means "ALL CA customers, plus NY customers over $500"
WHERE state = 'CA' OR state = 'NY' AND total_spent > 500

Rule: if a WHERE mixes OR and AND without parentheses, don't trust your first reading — rewrite it with explicit parentheses to see what it actually does. Nothing errors; it just returns a plausible-looking wrong number.


Join types — which rows survive

Two tables: customers (one row per customer) and orders (many or zero per customer).

Join typeCustomer with 0 ordersCustomer with 3 ordersUse when
INNER JOINDisappears entirelyAppears 3 timesOnly matched rows matter
LEFT JOINAppears once, order columns NULLAppears 3 timesThe query claims to show "all" of the left table
RIGHT JOINMirror of LEFTMirror of LEFTRare — rewrite as LEFT by swapping table order

"All"/"every" rule: any query whose stated purpose includes "all customers" / "every campaign" needs a LEFT JOIN from that table. An INNER JOIN silently drops the no-match rows and understates the count — the join type, not the WHERE clause, is the first thing to verify.


The multiplication trap

Joining two "many" tables per key creates every combination:

Customer hasResult rows
3 orders, 2 addresses (both joined)3 × 2 = 6 rows, not 5

Any SUM() taken after a row-multiplying join is silently inflated — this is the most common inflated-revenue bug in real dashboards and in AI-generated "summary" queries.


The row-count habit

Before trusting any query with a JOIN, ask:

  1. How many rows should this reasonably return? (One per customer? One per order? One per day?)
  2. Does the join type match that expectation?
  3. Did any join multiply rows before an aggregate was taken?

Traps

  • Reading SELECT before FROM — you form a guess before you know what table it pulls from.
  • Trusting the alias over the expression (AS total_users on a session count).
  • OR mixed with AND without parentheses — AND binds first; the query silently includes the wrong rows.
  • != treated as if it catches "unknown" — it drops NULL rows instead.
  • INNER JOIN on a query that claims to show "all X" — non-matching rows vanish.
  • SUM() after joining two many-per-key tables — the join multiplied rows first.
  • Trusting the plain-English explanation over the code — when they disagree, the code is ground truth.

Aligned to the PostgreSQL documentation and standard SQL semantics.

Make this cheat sheet yours

Personalize this sheet — focus it however you study, or build one from the exact questions you keep getting wrong.

Sign up free to create a personalized cheat sheet.