Lecture

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:

Basic WHERE Syntax
SELECT column1, column2 FROM table_name WHERE condition;
  • SELECT specifies the columns to display
  • FROM selects the table to query
  • WHERE filters for rows that match a given 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:

Filter students with score > 90
SELECT name, exam_score FROM final_exam WHERE exam_score > 90;

This would return:

nameexam_score
Emily Davis95
Ethan Brown93

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 It 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!

Filter students who passed the final exam
SELECT name, passed FROM final_exam WHERE passed = 'Yes';

This query returns the students who passed the final exam:

namepassed
Emily DavisYes
Ethan BrownYes
Quiz
0 / 1

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

Run
Generate

Tables

Execution Result