Lesson 1.2

WHERE: keep the rows you want

Filter rows with conditions, lists, ranges and text patterns.

Imagine you're a data analyst at Uber. The trips table has one row per ride request.

1Keep rows that match

WHERE keeps only the rows where the condition is true. Put text in single quotes.

SELECT trip_id, city, fare
FROM trips
WHERE city = 'Seattle';

Practice

Find every trip paid in cash.

Return: trip id and status

Write your query here

2Compare numbers

Use >, <, >= and <= to compare. Use <> for "not equal".

SELECT trip_id, distance_km, fare
FROM trips
WHERE fare > 60;

Practice

Find trips with a surge multiplier of 1.5 or more.

Return: trip id, city and surge multiplier

Write your query here

3Combine conditions with AND and OR

AND needs both conditions to be true; OR needs at least one. When you mix them, put brackets around the OR part.

SELECT trip_id, city, status
FROM trips
WHERE status = 'completed'
  AND (city = 'Austin' OR city = 'Denver');

Common mistake

Without the brackets, SQL runs AND first. You'd get completed Austin trips plus every Denver trip, cancelled ones included.

Practice

Find completed trips in Miami or Chicago with a fare over 40.

Return: trip id, city and fare

Write your query here

4Match a list with IN

IN is a short way to write several ORs on the same column. NOT IN keeps everything else.

SELECT trip_id, city
FROM trips
WHERE city IN ('Austin', 'Denver');

Practice

Find trips that didn't happen: status is cancelled or no_driver.

Return: trip id and status

Write your query here

5Pick a range with BETWEEN

BETWEEN a AND b keeps values from a to b, including both ends.

SELECT trip_id, distance_km
FROM trips
WHERE distance_km BETWEEN 5 AND 10;

For timestamps, use >= the first day and < the day after the last day, so nothing late on the last day gets lost.

SELECT trip_id, request_ts
FROM trips
WHERE request_ts >= '2026-03-01'
  AND request_ts < '2026-03-08';

Common mistake

request_ts BETWEEN '2026-02-01' AND '2026-02-28' misses almost all of February 28, because '2026-02-28' means midnight at the start of that day.

Practice

Find every trip requested in February 2026.

Return: trip id and request time

Write your query here

6Search text with LIKE

In LIKE, % means "any text". ILIKE works the same way but ignores upper and lower case.

SELECT name, city
FROM restaurants
WHERE name LIKE '%Sushi%';

Practice

You're now at DoorDash. Find restaurants with "pizza" anywhere in their name, in any capitalisation.

Return: name and city

Write your query here