Filtering rows with WHERE
Most interview questions start the same way: "give me these columns from the rows that match". This lesson shows how a query does that, step by step.
What you will be able to do:
- Say the order in which
FROM,WHEREandSELECTrun. - Combine two tests with
ANDorORand predict which rows stay. - Solve LeetCode 1757 alone.
Four words you need
name.is_remote = 'N'.FROM, WHERE or SELECT.You write it in one order. It runs in another.
You write SELECT first. But the database runs FROM first. Think of the query as a pipeline: rows go in on the left, and each step makes the result smaller.
WRITTEN ORDER RUN ORDER
SELECT columns 1. FROM take the table (all rows)
FROM table 2. WHERE keep rows where the condition is TRUE
WHERE condition 3. SELECT keep only the columns you named
Click through the steps for this query. It uses an example table, not the exercise table.
SELECT name
FROM Employees
WHERE is_remote = 'N'
AND is_manager = 'Y';
FROM runs first. It takes every row and every column of Employees.
WHERE tests each row, one at a time. With AND, a row stays only if both tests are TRUE. Bo is remote, Caio is not a manager, and Eli fails both tests. All three are removed.
SELECT runs last. It keeps only the name column from the rows that are left.
Because WHERE runs before SELECT, a name you create in SELECT (an alias, like AS x) does not exist yet inside WHERE. You will need this fact in Sprint 2.
AND vs OR
A AND B keep the row only if BOTH are TRUE
A OR B keep the row if AT LEAST ONE is TRUE
Two MySQL details that often cost points in interviews:
- Text values go in single quotes:
'Y'. Without quotes, MySQL readsYas a column name and returns an error. - To compare values, use one
=. MySQL has no==.
Practice from memory
Try not to scroll up. Remembering on your own is what makes it stay.
1. Which clause runs first?
2. In the example query, what happens to Bo (remote = Y, manager = Y)?
3. What does WHERE remove?
4. A row passes test A but fails test B. The query uses WHERE A OR B. What happens?
5. You write WHERE is_manager = Y with no quotes. What does MySQL do?
Fill in the blanks: a different table
Orders(id, is_paid ENUM('Y','N'), is_shipped ENUM('Y','N')). Return the id of orders that are paid but not shipped yet.
SELECT
FROM
WHERE is_paid =
is_shipped = ;
🏆 Challenge: LeetCode 1757 (Recyclable and Low Fat Products)
Your task: open 1757, choose MySQL, and write the query yourself. The table is Products(product_id, low_fats ENUM('Y','N'), recyclable ENUM('Y','N')). Return the product_id of products that are both low fat and recyclable. Time box: 5 minutes.
Send me in the chat:
- Your query.
- The result: Accepted, or the error or wrong output you saw.
- One sentence: which clause filters the rows, and does it run before or after
SELECT?
To pass (check these yourself first):
- LeetCode says Accepted.
- You chose
ANDorORcorrectly, and you can say why. - Text values are in single quotes.
- Your sentence says that WHERE removes rows and runs before SELECT.
Read next
Main source: "SQL queries don't start with SELECT" by Julia Evans. It is short, and it shows the full run order you will use in every sprint.
- SQL queries don't start with SELECT 📄 Article · 5 min read · Julia Evans. Look at the diagram of the full order. Sprint 2 adds the GROUP BY and HAVING steps.
- SQLBolt Lesson 2: Queries with constraints 🛠 Interactive · 10 min · Run WHERE with AND/OR in the browser, with instant feedback.
💬 Is something still unclear? Ask me in the chat. Any question is fine, even "what does this word mean?" I'm your tutor for this course.