WHERE
Filter rows conditionally with WHERE using comparison operators, logical operators (AND, OR, NOT), NULL checks, and pattern matching.
While select acts like a vertical cookie cutterโchoosing which columns to showโthe WHERE clause acts like a horizontal filter or a bouncer at the door. It evaluates every row in the table against one or more rules and discards any row that does not pass the test.
Without WHERE, every query would dump the entire contents of your database into memory.
Basic Filtering Syntax
The WHERE clause always comes immediately after the FROM clause:
SELECT name, price, category
FROM products
WHERE price < 50;
Given this table:
TABLE: products
โโโโโโฌโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโฌโโโโโโโโโโโโ
โ id โ name โ price โ category โ
โโโโโโผโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโผโโโโโโโโโโโโค
โ 1 โ Wireless Mouse โ 24.99 โ Tech โ
โ 2 โ Ergonomic Desk โ 299.00 โ Furniture โ
โ 3 โ Coffee Mug โ 12.50 โ Kitchen โ
โ 4 โ Mechanical Board โ 89.99 โ Tech โ
โโโโโโดโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโดโโโโโโโโโโโโ
The query evaluates each rowโs price. Rows 1 and 3 pass; rows 2 and 4 are discarded:
โโโโโโโโโโโโโโโโโโฌโโโโโโโโฌโโโโโโโโโโโ
โ name โ price โ category โ
โโโโโโโโโโโโโโโโโโผโโโโโโโโผโโโโโโโโโโโค
โ Wireless Mouse โ 24.99 โ Tech โ
โ Coffee Mug โ 12.50 โ Kitchen โ
โโโโโโโโโโโโโโโโโโดโโโโโโโโดโโโโโโโโโโโ
Comparison Operators
SQL provides standard mathematical and relational comparisons:
| Operator | Meaning | Example |
|---|---|---|
= | Equals (Notice single equals, not ==!) | WHERE status = 'active' |
!= or <> | Not equal | WHERE status != 'archived' |
>, < | Greater than, Less than | WHERE age >= 21 |
BETWEEN a AND b | Value falls within range (inclusive) | WHERE price BETWEEN 10 AND 50 |
IN (...) | Matches any item in a list | WHERE city IN ('London', 'Tokyo', 'Paris') |
-- Finding users in target cities
SELECT name, city
FROM customers
WHERE city IN ('London', 'Madrid');
Combining Conditions: AND, OR, and NOT
You can chain multiple criteria together using logical operators:
AND: Returns the row only if both conditions are true.OR: Returns the row if either condition is true.NOT: Inverts the condition.
SELECT name, department, salary
FROM employees
WHERE department = 'Engineering' AND salary > 80000;
The Parentheses Trap (Operator Precedence)
In SQL, AND has higher precedence than OR. The database evaluates AND operations first unless you group them with parentheses ():
-- DANGEROUS / MISLEADING:
SELECT name, city, is_active
FROM customers
WHERE city = 'London' OR city = 'Paris' AND is_active = TRUE;
-- Evaluated as: (city = 'London') OR (city = 'Paris' AND is_active = TRUE)
-- Inactive London customers will be mistakenly included!
-- SAFE AND EXPLICIT:
SELECT name, city, is_active
FROM customers
WHERE (city = 'London' OR city = 'Paris') AND is_active = TRUE;
Always wrap OR conditions in parentheses when combining them with AND.
Pattern Matching with LIKE
When searching for text without knowing the exact string, use LIKE with wildcards:
%: Matches zero, one, or multiple characters._: Matches exactly one single character.
-- Find any customer whose email ends in '@gmail.com'
SELECT name, email
FROM customers
WHERE email LIKE '%@gmail.com';
-- Find phone codes with pattern 'US-01', 'US-09', etc.
SELECT code
FROM regions
WHERE code LIKE 'US-0_';
The NULL Trap: Why = NULL Fails
In SQL, NULL does not mean zero or empty text "". It means value unknown or missing.
Because NULL is unknown, SQL follows three-valued logic: TRUE, FALSE, or UNKNOWN.
If you ask SQL: โIs an unknown value equal to another unknown value?โ, SQL responds: โI donโt know (UNKNOWN)โ.
-- CATASTROPHIC ERROR: Returns 0 rows every single time!
SELECT * FROM users WHERE deleted_at = NULL;
-- THE CORRECT SQL WAY:
SELECT * FROM users WHERE deleted_at IS NULL;
SELECT * FROM users WHERE email IS NOT NULL;
[!IMPORTANT] Never use
= NULLor!= NULL. Always useIS NULLandIS NOT NULL.
Common Traps to Avoid
- Using Double Quotes for Text: In SQL, single quotes (
'New York') represent text strings. Double quotes ("New York") are reserved in standard SQL for identifiers like table or column names with spaces. - Writing
==for Equality: Programming languages like Python use==. SQL uses a single=for comparison. - Searching for Case-Sensitive Text with
=: In systems like PostgreSQL,'Alice'does not match'alice'. Use lower-casing (LOWER(name) = 'alice') orILIKEfor case-insensitive comparisons.
Next Steps
Master this concept to build a rock-solid foundation. Next, proceed to comparison-operators.