Brightlane's UK data-enrichment team is prioritising incomplete profiles for an outreach effort to collect missing location data.
Write a query to return the ID and name of every UK customer who has no city on file.
Assumptions:
- The
customerstable contains every customer Brightlane has on file. - UK customers are identified by
country = 'GB'. - A missing
cityis stored asNULL, not as an empty string.
Output:
- One row per qualifying UK customer, with columns
idandname.
Schema · ecommerce 5 tables
Run previews · Check grades
Write a query, then run it to see results here.
Worked solution Try it yourself first
SELECT
id,
name
FROM
customers
WHERE
country = 'GB'
AND city IS NULL The shape
A country equality and an IS NULL test joined by AND — both have to hold on the same row for it to land in the outreach list. The result is a single row: Ola Nelson, the one UK customer with no city recorded.
Clause by clause
SELECT id, namereturns just the two columns the outreach effort needs to address each profile.FROM customersreads the customer table. The filter narrows the population from there.WHERE country = 'GB' AND city IS NULLis the compound condition.country = 'GB'keeps UK rows.city IS NULLkeeps rows where the city is absent. TheANDrequires both to be true on the same row, so a UK customer with a city on file is dropped, and a US customer with no city is also dropped. Only the UK customers with missing city values reachSELECT.
Why IS NULL and not city = NULL
A comparison with = against NULL doesn't return true or false. It returns unknown, because PostgreSQL can't decide whether a missing value equals anything. WHERE keeps only rows where the condition evaluates to true, so an unknown row is silently dropped — and every NULL row goes through that branch. The query runs, returns zero rows, and looks like "there are no UK customers with a missing city" when really the test never matched anything.
IS NULL is the operator that exists for exactly this case. It doesn't compare values; it asks a direct question — is this value absent? — and always returns true or false. That's the only correct way to test for a missing value in PostgreSQL.
You practiced combining a value-equality test with an IS NULL test using AND. NULL filters mix freely with regular conditions inside a single WHERE — the only rule is that NULL itself must be tested with IS NULL, never with =.