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.

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 SELECT knowledge
  • Familiarity with column names

Step-by-step instructions

Step 1: Add WHERE after the table reference

Place the condition after the FROM clause.

SELECT *
FROM users
WHERE user_id = 42;

This returns only the matching row.

Use comparison operators for ranges.

SELECT *
FROM orders
WHERE total > 100;

Combine conditions with AND or OR.

SELECT *
FROM orders
WHERE status = 'paid'
AND total > 100;

What to look for:

  • WHERE filters rows
  • Works with SELECT, UPDATE, and DELETE
  • Use exact or range conditions
  • Combine rules with AND and OR
  • Missing WHERE affects every row

Examples you can copy

One user

SELECT *
FROM users
WHERE email = 'alex@example.com';

Expired sessions

SELECT *
FROM sessions
WHERE expires_at < NOW();

Premium orders

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:

WHERE status = paid

Why it breaks: SQL treats paid as a column.

Corrected approach:

WHERE status = 'paid'

Mistake 2: Weak conditions in updates or deletes

What the reader might do:

DELETE FROM users
WHERE active = FALSE;

Why it breaks: this may delete too many users.

Corrected approach:

Add more precise filters.

WHERE active = FALSE
AND last_login < '2025-01-01'

Mistake 3: Mixing AND and OR without grouping

What the reader might do:

WHERE status = 'paid'
OR status = 'pending'
AND total > 100

Why it breaks: operator precedence may produce unexpected results.

Corrected approach:

Use parentheses.

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 WHERE to filter rows
  • Add it after FROM
  • Quote string values
  • Combine rules with AND and OR
  • Missing WHERE can affect every row