A shipping system at Brightlane is building a city dropdown for address validation. The dropdown needs every city value that appears in the customer base, with a single entry representing customers who have no city on file.
Write a query to return one row per unique city value.
Assumptions:
- The
customerstable contains every customer Brightlane has on file. - Some customers have a recorded
cityvalue; many customers have a missingcity. - Customers with no city on file appear as a single row in the dropdown — one entry, not one per missing-city customer.
Output:
- One row per unique city value, with a single column
city. One row will have a missingcity.
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 DISTINCT
city
FROM
customers The shape
DISTINCT treats two NULL values as equal for the purpose of deduplication, even though NULL = NULL is unknown everywhere else in SQL. That's the entire reason this query works as a single statement. Hundreds of customers with no city on file collapse into one NULL row in the dropdown, alongside every real city.
Clause by clause
SELECT DISTINCT cityreturns the uniquecityvalues from whatever rowsFROMhands up. The deduplication runs across the full candidate set, and a NULL row collapses against any other NULL row the same way twoLondonrows would collapse.FROM customersis the row source. Every customer contributes theircityvalue — including the missing ones — to the candidate set. There's no filter; the dropdown wants the missing case represented, not removed.
Why this and not a two-statement approach
The natural-feeling instinct is to think of NULL handling as a separate concern: deduplicate the real cities, then add a row for the missing case. That instinct comes from how NULL equality usually works in SQL, where two NULLs are never equal to each other. If DISTINCT followed those rules, every NULL row would survive deduplication as its own distinct value, and the dropdown would contain hundreds of empty rows.
DISTINCT doesn't follow those rules. The SQL standard makes a deliberate exception for deduplication contexts: two NULLs are equal for the purpose of removing duplicates. So the single SELECT DISTINCT city FROM customers produces exactly the dropdown shape the address-validation system needs — one row per real city plus one row representing the missing case.
You practiced relying on DISTINCT's NULL-as-equal behavior. The recurring shape any time a deduplicated result must surface a single 'missing' row alongside the real values.