WHERE Clause
Filter rows — get only the data you actually need
WHERE: Filtering Rows
WHERE filters which rows appear in your results. Think of SELECT as choosing columns (vertical slice) and WHERE as choosing rows (horizontal slice). Together, they let you extract the exact rectangle of data you need. Here's how it works:
WHERE price > 50 works even when price isn't in your SELECT list at all.=, != (or <>), <, >, <=, >= to build conditions. These work with numbers, text, and dates depending on the column type.WHERE city = 'Mumbai' requires single quotes around text values. Numbers don't need any quotes. Using double quotes will cause an error in most databases.How WHERE Works
Filter rows before they reach your output
- WHERE evaluates each row and keeps only TRUE ones
- Runs AFTER FROM but BEFORE SELECT
- Multiple conditions combine with AND / OR
- Cannot use column aliases — use the original expression
Common Traps
WHERE pitfalls that waste hours
- = NULL never works — use IS NULL instead
- String = comparisons: PostgreSQL, Oracle, and SQLite are case-sensitive by default; MySQL and SQL Server are typically case-insensitive
- WHERE runs before GROUP BY — can't filter aggregates here
- Using OR without parentheses changes logic unexpectedly
WHERE vs HAVING
Different filters for different stages
Useful Patterns
Common WHERE techniques
- Range:
WHERE price BETWEEN 10 AND 50 - List:
WHERE country IN ('IN', 'US', 'UK') - Pattern:
WHERE email LIKE '%@gmail.com' - Null check:
WHERE phone IS NOT NULL
Performance Tips
Keep WHERE clauses fast
- Put the most selective condition first
- Avoid functions on indexed columns:
WHERE YEAR(date)is slow - Use = over LIKE when exact match is enough
- Index columns you frequently filter on
=Equals (use single quotes for text values)!= or <>Not equal< > <= >=Less/greater than, with or without equalsSingle quotesRequired for text: WHERE city = 'Mumbai' (not double quotes)| id | name | category | price | stock |
|---|---|---|---|---|
| P101 | Wireless Mouse | Electronics | 24.99 | 150 |
| P102 | Office Desk | Furniture | 189 | 25 |
| P103 | LED Desk Lamp | Furniture | 45.5 | 80 |
| P104 | USB-C Cable | Electronics | 12.99 | 500 |
| P105 | Bluetooth Speaker | Electronics | 79 | 60 |
| P106 | Office Chair | Furniture | 95 | 15 |
| P107 | Coffee Maker | Appliances | 89.99 | 40 |
| P108 | Notebook Set | Stationery | 8.5 | 200 |
| P109 | Wireless Headphones | Electronics | 159 | 30 |
| P110 | Standing Desk | Furniture | 425 | 8 |
Worked Example
Find all products that cost more than 50.
| name | price |
|---|---|
| Office Desk | 189 |
| Bluetooth Speaker | 79 |
| Office Chair | 95 |
| Coffee Maker | 89.99 |
| Wireless Headphones | 159 |
Forgetting quotes around text values
Without quotes, SQL thinks Electronics is a column name, not a text value. This causes a "no such column" error.
Always wrap text in single quotes: WHERE category = 'Electronics'
Using = for NULL comparisons
NULL is not a value — it means "unknown." You cannot compare it with =. This returns zero rows even if NULLs exist.
Use IS NULL or IS NOT NULL instead (covered in a later topic).
Key Concepts
Pro Tip
Without WHERE, every query returns the full table. WHERE is how you ask specific questions: "Which orders are pending?", "What products cost over 100?"
When to Use
Finding specific records, filtering by date range, showing only in-stock products, retrieving data for a single user ID.
Find all products in the 'Furniture' category that cost less than 100. Show the name and price.