skip to today's puzzle

Free visual guide

SQL WHERE vs HAVING

WHERE filters individual rows before grouping happens. HAVING filters whole groups after. Picture a tray of decorated sugar cookies and a stack of pastel tins.

Six-panel SQL Sip Cookie Tin Guide: WHERE price >= 4 removes Strawberry Sugar; GROUP BY shape gives Heart 1, Star 2 and Moon 1; HAVING COUNT(*) > 1 keeps only Star with 2 cookies.

The three steps

  1. WHERE sets aside cookies that cost under 4.
  2. GROUP BY puts the remaining cookies into tins by shape — all the heart cookies together, and all the star cookies together.
  3. HAVING keeps only tins holding more than one cookie.

Both in one query

SELECT shape, COUNT(*) AS cookies
FROM cookies
WHERE price >= 4
GROUP BY shape
HAVING COUNT(*) > 1;

Rule of thumb: if the condition is about one row, use WHERE. If it uses an aggregate like COUNT or SUM, use HAVING.

Want the full example with every starting row? Read the WHERE vs HAVING lesson.

Ready to practice?

Chapter 4 of the Foundations Track is all about filtering groups. Tap tiles, see real results. No terminal, no setup.

practice WHERE & HAVING