WHERE Clause

Filter rows — get only the data you actually need

Beginner 14 minWHEREfilteringconditions

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:

Condition-based filtering — WHERE evaluates a condition for every row in the table. If the condition is true, the row is included. If it's false or NULL, the row is excluded from the results entirely.
Runs before SELECT — WHERE executes before column selection, so you can filter on columns you don't display. WHERE price > 50 works even when price isn't in your SELECT list at all.
Comparison operators — Use =, != (or <>), <, >, <=, >= to build conditions. These work with numbers, text, and dates depending on the column type.
Text needs single quotes — 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.

Knowledge Canvas

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
  • Most databases reject SELECT aliases in WHERE (SQLite allows them), so repeat 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

WHERE: filters individual rows
HAVING: filters groups after aggregation
Runs before GROUP BY
Runs after GROUP BY
Cannot use aggregate functions
Designed for aggregate conditions
WHERE price > 100
HAVING COUNT(*) > 5

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

Syntax

Syntax Template
1SELECT
2 columns
3FROM table
4WHERE condition;
5
6-- Comparison operators
7-- WHERE price > 50
8-- WHERE status = 'delivered'
9-- WHERE stock != 0
10-- WHERE joined_date >= '2024-01-01'
=Equals (use single quotes for text values)
!= or <>Not equal
< > <= >=Less/greater than, with or without equals
Single quotesRequired for text: WHERE city = 'Mumbai' (not double quotes)

Sample Data

products
10 rows
idnamecategorypricestock
P101Wireless MouseElectronics24.99150
P102Office DeskFurniture18925
P103LED Desk LampFurniture45.580
P104USB-C CableElectronics12.99500
P105Bluetooth SpeakerElectronics7960
P106Office ChairFurniture9515
P107Coffee MakerAppliances89.9940
P108Notebook SetStationery8.5200
P109Wireless HeadphonesElectronics15930
P110Standing DeskFurniture4258

Worked Example

Find all products that cost more than 50.

SQL
1SELECT
2 name,
3 price
4FROM products
5WHERE price > 50;
The database scans every row in the products table and checks if price > 50. Six products pass the filter: Office Desk (189), Bluetooth Speaker (79), Office Chair (95), Coffee Maker (89.99), Wireless Headphones (159), and Standing Desk (425). Products priced at 50 or below — Wireless Mouse (24.99), LED Desk Lamp (45.50), USB-C Cable (12.99), and Notebook Set (8.50) — are excluded. With no ORDER BY, SQL doesn't guarantee any row order — here SQLite happens to return them in the order they were inserted, not sorted by price.
Output
6 rows
nameprice
Office Desk189
Bluetooth Speaker79
Office Chair95
Coffee Maker89.99
Wireless Headphones159
Standing Desk425

Common Mistakes

❌

Forgetting quotes around text values

SQL
1SELECT
2 *
3FROM products
4WHERE category = Electronics;

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

SQL
1SELECT
2 *
3FROM reviews
4WHERE comment = NULL;

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

1WHERE runs before SELECT — filter on columns you don't display
2Single quotes for text: WHERE city = 'Mumbai' (not double quotes)
3NULL comparisons need IS NULL — not = or !=
4Without WHERE, every query returns the entire table

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.

Challenge

Solve the problem below

Find all products in the 'Furniture' category that cost less than 100. Show the name and price.

Your Query