How to Use WHERE in SQL
Use WHERE when your SQL query should target only the rows that match specific conditions. This is essential for safe filtering in SELECT, UPDATE, and DELETE queries.
What you’ll build or solve
You’ll learn how to use WHERE in SQL with exact matches, comparison operators, and multiple conditions. You’ll also know how to avoid overly broad queries.
Learn SQL on Mimo
When this approach works best
This approach is the right choice when not every row in the table should be affected.
Common real-world scenarios include:
- One customer lookup
- Paid orders only
- Expired sessions
- Updating one product
- Deleting test data
This is a bad idea when you intentionally need every row. In that case, omit WHERE carefully.
Prerequisites
You only need:
- A SQL table with rows
- Basic
SELECTknowledge - Familiarity with column names
Step-by-step instructions
Step 1: Add WHERE after the table reference
Place the condition after the FROM clause.
SQL
SELECT *
FROM users
WHERE user_id = 42;
This returns only the matching row.
Use comparison operators for ranges.
SQL
SELECT *
FROM orders
WHERE total > 100;
Combine conditions with AND or OR.
SQL
SELECT *
FROM orders
WHERE status = 'paid'
AND total > 100;
What to look for:
WHEREfilters rows- Works with
SELECT,UPDATE, andDELETE - Use exact or range conditions
- Combine rules with
ANDandOR - Missing
WHEREaffects every row
Examples you can copy
One user
SQL
SELECT *
FROM users
WHERE email = 'alex@example.com';
Expired sessions
SQL
SELECT *
FROM sessions
WHERE expires_at < NOW();
Premium orders
SQL
SELECT *
FROM orders
WHERE plan = 'pro'
AND status = 'paid';
Common mistakes and how to fix them
Mistake 1: Forgetting quotes around strings
What the reader might do:
SQL
WHERE status = paid
Why it breaks: SQL treats paid as a column.
Corrected approach:
SQL
WHERE status = 'paid'
Mistake 2: Weak conditions in updates or deletes
What the reader might do:
SQL
DELETE FROM users
WHERE active = FALSE;
Why it breaks: this may delete too many users.
Corrected approach:
Add more precise filters.
SQL
WHERE active = FALSE
AND last_login < '2025-01-01'
Mistake 3: Mixing AND and OR without grouping
What the reader might do:
SQL
WHERE status = 'paid'
OR status = 'pending'
AND total > 100
Why it breaks: operator precedence may produce unexpected results.
Corrected approach:
Use parentheses.
SQL
WHERE (status = 'paid' OR status = 'pending')
AND total > 100
Troubleshooting
If too many rows match, tighten the condition.
If no rows return, verify the exact stored value.
If mixed logic behaves oddly, add parentheses.
If the query is slow, index frequently filtered columns.
Quick recap
- Use
WHEREto filter rows - Add it after
FROM - Quote string values
- Combine rules with
ANDandOR - Missing
WHEREcan affect every row
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot