After this lesson you can
- Filter rows with comparisons, AND, OR, IN and LIKE
- Explain why a comparison with NULL is never true
- Use IS NULL instead of = NULL
WHERE decides which rows survive. It runs after FROM and before
SELECT, which is why you can filter on a column you never return.
SELECT name
FROM customers
WHERE country = 'GE' AND credit > 0;
AND binds tighter than OR, so parenthesise the moment you mix them.
WHERE a AND b OR c almost never means what the person typing it meant.
country IN ('GE', 'PL') is shorthand for country = 'GE' OR country = 'PL'. LIKE matches a pattern: % stands for any run of characters,
including none, and _ for exactly one, so name LIKE 'Ni%' keeps every
name that starts with Ni.
Try it
CREATE TABLE customers ( id int PRIMARY KEY, name text NOT NULL, country text NOT NULL, credit int);INSERT INTO customers VALUES (1, 'Nino Beridze', 'GE', 500), (2, 'Ana Kapanadze', 'GE', 1200), (3, 'Luka Meladze', 'DE', 0), (4, 'Mari Tsiklauri', 'PL', 300), (5, 'Giorgi Abashidze', 'GE', NULL);SELECT name, country, creditFROM customersWHERE country IN ('GE', 'PL') AND credit > 0;NULL is not a value
NULL means unknown. Comparing an unknown to anything gives another
unknown, and a WHERE clause keeps a row only when the answer is true.
So WHERE credit = NULL returns nothing, ever. Not an error, not a warning:
nothing. The same is true of <> NULL.
SELECT name FROM customers WHERE credit IS NULL;
IS NULL and IS NOT NULL are the only comparisons that answer the
question. Reach for them the moment a column is nullable.
= NULL reads naturally and fails silently, which is why people write it
with confidence, in code and in interviews.
CREATE TABLE customers ( id int PRIMARY KEY, name text NOT NULL, country text NOT NULL, credit int);INSERT INTO customers VALUES (1, 'Nino Beridze', 'GE', 500), (2, 'Ana Kapanadze', 'GE', 1200), (3, 'Luka Meladze', 'DE', 0), (4, 'Mari Tsiklauri', 'PL', 300), (5, 'Giorgi Abashidze', 'GE', NULL);SELECT (SELECT COUNT(*) FROM customers WHERE credit = NULL) AS equals_null, (SELECT COUNT(*) FROM customers WHERE credit <> 500) AS not_500, (SELECT COUNT(*) FROM customers WHERE credit IS NULL) AS is_null;Common mistakes
- Mixing
ANDandORwithout parentheses. "Georgian or Polish customers with more than 400 credit" written ascountry = 'GE' OR country = 'PL' AND credit > 400reads asGE OR (PL AND credit > 400), so every Georgian comes back — including one whose credit is unknown.
CREATE TABLE customers ( id int PRIMARY KEY, name text NOT NULL, country text NOT NULL, credit int);INSERT INTO customers VALUES (1, 'Nino Beridze', 'GE', 500), (2, 'Ana Kapanadze', 'GE', 1200), (3, 'Luka Meladze', 'DE', 0), (4, 'Mari Tsiklauri', 'PL', 300), (5, 'Giorgi Abashidze', 'GE', NULL);SELECT name, country, creditFROM customersWHERE country = 'GE' OR country = 'PL' AND credit > 400;- Expecting
<>to keep the NULL rows.credit <> 500is unknown for aNULLcredit, so that row is dropped too: four rows have a credit that is not 500 as far as a person is concerned, and the count above says three. - Quoting a string with double quotes. In standard SQL
"GE"is a column name, not text; strings take single quotes. - Filtering on an alias from the
SELECTlist.WHEREruns beforeSELECT, so the alias does not exist yet. Repeat the expression.
What interviewers ask
- Why does
WHERE x <> 5miss rows wherexis NULL? The comparison is unknown, not true, andWHEREkeeps only true. AddOR x IS NULLif those rows belong in the answer. - Which binds tighter,
ANDorOR?AND. Parenthesise whenever both appear. - Can
WHEREuse a column you do not select? Yes — it runs beforeSELECT, against every column of the tables in .
Try it yourself
2 visible tests · 2 hidden testsTable customers(id, name, country, credit), where credit may be
NULL. Return the name of every customer whose credit is NULL —
not zero, genuinely unknown.
CREATE TABLE customers ( id int PRIMARY KEY, name text NOT NULL, country text NOT NULL, credit int);INSERT INTO customers VALUES (1, 'Nino', 'GE', 500), (2, 'Ana', 'GE', 0), (3, 'Luka', 'DE', NULL), (4, 'Mari', 'PL', NULL);Sign up to run the tests, check the hidden ones and save your progress. Sign up