SQL WHERE Clause
What is the WHERE Clause?
The WHERE clause filters rows returned by a SELECT statement. Only rows that satisfy the specified condition appear in the result set.
Without WHERE, a query returns every row in the table. Adding WHERE lets you narrow the results to exactly the data you need.
Syntax
SELECT column1, column2
FROM table_name
WHERE condition;Conditions use comparison operators such as =, !=, <, >, <=, and >=. You can combine multiple conditions with AND, OR, and NOT.
When to Use
- Fetching a specific record by its ID
- Filtering products above a certain price
- Finding students in a particular grade
- Limiting results to a date range
Key Points
- Evaluated Per Row — The database checks the
WHEREcondition against every row and only includes rows where the condition is true. - NULL Handling — Comparisons with
NULLusing=return unknown, not true or false. UseIS NULLinstead. - Index Usage — Filtering on indexed columns makes queries faster because the database can skip irrelevant rows.
- Execution Order —
WHEREis processed beforeSELECT, so you cannot reference column aliases defined in theSELECTlist. - String Comparisons — Wrap string literals in single quotes:
WHERE first_name = 'Alice'.
The WHERE clause is essential for every real-world query. Combine it with other clauses like ORDER BY and LIMIT to build powerful, targeted queries.
Guided Practice
Solve the challenge below. Use hints when stuck and check your answer for instant feedback.
SQL WHERE Clause Challenge
Write a query that find all students who are 20 years old.
Expected result
Only rows where the age column equals 20.
Hidden checks
- Returned rows and values
- Output columns and result shape
- Final database state after the query runs
Lesson guidance
What is the WHERE Clause?
More Examples
Filter with comparison operator
Find orders where the total is greater than 100.
Combine conditions with AND
Find students older than 18 in grade A.