NO. 02 · 7 exercises

2. Filtering

WHERE, comparisons, and the strange case of NULL.

The lesson

Filtering

Last chapter you learned to ask a table for columns. But a real question is usually pickier: not "show me all the tracks", but "show me the tracks that last more than five minutes" or "the customers who live in Spain". That pickiness lives in one clause: WHERE.

WHERE is the sieve. It looks at every row, decides whether that row qualifies, and only keeps the ones that say yes. The rest of your SELECT — columns, aliases, computed expressions — stays exactly the same.

WHERE: keeping only what you asked for

The shape is simple:

SELECT title, duration_sec
FROM tracks
WHERE duration_sec > 300;

The condition duration_sec > 300 is checked against every row of tracks. Rows where it's true — eleven of our tracks pass the five-minute mark — come back; the rest are thrown away. Notice you're still choosing columns with SELECT: WHERE only decides which rows, never which columns.

The comparison operators are the ones you know from math:

OperatorMeaningExample
>greater thanduration_sec > 300
<less thanduration_sec < 200
>=greater than or equalduration_sec >= 300
<=less than or equalrelease_year <= 1980
=equalunit_price = 0.99
<> (or !=)not equalgenre_id <> 3

Comparing text

Numbers compare like numbers, but text has a quirk: you must write it in quotes, and the match is exact — capitals, accents and spaces all count.

SELECT first_name, last_name
FROM customers
WHERE country = 'Spain';

Two customers live in Spain, and exactly those two rows come back. 'spain' or ' Spain' would find nothing. Dates, as it happens, are stored as text ('2022-01-15'), and because of the clever year-month-day format, comparing them with > and < gives correct chronological results for free.

Combining conditions: AND

One condition is rarely enough. The bargain shelf is for tracks that are cheap and long:

SELECT title
FROM tracks
WHERE unit_price = 0.99 AND duration_sec > 300;

AND means both conditions must be true. Four tracks pass. If you want at least one condition to be true, you use OR:

SELECT title
FROM tracks
WHERE unit_price = 0.99 OR duration_sec > 300;

Now every cheap track counts, every long track counts, and a track that is both is still listed once. OR is the "either way is fine" operator.

OR and the power of parentheses

What if you want "cheap, or long — but only in Rock"? A naive attempt:

WHERE unit_price = 0.99 OR duration_sec > 300 AND genre_id = 1

That is not what you said. Like multiplication before addition, AND binds tighter than OR, so this means: cheap, or (long and Rock) — the cheap tracks of any genre sneak in.

Parentheses override the order, exactly as in arithmetic:

SELECT title
FROM tracks
WHERE (unit_price = 1.29 AND genre_id = 1) OR duration_sec > 360;

Read it out loud: "premium Rock tracks, or anything longer than six minutes". When you mix AND and OR, put parentheses around the group that belongs together. When in doubt, add them even if not strictly needed — they cost nothing and make intent visible.

BETWEEN: a range in one breath

A range with two comparisons (release_year >= 2005 AND release_year <= 2015) is so common that SQL has a shorthand:

SELECT title
FROM albums
WHERE release_year BETWEEN 2005 AND 2015;

BETWEEN is inclusive: both 2005 and 2015 count, and seven albums land in the window. It works on dates too — joined_date BETWEEN '2022-01-01' AND '2022-12-31' finds everyone who signed up during 2022.

IN: a list of values

Sometimes you don't want a range, you want a shopping list. "Artists from the UK, Sweden or Brazil" — as three ORs it's noisy; as a list it's one line:

SELECT name
FROM artists
WHERE country IN ('UK', 'Sweden', 'Brazil');

IN checks a column against a list of values, and it works with numbers just the same: WHERE genre_id IN (1, 4) selects Rock and Folk tracks. It's pure shorthand — the database reads it exactly like a chain of ORs.

The strange case of NULL

Some rows have no value at all. Two artists have no formation year, the live album has no release year, and two customers have no support representative. That absence is called NULL, and it breaks the rules you just learned.

NULL is not a value — it's the absence of one. So formed_year = NULL isn't a condition, it's a mistake: the result is neither true nor false, and the row quietly disappears from your answer. Same with <> NULL. Comparisons never touch NULL rows.

The only correct way to ask about them:

SELECT first_name, last_name
FROM customers
WHERE support_rep_id IS NULL;

Two customers come back — the ones with nobody assigned. And the opposite:

SELECT title, release_year
FROM albums
WHERE release_year IS NOT NULL;

Every album with a recorded year. IS NULL and IS NOT NULL are the only questions NULL can answer, and they always can — never true and false at once, always exactly one of the two.

Putting it all together

The anniversary call list mixes everything: a country group, a date comparison, an OR trapped inside an AND, and a NULL clause standing apart:

SELECT first_name, last_name
FROM customers
WHERE ((country = 'Spain' OR country = 'Brazil') AND joined_date > '2022-01-01')
   OR support_rep_id IS NULL;

Five customers. Read the parentheses the way the database does: first the parenthesized group (Spanish or Brazilian, and joined after the new year), then "or has no rep at all".

What you just learned

  • WHERE keeps only the rows where the condition is true; it never touches which columns you see.
  • Numbers compare with >, <, =, and friends; text must be quoted and matches exactly.
  • AND requires both conditions, OR requires at least one, and parentheses decide which groups first.
  • BETWEEN a AND b is an inclusive range; IN (...) is a list of allowed values.
  • NULL is not a value: it never matches =, and only IS NULL / IS NOT NULL can test for it.

Dialect note. Everything in this chapter works identically in SQLite, PostgreSQL and MySQL. The one rule you must carry everywhere is the NULL rule: = NULL never works, in any database. (PostgreSQL and MySQL do offer extra null-safe equality operators — IS NOT DISTINCT FROM and <=> — but SQLite keeps it simple with just IS NULL, and that's all you ever need.)

Time to sieve some rows. The exercises assume you've read this far — each one is a minute or two.