Brightlane's customer success team is building a city-based territory report and wants cities listed in reverse alphabetical order.
Write a query to return each customer's name and city.
Assumptions:
- Some customers have a recorded
cityvalue; others havecityset toNULL. - Customers with no city on record must appear at the end of the list, regardless of sort direction.
- Customers within the same city should be ordered by
idascending.
Output:
- One row per customer, with columns
nameandcity, sorted bycitydescending (NULL cities last), then byidascending.
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
name,
city
FROM
customers
ORDER BY
city DESC NULLS LAST,
id The shape
NULLS LAST overrides PostgreSQL's default NULL position so that customers with no recorded city land at the end of the report, even though the sort direction is DESC — which would otherwise put them at the top.
Clause by clause
SELECT name, cityreturns the two columns the territory report needs.FROM customersreads every customer on file, including those whosecityisNULL.ORDER BY city DESC NULLS LAST, idcarries three pieces.city DESCsorts cities in reverse alphabetical order, soZurichcomes beforeWarsawand so on down to the earliest letter.NULLS LASTattaches to that same sort key and tells PostgreSQL where to place rows whosecityisNULLregardless of direction. The second sort key,idascending, breaks ties when multiple customers share the same city — three customers inTorontocome back inidorder.
Why this and not ORDER BY city DESC
Without the explicit NULLS LAST, PostgreSQL applies its default rule: NULL values sort first under DESC and last under ASC. So a plain ORDER BY city DESC would push every customer with no recorded city to the top of the list — directly contradicting the prompt, which says those customers must appear at the end regardless of direction. The default isn't wrong; it just doesn't match what the territory report needs.
NULLS LAST is the explicit override. It binds to the sort key it follows, so the placement is determined by the clause itself rather than by PostgreSQL's defaults plus the sort direction.
The trap
The default NULL position flips with the sort direction. Switch a working ORDER BY city ASC (NULLs at the end, as expected) to ORDER BY city DESC to flip the alphabetical order, and the NULLs silently jump from the bottom of the report to the top. The query runs, the cities sort correctly, and the rows with no recorded city now sit above Zurich — completely changing the shape of the report. Any time a report has a required NULL position that has to hold across sort-direction changes, write NULLS FIRST or NULLS LAST explicitly. Don't rely on the default lining up with the direction by accident.
You practiced overriding PostgreSQL's default NULL-sort position with NULLS LAST. The default places NULLs first under DESC and last under ASC — explicit NULLS FIRST / NULLS LAST is the recurring fix any time the report's required NULL position contradicts the default.