Filtering rows with WHERE
Keep the rows you mean with WHERE, combine conditions, and learn why comparing with NULL finds nothing.
What runs in CodeSplash
- Missing-data patterns and MCAR tests Verified in Python and R
You will be able to
- Filter rows with WHERE, AND, OR, IN, BETWEEN and LIKE
- Find missing values with IS NULL
- Predict what a condition does to rows with missing values
Read first
Keep the rows you mean
ReadWHERE keeps the rows where a condition is true.
SELECT species, island, body_mass
FROM palmer_penguins_sample
WHERE island = 'Biscoe';
Text goes in single quotes. Column names don't, unless they contain spaces or punctuation, and then they take double quotes: "Solar.R".
Combine conditions with AND and OR, and use parentheses when you mix them.
Count a filter
In the appRun these one at a time. Change SELECT count(*) to SELECT * whenever you want to see the rows.
SELECT count(*) AS n
FROM palmer_penguins_sample
WHERE species = 'Gentoo' AND body_mass > 5000;
SELECT count(*) AS n
FROM palmer_penguins_sample
WHERE island IN ('Dream', 'Torgersen');
SELECT count(*) AS n
FROM palmer_penguins_sample
WHERE flipper_len BETWEEN 190 AND 200;
SELECT count(*) AS n
FROM palmer_penguins_sample
WHERE species LIKE 'G%';
A tab runs every statement and gives each its own result. BETWEEN includes both ends. In LIKE, % matches anything.
A trap with missing values
PredictPredict first
Eleven penguins have no recorded sex. How many rows does this return?
SELECT * FROM palmer_penguins_sample WHERE sex = NULL;
Reveal
Zero. NULL means unknown, so sex = NULL asks "is sex equal to something unknown?", and the answer is unknown, not true. WHERE keeps only rows where the condition is true. To find missing values, write sex IS NULL.
What a NOT misses
ExploreA NULLNULLSQL's marker for a missing value. Comparing with it gives unknown, so use IS NULL rather than = NULL. also hides from conditions that look exhaustive. Run these.
SELECT count(*) AS n FROM palmer_penguins_sample WHERE sex = 'male';
SELECT count(*) AS n FROM palmer_penguins_sample WHERE sex <> 'male';
SELECT count(*) AS n FROM palmer_penguins_sample WHERE sex IS NULL;
You'll get 168, 165 and 11. "Male" and "not male" should cover everyone, yet together they miss 11 birds. Comparing an unknown value gives unknown, so those rows fail both conditions.
When you mean to keep them, say so:
SELECT count(*) AS n
FROM palmer_penguins_sample
WHERE sex <> 'male' OR sex IS NULL;
Write your own filter
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
Keep only some rows
List the species, island and body mass of every penguin heavier than 5,500 grams.
SELECT species, island, body_mass
FROM {{table}}
LIMIT 10;Hints
- WHERE goes after FROM and keeps the rows where its condition is true.
- Remove the LIMIT, or you may cut off rows the answer needs.
Show a solution
SELECT species, island, body_mass
FROM {{table}}
WHERE body_mass > 5500;In the app this opens in a real SQL tab on the sample table and checks your result.
Check your understanding
CheckpointCheck yourself
Run the first query in the earlier step. How many Gentoo penguins weigh more than 5,000 grams?
Show the answer
Answer: 61
Both conditions must be true for a row to be kept.
Check yourself
How many penguins have a flipper length from 190 to 200 mm, inclusive?
Show the answer
Answer: 117
BETWEEN 190 AND 200 is the same as flipper_len >= 190 AND flipper_len <= 200.
Check yourself
How many rows does WHERE sex <> 'male' OR sex IS NULL return?
Show the answer
Answer: 176
: the rows that are not male, plus the ones with no recorded sex.
Check yourself
You want Gentoo or Adelie penguins over 5,000 grams. Which is right?
Show the answer
Answer: b
AND binds tighter than OR, so the first keeps every Gentoo, whatever its weight. The third asks for a penguin that's two species at once.
Missing on purpose?
ReflectThink of a filter you've written recently. Did it silently drop rows with a missing value? Would that change your answer? Whether values are missing at random is a question you can test:
Recap
RecapWHEREkeeps rows where a condition is true.- Find missing values with
IS NULL;= NULLfinds nothing. <>skips rows with missing values too. Say what you want done with them.
Concepts in this lesson