runtime
03 / 88

lesson 03 Filtering & sorting · beginner

Filter with WHERE

Keep only rows that meet a condition.

the idea

WHERE keeps rows for which a condition is true. Text values need single quotes; column names do not. Rows whose comparison evaluates to NULL are not kept.

tables in this lesson

The challenges use this data. Each column shows its type underneath; → marks a link to another table.

customers4 columns · 8 rows
idinteger · keynametextcitytextjoined_attext
1AliceChicago2024-01-10
2BenAustin2024-02-12
3CaraSeattle2024-03-05
4DevChicago2024-04-20
5ElenaNULL2024-05-01

showing 5 of 8 rows

example

The idea in one query. Press Run to see what it returns; running examples never affects your progress.

example.sql
SELECT name, city
FROM customers
WHERE city = 'Chicago';

Watch for: SQL uses = for equality. City names are string values, so quote them.

build it step by step

Each step changes one thing. Run them in order and watch the result change.

  1. Start with customers. Chicago and Austin are values, not column names.

    step-1.sql
    SELECT name,city
    FROM customers;
  2. Keep matching rows. WHERE keeps true comparisons. Missing city values do not match.

    step-2.sql
    SELECT name,city
    FROM customers
    WHERE city='Chicago';

challenge 1

Show name and city for customers in Chicago.

Return namecityany row order

challenge-1.sql
 
To master this lesson, solve both challenges without hints and pass the concept check. 3 of 3 to go.