A
WHEREcondition uses SQL's three-valued logic:TRUE,FALSE, orUNKNOWN. Text comparison and case sensitivity depend on the database and collation.
1. Introduction
The WHERE clause is used to filter rows in SQL.
It allows you to decide which rows should remain in the final result and which rows should be removed.
Without WHERE, SQL returns all rows.
With WHERE, SQL returns only the rows that match your condition.
This is one of the most important SQL concepts because most real-world queries need filtering.
2. What is WHERE?
WHERE restricts rows by applying conditions.
Only rows that satisfy the condition are included in the final result.
Rows that do not satisfy the condition are ignored.
Simple Definition
WHERE decides:
Which rows are allowed to stay.
Basic Syntax
SELECT column_name
FROM table_name
WHERE condition;3. Basic Example
If you want to find countries in the Oceania region:
SELECT *
FROM countries
WHERE region = 'Oceania';Output
| country | population | area | region |
|---|---|---|---|
| Australia | 25700000 | 7692000 | Oceania |
Explanation
The WHERE clause checks every row.
It keeps only rows where:
region = 'Oceania'All other rows are removed.
4. Deep Dive — WHERE is the Gatekeeper
Think of WHERE as the gatekeeper of your data.
It stands between:
- the raw table
- the final result
Its job is to inspect every row and decide whether that row is allowed to pass.
For every row, SQL checks the condition.
The result is always one of two things:
TRUEFALSE
Gatekeeper Logic
If condition is TRUE
→ row is allowed into the result
If condition is FALSE
→ row is blocked and removed
Mental Model
Think of WHERE like a security guard checking IDs at the entrance.
- valid row → enter
- invalid row → rejected
Only approved rows make it to the output.
5. SQL Execution Order with WHERE
Even though we write:
SELECT ...
FROM ...
WHERE ...SQL does not process it in that order.
This is extremely important.
Actual Execution Order
SQL logically processes:
FROMWHERESELECT
Why This Matters
SQL first:
- finds the table
- filters rows
- selects columns
This means SQL filters rows before deciding what columns to show.
That is why you can filter using a column even if that column is not visible in the final result.
Example
SELECT product_name
FROM products
WHERE stock_quantity = 0;Even though stock_quantity is not shown in output, SQL still uses it during filtering.
Why?
Because filtering happens before selection.
6. Real-Life Example — products Table
We will use this products table:
| product_id | product_name | category | price | stock_quantity |
|---|---|---|---|---|
| 101 | Gaming Mouse | Accessories | 50 | 100 |
| 102 | Mechanical Keyboard | Accessories | 120 | 50 |
| 103 | Curved Monitor | Electronics | 300 | 20 |
| 104 | Ergonomic Chair | Furniture | 250 | 0 |
| 105 | USB-C Cable | Cables | 15 | 200 |
7. Case 1 — Filtering by Text
When filtering text values, always use single quotes.
Text values are strings.
Strings must be wrapped in ' '.
Query
SELECT product_name, category
FROM products
WHERE category = 'Accessories';Output
| product_name | category |
|---|---|
| Gaming Mouse | Accessories |
| Mechanical Keyboard | Accessories |
Explanation
SQL checks each row and keeps only rows where:
category = 'Accessories'Rule
Text values must always be wrapped in single quotes.
Correct:
WHERE category = 'Accessories'Incorrect:
WHERE category = AccessoriesWithout quotes, SQL thinks Accessories is a column name.
8. Case 2 — Filtering by Numbers
When filtering numeric values, do not use quotes.
Numbers should be written directly.
Query
SELECT product_name, stock_quantity
FROM products
WHERE stock_quantity = 20;Output
| product_name | stock_quantity |
|---|---|
| Curved Monitor | 20 |
Explanation
SQL checks each row and keeps only rows where:
stock_quantity = 20Rule
Numbers should not be wrapped in quotes.
Correct:
WHERE stock_quantity = 20Avoid:
WHERE stock_quantity = '20'Some databases may convert it automatically, but it is bad practice and may hurt performance.
9. Case 3 — Finding Items Out of Stock
This is a very common real-world business query.
Managers often need to find products that are out of stock.
Query
SELECT product_name, category
FROM products
WHERE stock_quantity = 0;Output
| product_name | category |
|---|---|
| Ergonomic Chair | Furniture |
Explanation
SQL checks every row and returns only rows where:
stock_quantity = 0This helps identify products that need restocking.
10. Common Mistakes to Avoid
1. Forgetting Quotes for Text
Incorrect:
WHERE category = AccessoriesCorrect:
WHERE category = 'Accessories'Text values must be wrapped in single quotes.
2. Using Quotes for Numbers
Avoid:
WHERE stock_quantity = '20'Prefer:
WHERE stock_quantity = 20Numbers should remain numeric.
3. Case Sensitivity
In many databases:
'Accessories'is different from:
'accessories'Always match the stored case exactly unless your database uses case-insensitive collation.
4. Using Aliases in WHERE
Incorrect:
SELECT price AS cost
FROM products
WHERE cost = 50;This fails.
Why?
Because SQL processes:
FROMWHERESELECT
At the time WHERE runs, cost does not exist yet.
Aliases are created later in SELECT.
Correct Version
SELECT price AS cost
FROM products
WHERE price = 50;11. Summary
WHERE
Filters rows based on conditions.
What WHERE Does
- checks each row
- evaluates condition
- keeps matching rows
- removes non-matching rows
SQL Execution Order
SQL processes:
FROMWHERESELECT
Important Rules
- text values need single quotes
- numbers do not need quotes
- aliases cannot be used in WHERE
- WHERE acts like a gatekeeper
Final Mental Model
When SQL sees:
SELECT ...
FROM ...
WHERE ...It thinks:
- Find the table
- Check every row
- Keep only valid rows
- Return requested columns