N007-H1 Tier 1 · Foundations · hard ecommerce · Brightlane

Return each customer's name and city

Part of ORDER BY and Result Sorting in SQL

The problem

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 city value; others have city set to NULL.
  • 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 id ascending.

Output:

  • One row per customer, with columns name and city, sorted by city descending (NULL cities last), then by id ascending.
Schema · ecommerce 5 tables
categories
id integer
name text
parent_id? integer
products
id integer
name text
category_id integer
price numeric
stock_qty integer
attributes? jsonb
order_items
id integer
order_id integer
product_id integer
quantity integer
unit_price numeric
customers
id integer
name text
email text
city? text
country text
created_at timestamptz
is_active boolean
orders
id integer
customer_id integer
ordered_at timestamptz
status text
total_amount numeric

Run previews · Check grades

Write a query, then run it to see results here.

Worked solution Try it yourself first
Solution query
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, city returns the two columns the territory report needs.
  • FROM customers reads every customer on file, including those whose city is NULL.
  • ORDER BY city DESC NULLS LAST, id carries three pieces. city DESC sorts cities in reverse alphabetical order, so Zurich comes before Warsaw and so on down to the earliest letter. NULLS LAST attaches to that same sort key and tells PostgreSQL where to place rows whose city is NULL regardless of direction. The second sort key, id ascending, breaks ties when multiple customers share the same city — three customers in Toronto come back in id order.

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.

How you actually get good at SQL

Reading explains SQL. Writing it, over and over with instant feedback, is what makes you fluent.

That's the whole SQLMaxx loop: 600+ real problems, instant AI feedback, mastery you can actually see, and spaced review that won't let you forget.

A stack of SQL practice problem cards, the top card showing an employees table.
615 problems · 66 concepts

Real problems. Not toy examples.

615 hand-built problems spanning all 66 concepts, from basic SELECTs to window functions, built on real schemas and real business questions, the kind you'll actually get asked on the job. Enough reps to make SQL automatic.

A retro computer showing a SQL query marked correct with a green checkmark.
Instant AI feedback

Write a query. Know if it's right in one second.

No copying an answer and hoping it clicked. The AI grader checks your real query against real data, catches exactly what's wrong, and explains the fix in plain English, like a senior analyst reading over your shoulder on every problem.

A circular mastery progress dial filling from blue to green, the SQLMaxx diamond at its center.
Mastery tracking

Stop guessing whether you actually know it.

SQLMaxx tracks every concept and shows you what you've mastered and what's still shaky. Your skills fill in one concept at a time, so 'I think I get joins' becomes something you can prove.

A SQL query editor circled by a blue return arrow with a clock, scheduled to come back for review.
Spaced review

Learn it once. Keep it for good.

Most of what you learn this week fades by next week. So when a concept comes due for review, SQLMaxx hands you a fresh problem to solve from a blank editor, not a flashcard to re-read. A research-backed spaced-repetition algorithm (FSRS) times each return for right before you'd forget, so your SQL is still there months later, when the interview or the job actually needs it.

Practice, feedback, mastery, review. That's the loop that turns reading into real skill.

Start free

No account, no credit card. Start solving in under a minute.