Filtering Rows with WHERE
WHERE keeps the rows for which a condition is true.
SELECT name, category, price
FROM products
WHERE price > 50;
Comparison operators
| Operator | Meaning |
|---|---|
= |
equal to |
<> or != |
not equal to |
< > <= >= |
less than, greater than, and so on |
Text values go in single quotes:
SELECT *
FROM orders
WHERE status = 'delivered';
AND, OR and NOT
AND requires every condition. OR requires at least one. AND is evaluated before OR, so use brackets to make the logic explicit.
SELECT name, category, price
FROM products
WHERE category = 'Accessories'
AND (price < 30 OR price > 50);
Run it, then remove the brackets and run it again. The result changes.
IN
IN matches any value in a list. It is shorter than a chain of OR conditions.
SELECT name, city
FROM customers
WHERE city IN ('Pune', 'London');
BETWEEN
BETWEEN includes both ends of the range.
SELECT order_id, order_date
FROM orders
WHERE order_date BETWEEN '2025-04-01' AND '2025-04-30';
Dates written as YYYY-MM-DD sort and compare correctly as text, which is why this format is used everywhere in data work.
LIKE
LIKE matches a pattern. % stands for any number of characters and _ for exactly one.
SELECT name
FROM products
WHERE name LIKE '%o%';
NULL means unknown
NULL marks a missing value. It is not zero and not an empty string. Because it is unknown, comparing anything with NULL using = never returns true.
-- this returns no rows
SELECT name FROM customers WHERE city = NULL;
Use IS NULL and IS NOT NULL:
SELECT name, city
FROM customers
WHERE city IS NULL;
A common mistake: WHERE city <> 'Pune' does not return customers whose city is NULL, because the comparison is unknown for them. Add OR city IS NULL if you want them included.
Practice
Find the orders placed in May 2025 that are not cancelled.