Skip to content

WHERE, HAVING and FILTER

Filter rows before grouping, filter groups after, and count several conditions in one query.

Applied ~7 min8 steps

What runs in CodeSplash

You will be able to

  • Choose between WHERE and HAVING
  • State the order in which a query's clauses run
  • Compute conditional counts and shares with FILTER

Read first

01

Before or after grouping?

Read

WHERE filters rows before they're grouped. HAVING filters groups after they're summarized.

SELECT island, count(*) AS n
FROM palmer_penguins_sample
GROUP BY island
HAVING count(*) >= 100
ORDER BY n DESC;

You can't write this with WHERE, because count(*) doesn't exist until after the grouping. A query runs in this order, whatever order you write it in:

FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT

02

Predict the result

Predict

Predict first

Which species does this return?

SELECT species, count(*) AS n FROM palmer_penguins_sample WHERE body_mass > 4000 GROUP BY species HAVING count(*) > 50;

Adelie, Chinstrap and GentooOnly GentooNone
Reveal

Only Gentoo, with 122 birds. WHERE first keeps the penguins over 4,000 grams. Grouping leaves 35 Adelies, 15 Chinstraps and 122 Gentoos, and HAVING then keeps the groups with more than 50.

03

Try both

In the app

Run the query, then move the condition around.

SELECT species, count(*) AS n
FROM palmer_penguins_sample
WHERE body_mass > 4000
GROUP BY species
HAVING count(*) > 50
ORDER BY species;

Delete the WHERE line and run it again. Now you get all three species, because each has more than 50 birds in total. Same HAVING, different rows going in.

04

Count several things at once

Explore

FILTER applies a condition to one aggregate, so one query can answer several questions:

SELECT species,
       count(*) AS n,
       count(*) FILTER (WHERE body_mass > 4000) AS heavy,
       round(100.0 * count(*) FILTER (WHERE body_mass > 4000) / count(*), 1) AS pct_heavy
FROM palmer_penguins_sample
GROUP BY species
ORDER BY species;

Run it. A WHERE here would have removed the light birds from n as well, and every percentage would be 100. Change 4000 to 3500 and see how the shares move.

Multiply by 100.0, not 100, to stay out of integer arithmetic.

05

Write your own HAVING

In the app

Now write one from a task. It opens in a new SQL tab, and Check compares your result with the answer's.

SQL exercise · penguins

Filter on a group's summary

Which islands have more than 100 penguins? Show the island and the count, calling it n.

Hints
  • WHERE filters rows before grouping; HAVING filters groups after.
Show a solution
SELECT island, count(*) AS n
FROM {{table}}
GROUP BY island
HAVING count(*) > 100;

In the app this opens in a real SQL tab on the sample table and checks your result.

06

Check your understanding

Checkpoint

Check yourself

Using the FILTER query, how many Adelie penguins weigh more than 4,000 grams?

Show the answer

Answer: 35

35 of 152. FILTER applies only to that one count.

Check yourself

You want islands with an average body mass over 4,500 grams. Which do you use?

Show the answer

Answer: b

The average doesn't exist until rows are grouped, so WHERE can't see it.

Check yourself

Put these clauses in the order a query runs them.

  1. 1SELECT
  2. 2GROUP BY
  3. 3WHERE
  4. 4HAVING
  5. 5FROM
  6. 6ORDER BY

Show the answer

The order:

  1. FROM
  2. WHERE
  3. GROUP BY
  4. HAVING
  5. SELECT
  6. ORDER BY

This order explains why WHERE can't use an aggregate, and why ORDER BY can use a name you gave in SELECT.

07

From counts to a test

Reflect

You've got heavy and light counts by species: a table of counts. Does the share of heavy birds really differ between species, or could this be chance? Counts in a table call for a different test from means:

08

Recap

Recap
  • WHERE filters rows before grouping; HAVING filters groups after.
  • Clauses run in a fixed order, which isn't the order you write them.
  • FILTER gives one aggregate its own condition, so you can compute shares in one query.

Concepts in this lesson