SQL from Scratch · Day 10 of 60 ·

WHERE: asking for specific rows

So far, every query has returned every row. Real questions are narrower: which customers live in Lisbon? WHERE is how you ask.

SELECT name, city
FROM customers
WHERE city = 'Lisbon';

The database checks each row against the condition and keeps only the rows where it’s true. It’s like a filter on a spreadsheet, written in a sentence.

Three details trip up beginners:

Text goes in single quotes: ‘Lisbon’. Numbers don’t need quotes: WHERE price = 500.

Whether ‘lisbon’ matches ‘Lisbon’ depends on your database and its settings, so match the stored spelling until you know.

Order matters in the query. FROM comes before WHERE, and WHERE comes before ORDER BY:

SELECT name, price
FROM products
WHERE category = 'Bags'
ORDER BY price;

If no rows match, you get an empty result, not an error.

Go deeper: PostgreSQL on WHERE and table expressions

What's the first filter you'd run on your own data?

Join the conversation on LinkedIn