4.4.5Databases

SQL clauses — WHERE, GROUP BY, HAVING, ORDER BY, LIMIT

2,199 words10 min readdifficulty · medium

The Logical Processing Order (memorize this)

Figure — SQL clauses — WHERE, GROUP BY, HAVING, ORDER BY, LIMIT

WHERE — filter rows


GROUP BY — collapse rows into groups


HAVING — filter the groups


ORDER BY — sort the result


LIMIT — keep only some rows

Recall Feynman: explain to a 12-year-old

Imagine a big box of trading cards.

  • WHERE = "throw away cards I don't want" (one card at a time).
  • GROUP BY = "make piles, one pile per team."
  • HAVING = "throw away whole piles that have fewer than 5 cards."
  • ORDER BY = "line up the remaining piles biggest-first."
  • LIMIT = "just hand me the top 3 piles." You can't throw away a pile before the piles exist — that's why HAVING comes after the piling, not before. Simple!

Flashcards

What is SQL's logical processing order of clauses?
FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT
Why can't WHERE use aggregate functions like COUNT(*)?
WHERE runs before GROUP BY, so groups (and their aggregates) don't exist yet.
What is the one-line difference between WHERE and HAVING?
WHERE filters individual rows before grouping; HAVING filters whole groups after grouping (can use aggregates).
Can ORDER BY use a SELECT alias? Why?
Yes — ORDER BY runs after SELECT, so aliases are already defined.
Can WHERE use a SELECT alias? Why?
No — WHERE runs before SELECT, so the alias doesn't exist yet.
Why must LIMIT be paired with ORDER BY for "top N"?
Without ORDER BY the result order is unspecified, so LIMIT returns arbitrary rows, not the largest ones.
With GROUP BY, which columns may appear bare in SELECT?
Only the grouping columns; all others must be inside an aggregate function.
How do you find NULL values, and why not = NULL?
Use IS NULL; = NULL yields NULL (not TRUE) because NULL means "unknown".
What does LIMIT n OFFSET m do?
Skips m rows then returns up to n rows — the basis of pagination.
In which stage are column aliases created?
In the SELECT stage (step 5 of the logical order).

Connections

  • SQL SELECT basics — the clause these all attach to
  • Aggregate functions (COUNT, SUM, AVG, MIN, MAX) — what GROUP BY summarizes
  • JOINs — part of the FROM stage, earliest step
  • NULL handling and three-valued logic — why IS NULL exists
  • Subqueries and CTEs — workaround when aliases/aggregates aren't visible yet
  • Indexes — why WHERE-before-GROUP-BY ordering matters for performance
  • Pagination patterns — LIMIT/OFFSET in practice

Concept Map

raw rows

survivors

one row per group

filtered groups

result set

sorted

enables

usable in

cannot see

defines

visible to

invisible to

FROM and JOINs

WHERE filters rows

GROUP BY makes groups

HAVING filters groups

SELECT computes columns and aliases

ORDER BY sorts

LIMIT keeps some rows

Aggregates like COUNT

Column aliases

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, SQL query likhne ka order aur uske chalne (execute hone) ka order alag hota hai. Hum likhte hain SELECT ... FROM ... WHERE ... GROUP BY ... HAVING ... ORDER BY ... LIMIT, lekin database andar-andar isko is order me process karta hai: pehle FROM (rows lao), phir WHERE (ek-ek row filter karo), phir GROUP BY (rows ko piles/groups me baant do), phir HAVING (poore groups ko filter karo), phir SELECT (columns aur alias banao), phir ORDER BY (sort karo), aur last me LIMIT (sirf top N rows rakho). Yeh order yaad kar lo to SQL ke 80% confusion khatam.

Sabse important baat: WHERE rows pe kaam karta hai, HAVING groups pe. Isliye COUNT(*) jaisa aggregate WHERE me nahi chalega — kyunki us time tak groups bane hi nahi hote. Aggregate ka condition lagana ho to HAVING use karo. Aur ek aur trap: alias (jaise AS headcount) WHERE me kaam nahi karega kyunki SELECT abhi tak run hua hi nahi, par ORDER BY me chalega kyunki woh SELECT ke baad aata hai.

LIMIT ke saath hamesha ORDER BY lagana, warna "top 3" maangne par database koi bhi random 3 rows de dega — silent bug ban jaata hai. Aur NULL dhoondhna ho to IS NULL likho, = NULL kabhi mat likhna, kyunki NULL ka matlab "pata nahi" hota aur NULL = NULL bhi TRUE nahi hota.

Bas yeh mental picture rakho — cards ka box: pehle bekar cards phenko (WHERE), phir team-wise piles banao (GROUP BY), chhoti piles hatao (HAVING), badi-se-chhoti lagao (ORDER BY), top 3 utha lo (LIMIT). Itna clear ho gaya to exam aur interview dono nikal jaayenge.

Go deeper — visual, from zero

Test yourself — Databases

Connections