WHERE: Filter Rows
Keep only the rows that match a condition — with numbers, text and comparison operators.
A table can have millions of rows, but you rarely want them all. "Who scored over 500?", "Show only Indian players" — these questions need a filter. The WHERE clause is SQL's filter for rows.
Definition
The WHERE clause filters rows before they reach the result. It tests a condition (a true/false expression) against every row and keeps only the rows where the condition is TRUE. Rows where it is FALSE or UNKNOWN (NULL) are discarded.
Syntax
sql
SELECT column1, column2
FROM table_name
WHERE condition;| Operator | Meaning | Example |
|---|---|---|
= | equal to | country = 'India' |
<> or != | not equal to | team <> 'MI' |
> / < | greater / less than | runs > 500 |
>= / <= | greater / less than or equal | runs >= 700 |
AND / OR | combine conditions | country = 'India' AND runs > 300 |
(Chapter 5 covers AND, OR, NOT, IN, BETWEEN, LIKE and IS NULL in depth.)
Example — numeric filter
sql
SELECT *
FROM players
WHERE runs > 500;| id | name | team | country | runs |
|---|---|---|---|---|
| 1 | Virat Kohli | RCB | India | 750 |
| 3 | Jos Buttler | RR | England | 850 |
| 5 | David Warner | DC | Australia | 700 |
Execution order: 1️⃣ FROM players → 2️⃣ WHERE runs > 500 checks each row (Rohit 500 ✗ — 500 is not greater than 500, Dhoni 350 ✗, Bumrah 20 ✗) → 3️⃣ SELECT *.
Example — text filter
sql
SELECT name, team
FROM players
WHERE country = 'India';| name | team |
|---|---|
| Virat Kohli | RCB |
| Rohit Sharma | MI |
| MS Dhoni | CSK |
| Jasprit Bumrah | MI |
Execution order: 1️⃣ FROM → 2️⃣ WHERE country = 'India' → 3️⃣ SELECT name, team. Notice that country is used in WHERE even though it is not in the SELECT list — that is perfectly fine, because WHERE runs before SELECT and can see every column.
Example — not equal
sql
SELECT *
FROM players
WHERE team <> 'MI';| id | name | team | country | runs |
|---|---|---|---|---|
| 1 | Virat Kohli | RCB | India | 750 |
| 3 | Jos Buttler | RR | England | 850 |
| 4 | MS Dhoni | CSK | India | 350 |
| 5 | David Warner | DC | Australia | 700 |
Common mistakes
- ❌
WHERE country = India→ SQL thinks India is a column. ✅WHERE country = 'India'. - ❌
WHERE runs = '750'— comparing a number to text forces a conversion. ✅WHERE runs = 750. - ❌ Using a SELECT alias in WHERE:
SELECT runs AS r … WHERE r > 500→ Invalid column name 'r' in SQL Server, because WHERE runs before SELECT creates the alias. ✅ Repeat the column:WHERE runs > 500. - ❌ Expecting
> 500to include 500. ✅ Use>= 500to include it.
SQL Server vs MySQL / PostgreSQL
| SQL Server (default) | MySQL (default) | PostgreSQL | |
|---|---|---|---|
'india' = 'India'? | True (case-insensitive collation) | True | False (case-sensitive) |
Note: The browser playground compares text case-sensitively, so type values exactly as stored (
'India', not'india').
Interview tip
Q: Can you use a column in WHERE that is not in the SELECT list? A: Yes. WHERE is evaluated before SELECT, on the full rows from FROM, so it can use any column of the table. The reverse is also why a SELECT alias can't be used in WHERE.
Key points to remember
- WHERE filters ROWS: it keeps only rows where the condition is TRUE.
- Execution order: FROM → WHERE → SELECT, so WHERE can use any table column, even ones not selected.
- Comparison operators: =, <> (or !=), >, <, >=, <=; combine conditions with AND / OR.
- Text values go inside single quotes; numbers are written without quotes.
- A SELECT alias cannot be used in WHERE in SQL Server — WHERE runs before SELECT creates it.
- > is strict: runs > 500 does NOT include 500; use >= to include it.
Try it yourself
Who scored over 500?
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Only Indian players
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Two conditions with AND
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: filter by team
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
