Lesson 1 · Milestone: Filter

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:

Four words you need

rowOne record in a table. Example: one employee.
columnOne field that every row has. Example: name.
conditionA test on a row that is TRUE or FALSE. Example: is_remote = 'N'.
clauseOne part of a query, such as 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';
1 · FROM Employees id name  remote manager 1  Ana   N      Y 2  Bo    Y      Y 3  Caio  N      N 4  Dee   N      Y 5  Eli   Y      N all 5 rows, all columns 2 · WHERE … id name  remote manager 1  Ana   N      Y 4  Dee   N      Y rows 2, 3, 5 removed: each one fails at least one test WHERE removes ROWS 3 · SELECT name name Ana Dee SELECT removes COLUMNS

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:

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:

To pass (check these yourself first):

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.

💬 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.