Filtering with WHERE
The WHERE clause in SQL is used to filter rows based on a condition.
Rather than returning all records in a table, WHERE lets you retrieve only the rows that meet your criteria.
Basic WHERE Syntax
Here's the structure of a SELECT query using WHERE:
SELECT column1, column2 FROM table_name WHERE condition;
SELECTspecifies the columns to displayFROMselects the table to queryWHEREfilters rows that meet the condition
Example: Filter Students by Exam Score
Let's say the teacher wants a list of students who scored above 90 on the final exam:
SELECT name, exam_score FROM final_exam WHERE exam_score > 90;
This would return:
| name | exam_score |
|---|---|
| Emily Davis | 95 |
| Ethan Brown | 93 |
Common Comparison Operators
You can use many operators in the WHERE clause:
=equal to!=or<>not equal to>greater than<less than>=greater than or equal to<=less than or equal to
You can also combine multiple conditions using AND, OR, and NOT.
Why filtering with WHERE matters
Filtering data helps you answer real questions, such as:
- Which students passed the exam?
- Who scored above 90?
- What records match specific criteria?
WHERE gives you control over your queries and helps you explore meaningful patterns in data.
Try it Yourself!
SELECT name, passed FROM final_exam WHERE passed = 'Yes';
This query returns the students who passed the final exam:
| name | passed |
|---|---|
| Emily Davis | Yes |
| Ethan Brown | Yes |
Which of the following SQL queries returns only students who passed the final exam?
SELECT name FROM final_exam WHERE passed = 'Yes';
SELECT * FROM final_exam;
SELECT name, passed FROM final_exam;
SELECT passed FROM final_exam WHERE name = 'Alex Kim';
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help
Code Editor
Tables
Execution Result