What is WHERE?
The WHERE clause lets you filter rows based on specific conditions. Instead of retrieving all rows from a table, you only get the ones that match your criteria.
Think of it like using a search filter in an online store: "Show me only products under $50 in the Electronics category." WHERE does the same thing for database queries.
Basic Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;💡 Pro Tip: WHERE comes after FROM and before ORDER BY or GROUP BY. The condition must evaluate to true or false for each row.
Common Patterns
Filter rows using comparison operators
SELECT name, age
FROM customers
WHERE age >= 18;Result: Returns only customers who are 18 or older
See It In Action
Watch how WHERE filters data step by step. Select an example to see the complete flow from input data to filtered results.
Filter by Age
Get customers who are 30 or older
Input Data
idINTEGER | nameTEXT | emailTEXT | countryTEXT | ageINTEGER |
|---|---|---|---|---|
| 1 | Alice Johnson | [email protected] | USA | 28 |
| 2 | Bob Smith | [email protected] | Canada | 35 |
| 3 | Carlos Silva | [email protected] | Brazil | 22 |
| 4 | Diana Chen | [email protected] | USA | 41 |
| 5 | Emma Davis | [email protected] | Canada | 19 |
SQL Query
SELECT name, age, country
FROM customers
WHERE age >= 30Result
2 rowsnameTEXT | ageNUMBER | countryTEXT |
|---|---|---|
| Bob Smith | 35 | Canada |
| Diana Chen | 41 | USA |
Comparison Operators
=Equal To
age = 25Exact match. Use single quotes for text: country = 'USA'
!=Not Equal To
status != 'inactive'Excludes rows with the specified value. Can also use <>
> <Greater/Less Than
price > 100Compare numeric values. Also includes >= and <=
BETWEENRange
age BETWEEN 18 AND 65Inclusive range check. Easier to read than age >= 18 AND age <= 65
INList Match
country IN ('USA', 'Canada')Matches any value in the list. Cleaner than multiple OR conditions.
LIKEPattern Match
name LIKE 'John%'Use % for any characters, _ for single character.
Combining Conditions
AND - Both Must Be True
WHERE age >= 18 AND country = 'USA'Narrows results. Only rows where ALL conditions are true are returned.
OR - At Least One Must Be True
WHERE country = 'USA' OR country = 'Canada'Broadens results. Rows matching ANY condition are returned.
NOT - Negates a Condition
WHERE NOT country = 'USA'Reverses the condition. Can also use != or NOT IN
Parentheses - Control Order
WHERE (country = 'USA' OR country = 'Canada') AND age >= 18Group conditions to control evaluation order. Critical for complex filters!
Common Mistakes to Avoid
❌ Forgetting quotes for text values
WHERE country = USAText values must be in single quotes: country = 'USA'
❌ Using AND when you mean OR
WHERE country = 'USA' AND country = 'Canada'A row can't be both! Use OR: country = 'USA' OR country = 'Canada'
❌ Incorrect LIKE wildcards
WHERE email LIKE 'gmail.com'Use % wildcard: email LIKE '%gmail.com'
❌ Missing parentheses in complex conditions
WHERE age > 18 AND country = 'USA' OR country = 'Canada'Ambiguous! Use parentheses: (country = 'USA' OR country = 'Canada') AND age > 18
✅ Correct syntax with proper grouping
WHERE (country IN ('USA', 'Canada')) AND age BETWEEN 18 AND 65Clear, readable, and correct!