Find the Rows That Matter
Filter data using WHERE conditions.
You’ve learned to pick columns. Now it’s time to filter rows, so you only see the data you care about.
Goal
You’ll aim to:
Use
WHERE
to filter data.Add simple conditions to your query.
Tables
Let’s use the following table in this lesson:
The people
table
ID | Name | Age | City |
1 | Aisha | 30 | Karachi |
2 | Dan | 24 | New York |
3 | Fatima | 27 | Lahore |
4 | Lee | 22 | Seoul |
Basic WHERE
clause
Let’s say you only want people from Karachi. We used where
to add a condition to your query.
SELECT * FROM people WHERE city = 'Karachi';
Only shows rows where the city is 'Karachi'
.
Nice! You just filtered your results to only show rows where the city is 'Karachi'
.
Numeric filtering
You can also filter based on numbers using comparison operators. Let’s find people older than 25 and show just their names and ages:
SELECT name, age FROM people WHERE age > 25;
Use comparison operators with numbers.
Great! You just learned a condition with a numeric comparison.
Using WHERE
operators
=
: Equals>
: Greater than<
: Less than>=
and<=
: Greater than or equal to/Less than or equal to!=
: Not equal
Mini challenge
Show all pets that are younger than four years old:
The pets
table
ID | Name | Type | Age |
1 | Coco | dog | 5 |
2 | Luna | cat | 3 |
3 | Goldie | fish | 1 |
Use the above table to write your query:
# Write your query here:
Should show Luna (3) and Goldie (1).
If you’re stuck, click the “Show Solution” button.
Great! You just combined filtering with comparison.
Tips
Strings go in single quotes.
Numbers don’t need quotes.
The
WHERE
clause works with any column.
What’s next?
Let’s get smarter with multiple conditions using AND
, OR
, and NOT
!