×

sql · level 2 · unit 1 · lesson 3

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

WHERE picks rows. picks groups.

Which genres have more than one film? WHERE cannot answer that. It looks at one row at a time, before any groups exist, and a single row has nothing to count.

HAVING works after the grouping. It looks at each group's aggregate and keeps only the groups that pass. It goes straight after GROUP BY, before ORDER BY.

You can use both in one query: WHERE sieves the rows, then HAVING sieves the groups made from them.

SELECT genre, COUNT(*) AS films
FROM films
GROUP BY genre
HAVING COUNT(*) > 1;

Put COUNT(*) in WHERE and SQLite stops with misuse of aggregate function COUNT().