WHERE: Filtering Rows
WHERE keeps only the rows that match a condition.
Tables can hold millions of rows; you almost never want all of them. WHERE filters rows by a condition - only Delhi customers, only orders above ₹500, only this month. The database checks each row against the condition and returns the ones that match.
Example
SELECT name, price FROM products WHERE price < 500;
-- Result:
-- name | price
-- -------------------+------
-- Cotton T-shirt | 499
-- Steel Water Bottle | 349
-- Notebook Pack | 199▶ Try it yourselfComparison and logic
=,<>(not equal),<,>,<=,>=AND- both must be true;OR- either one;NOT- the oppositeIN ('a', 'b')- one of a list;BETWEEN 100 AND 500- in a range (both ends included)LIKE 'S%'- text pattern:%= any letters,_= one letter
Example
SELECT name, category, price
FROM products
WHERE category IN ('Fashion', 'Books')
AND price BETWEEN 300 AND 1500;
-- Result:
-- name | category | price
-- ---------------+----------+------
-- Cotton T-shirt | Fashion | 499
-- Denim Jeans | Fashion | 1299
-- Python Book | Books | 599▶ Try it yourselfExample
SELECT name FROM products WHERE name LIKE '%book%';
-- Result:
-- name
-- -------------
-- Notebook Pack
-- Python Book▶ Try it yourselfNULL - an empty value
NULL means "no value". You can't compare it with =; use IS NULL or IS NOT NULL.
Example
SELECT name, city FROM customers WHERE city IS NULL;
-- Result:
-- name | city
-- -----------+-----
-- Vikram Rao | NULL▶ Try it yourselfText values go in single quotes:
'Delhi'. Numbers don't need quotes.Key points
- WHERE keeps rows that match a condition.
- Combine with AND, OR, NOT; use IN, BETWEEN, LIKE.
- Use IS NULL, not = NULL.
📝 Test yourself
1. Which finds products whose name starts with S?
2. How do you find customers with no city?
3. Fill in the keyword for 'one of these values':
WHERE city ('Delhi', 'Pune')
