WHERE & Building Conditions
Understand exactly what WHERE does, how every condition is built (expression + operator + expression), and the five families of WHERE operators.
A table can hold millions of rows, but you rarely want all of them. A selector wants only Indian players, a fantasy-cricket app wants players with more than 500 runs, a fan wants everyone from MI or CSK. The WHERE clause is how you tell SQL which rows to keep. This chapter goes through every WHERE operator one by one.
Definition
The WHERE clause filters the rows of a table. It evaluates a condition (a search condition / predicate) for every row and keeps only the rows for which the condition is TRUE. Rows where the condition is FALSE or UNKNOWN are removed from the result.
Let us unpack that:
- Filters rows — WHERE works on rows, not columns. Choosing columns is the job of
SELECT. - For every row — SQL checks the condition separately for each row, one at a time.
- Condition / predicate — an expression whose answer is TRUE, FALSE or UNKNOWN (UNKNOWN happens when a
NULLis involved — covered in the IS NULL topic). - Only TRUE survives — "not sure" (UNKNOWN) is treated the same as "no". This one rule explains many NULL surprises later.
Syntax
sql
SELECT column_list
FROM table_name
WHERE condition;| Part | Meaning |
|---|---|
SELECT column_list | which columns to show (* = all) |
FROM table_name | which table to read rows from |
WHERE condition | which rows to keep — written after FROM |
; | ends the statement |
How a condition is built
Every simple condition has the same shape — compare two things:
Condition = Expression + Operator + Expression
An expression is anything that produces a value. The left side is usually a column, but it can be any of these:
| Left expression type | Example condition | Meaning |
|---|---|---|
| Column = Value | country = 'India' | compare a column with a fixed value |
| Function = Value | UPPER(team) = 'MI' | apply a function first, then compare |
| Expression = Value | runs / 2 = 250 | do arithmetic first, then compare |
| Subquery = Value (advanced) | runs = (SELECT MAX(runs) FROM players) | compare with a value calculated by another query |
Text values go inside single quotes ('India'); numbers do not (500).
The five operator families
| Family | Operators | Job |
|---|---|---|
| Comparison | = <> != > >= < <= | compare two values |
| Logical | AND OR NOT | combine or reverse conditions |
| Range | BETWEEN | value inside a range (both ends included) |
| Membership | IN NOT IN | value present in a list |
| Search | LIKE | text matches a pattern |
Plus IS NULL / IS NOT NULL for checking missing values.
Example — only Indian players
Our players table has 6 rows. Let us keep only the Indian ones.
sql
SELECT *
FROM players
WHERE country = 'India';| id | name | team | country | runs |
|---|---|---|---|---|
| 1 | Virat Kohli | RCB | India | 750 |
| 2 | Rohit Sharma | MI | India | 500 |
| 4 | MS Dhoni | CSK | India | 350 |
| 6 | Jasprit Bumrah | MI | India | 20 |
How it works (execution order):
- FROM players — SQL loads all 6 rows.
- WHERE country = 'India' — checks each row; Jos Buttler (England) and David Warner (Australia) give FALSE and are dropped.
- SELECT * — shows all columns of the 4 surviving rows.
Note: You write
SELECTfirst, but SQL runsFROM→WHERE→SELECT. WHERE filters rows before grouping (GROUP BY) and beforeSELECTbuilds the output.
Example — a function and a subquery in the condition
sql
SELECT name, runs
FROM players
WHERE runs = (SELECT MAX(runs) FROM players);| name | runs |
|---|---|
| Jos Buttler | 850 |
The subquery runs first and returns 850; the condition then becomes runs = 850.
Common mistakes
- ❌
WHERE country = India→ SQL thinksIndiais a column name. ✅WHERE country = 'India' - ❌
WHERE country = "India"→ in SQL Server double quotes mean an identifier (a name), not text. ✅ use single quotes. - ❌ Putting WHERE before FROM:
SELECT * WHERE runs > 500 FROM players. ✅FROMthenWHERE. - ❌ Using a SELECT alias in WHERE:
SELECT runs / 2 AS half ... WHERE half > 100fails, because WHERE runs before SELECT creates the alias. ✅ repeat the expression:WHERE runs / 2 > 100. - ❌ Using aggregates in WHERE:
WHERE SUM(runs) > 1000. ✅ that belongs inHAVING(after GROUP BY).
Interview tip
Q: What is the difference between WHERE and HAVING?
A: WHERE filters individual rows before grouping and cannot use aggregate functions. HAVING filters groups after GROUP BY and can use aggregates like SUM() or COUNT().
Key points to remember
- WHERE filters rows: it keeps only the rows for which the condition is TRUE.
- Rows where the condition is FALSE or UNKNOWN (because of NULL) are removed.
- Every simple condition = expression + operator + expression, e.g. country = 'India'.
- The left expression can be a column, a function, arithmetic or even a subquery.
- Five operator families: Comparison, Logical, Range (BETWEEN), Membership (IN), Search (LIKE).
- Execution order: FROM → WHERE → SELECT, so WHERE cannot use SELECT aliases.
- Text values need single quotes; numbers do not.
Try it yourself
Only Indian players
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Function and arithmetic in a condition
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Subquery = Value (advanced)
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.
