WHERE, HAVING and FILTER
Filter rows before grouping, filter groups after, and count several conditions in one query.
What runs in CodeSplash
- χ² test of independence Verified in Python and R
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
Before or after grouping?
ReadWHERE 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
Predict the result
PredictPredict 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;
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.
Try both
In the appRun 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.
Count several things at once
ExploreFILTER 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.
Write your own HAVING
In the appNow 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.
Check your understanding
CheckpointCheck 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.
Show the answer
The order:
- FROM
- WHERE
- GROUP BY
- HAVING
- SELECT
- ORDER BY
This order explains why WHERE can't use an aggregate, and why ORDER BY can use a name you gave in SELECT.
From counts to a test
ReflectYou'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:
Recap
RecapWHEREfilters rows before grouping;HAVINGfilters groups after.- Clauses run in a fixed order, which isn't the order you write them.
FILTERgives one aggregate its own condition, so you can compute shares in one query.
Concepts in this lesson