WHERE — Filtering Rows
Add conditions to your queries so you only get back the rows that actually matter to you.
WHERE is your filter. Without it, you get everything. With it, you get exactly what you need.
EXERCISE
1WHERE — Filtering with Comparison Operators
The WHERE clause filters which rows your query returns. Without it, you get everything. With it, you get exactly what you need.
Basic WHERE syntax:
SELECT columns
FROM table
WHERE condition;
Comparison operators:
SELECT * FROM products WHERE price = 999; -- equal
SELECT * FROM products WHERE price != 999; -- not equal (also: <>)
SELECT * FROM products WHERE price > 500; -- greater than
SELECT * FROM products WHERE price >= 500; -- greater than or equal
SELECT * FROM products WHERE price < 200; -- less than
SELECT * FROM products WHERE price <= 200; -- less than or equal
BETWEEN — inclusive range:
-- Rows where price is between 100 and 500 (includes 100 and 500):
SELECT name, price
FROM products
WHERE price BETWEEN 100 AND 500;
-- Date range:
SELECT * FROM orders
WHERE order_date BETWEEN '2024-01-01' AND '2024-03-31';
IN — match a list of values:
-- Instead of:
WHERE city = 'Mumbai' OR city = 'Delhi' OR city = 'Bangalore'
-- Use IN:
SELECT * FROM customers
WHERE city IN ('Mumbai', 'Delhi', 'Bangalore');
-- Exclusion with NOT IN:
SELECT * FROM products
WHERE category NOT IN ('Electronics', 'Clothing');
Pro Tip:
BETWEENis inclusive —BETWEEN 100 AND 500includes both 100 and 500. Be careful with dates:BETWEEN '2024-01-01' AND '2024-01-31'misses rows at'2024-01-31 23:59:59'. For datetime columns, use< '2024-02-01'instead.
EXERCISE
2WHERE with AND, OR & NOT
Combine multiple conditions in your WHERE clause using AND, OR, and NOT — and use parentheses to control the order of evaluation.
AND — all conditions must be true:
-- Customer must be in Mumbai AND have spent over 5000:
SELECT name, city, total_spent
FROM customers
WHERE city = 'Mumbai'
AND total_spent > 5000;
OR — at least one condition must be true:
-- Customers in Mumbai OR Delhi:
SELECT name, city
FROM customers
WHERE city = 'Mumbai'
OR city = 'Delhi';
NOT — inverts a condition:
-- Customers NOT in the 'inactive' status:
SELECT * FROM customers
WHERE NOT status = 'inactive';
-- Equivalent shorter form:
SELECT * FROM customers
WHERE status != 'inactive';
Parentheses — controlling evaluation order:
-- BUG: AND evaluates before OR (reads as: Mumbai, OR (Delhi AND premium))
SELECT * FROM customers
WHERE city = 'Mumbai'
OR city = 'Delhi'
AND is_premium = 1;
-- CORRECT: parentheses force the OR to evaluate first:
SELECT * FROM customers
WHERE (city = 'Mumbai' OR city = 'Delhi')
AND is_premium = 1;
Real-world example:
-- Active products in Electronics or Clothing with price under 1000:
SELECT name, category, price
FROM products
WHERE is_active = 1
AND (category = 'Electronics' OR category = 'Clothing')
AND price < 1000;
Pro Tip: AND has higher precedence than OR — it evaluates first, just like multiplication before addition in maths. Always use parentheses when mixing AND and OR to make your intent crystal clear.
EXERCISE
3IS NULL & IS NOT NULL
NULL means "no value" — and it behaves differently from everything else in SQL. You cannot compare it with = or !=. You must use IS NULL or IS NOT NULL.
What NULL means:
NULL is not zero, not an empty string, not false. It means the value is unknown or missing. This distinction matters because SQL handles NULL specially.
The trap — comparing NULL with =:
-- This will NEVER return rows, even if phone IS NULL:
SELECT * FROM customers WHERE phone = NULL; -- WRONG
-- This also will never work:
SELECT * FROM customers WHERE phone != NULL; -- ALSO WRONG
-- NULL is not equal to anything — not even another NULL
SELECT NULL = NULL; -- Returns NULL, not 1
The correct way — IS NULL and IS NOT NULL:
-- Find customers with no phone number recorded:
SELECT name, email
FROM customers
WHERE phone IS NULL;
-- Find customers who have provided a phone number:
SELECT name, phone
FROM customers
WHERE phone IS NOT NULL;
Practical example — finding incomplete records:
-- Orders where shipping address is missing:
SELECT id, customer_id, total
FROM orders
WHERE shipping_address IS NULL
AND status != 'cancelled';
IFNULL — substituting a fallback for NULL:
-- Show 'Not provided' instead of NULL for missing phones:
SELECT name, IFNULL(phone, 'Not provided') AS phone
FROM customers;
Pro Tip: Any arithmetic with NULL returns NULL.
NULL + 100 = NULL.NULL * 0 = NULL. This is why checking for NULLs before doing calculations is important.
