Filtering rows with WHERE
SQL · 17 questions
Short notes
Key idea
WHERE keeps only the rows that make a condition true. It runs before grouping and before SELECT, row by row.
Syntax
SELECT * FROM orders
WHERE status = 'shipped'
AND amount >= 500
AND city IN ('Chennai', 'Madurai')
AND order_date BETWEEN '2024-01-01' AND '2024-03-31'
AND customer_name LIKE 'S%';
<>or!=for "not equal".LIKE:%is any run of characters,_is exactly one.IS NULL/IS NOT NULLfor missing values.
Example
Orders over ₹1,000 that are not cancelled:
SELECT order_id, amount FROM orders
WHERE amount > 1000 AND status <> 'cancelled';
Traps
= NULLnever matches. UseIS NULL.ANDbinds tighter thanOR.a OR b AND cmeansa OR (b AND c); add brackets.BETWEENincludes both ends. With timestamps,BETWEEN '2024-01-01' AND '2024-01-31'misses most of 31 January.NOT INwith a NULL in the list returns no rows.
Want the full lesson with step-by-step examples, a project and a certificate? SQL course, ₹120 →
- Question 1easy
Which condition finds customers whose city is missing?
- Question 2easy
Which condition finds customers in Chennai or Coimbatore?
- Question 3medium
How many rows does this query return?
SELECT product_name, category, price FROM products WHERE product_name LIKE 'S%' OR category = 'Books' AND price > 600;
- Question 4hard
The
orderstable has 80 orders: 48 paid by UPI, 18 by Card, 9 by COD, 3 by NetBanking, and 2 with no payment method recorded. What does this query return?SELECT COUNT(*) FROM orders WHERE payment_method NOT IN ('UPI', 'Card'); - Question 5medium
The
productstable has 20 products. 4 are in the Books category, and 2 have no category. What does this query return?SELECT COUNT(*) FROM products WHERE category <> 'Books';
Filtering rows with WHERE
Filtering rows with WHERE
Filtering rows with WHERE
Filtering rows with WHERE
Filtering rows with WHERE
12 more questions on this topic, with hints, and tests
Sign up free for 21 days of every question and test.