Module 4 · SQL for Data Analysis
WHERE
Filter rows with comparisons, AND/OR, IN, BETWEEN, LIKE and NULL checks.
About 30 minutes
The problem
Harbourline has thousands of shipments, and nobody wants to scroll through all of them. Kemi's questions are always about a slice of the data:
- "Show me the big bookings: six containers or more."
- "Which shipments were cancelled?"
- "Which customers don't have an account manager yet?"
Each of these keeps some rows and drops the rest. That's what WHERE is for.
The concept
WHERE comes after FROM and keeps only the rows where a condition is true.
| Operator | Meaning | Example |
|---|---|---|
= | equal to | status = 'Delivered' |
<> or != | not equal to | mode <> 'Air' |
> >= < <= | greater / less than | containers >= 6 |
AND, OR | combine conditions | containers >= 6 AND status = 'Delivered' |
IN (…) | matches any value in a list | city IN ('Lagos', 'Abuja') |
BETWEEN a AND b | within a range, including both ends | booking_date BETWEEN '2026-01-01' AND '2026-01-31' |
LIKE | matches a text pattern | company_name LIKE '%Foods%' |
IS NULL | has no value | account_manager_id IS NULL |
Text values go in single quotes: 'Delivered'. Numbers don't: 6.
Example
SELECT shipment_id, booking_date, containers, status
FROM shipments
WHERE containers >= 6
AND status = 'Delivered';Walkthrough
WHERE containers >= 6keeps shipments of six containers or more.AND status = 'Delivered'also requires the shipment to be delivered. WithAND, both conditions must be true.- With
OR, either condition is enough.
When you mix AND and OR, use brackets to say what you mean:
SELECT company_name, city
FROM customers
WHERE (city = 'Lagos' OR city = 'Abuja')
AND industry = 'Pharmaceuticals';Without the brackets, AND is applied first and you'd get every Lagos customer plus only the pharmaceutical customers in Abuja.
IN is a tidier way to write several ORs on one column: city IN ('Lagos', 'Abuja').
LIKE matches patterns. % stands for "any characters", so '%Foods%' matches any name containing "Foods".
Finally, NULL means "no value". You can't test for it with =, because NULL = NULL isn't true in SQL. Always use IS NULL or IS NOT NULL:
SELECT company_name, city
FROM customers
WHERE account_manager_id IS NULL;Practice
Practice
Show the shipment_id, booking_date and freight_charge of every cancelled shipment.
Ctrl + Enter to run. Tab indents; press Esc then Tab to leave the editor.
Practice
Which customers don't have an account manager yet? Show their company_name and city.
Ctrl + Enter to run. Tab indents; press Esc then Tab to leave the editor.
Challenge
Challenge · optional
Show shipment_id, booking_date and status for shipments booked in January 2026 that were not delivered.
Ctrl + Enter to run. Tab indents; press Esc then Tab to leave the editor.
Challenge · optional
List the company_name of every customer in the food or agriculture business (industry 'Food & Beverage' or 'Agriculture') based in Lagos or Port Harcourt.
Ctrl + Enter to run. Tab indents; press Esc then Tab to leave the editor.
Check your understanding
Answer every question to check.