In ORDER BY last_name, first_name DESC, which columns sort descending, and how do you reverse both?
answer
- The keyword is local, not global
- Each sort key has its own direction
- Second key only breaks first-key ties
- ASC is the default when omitted
- DESC applies only to first_name here
basics
~10 sASC and DESC bind to one sort key each, so only first_name is descending; last_name still uses the default ASC. To reverse both, spell it out: ORDER BY last_name DESC, first_name DESC.
solid answer
~40 sORDER BY takes a comma-separated list of **sort specifications**, and each one carries its own direction. `ORDER BY last_name, first_name DESC` therefore sorts `last_name` ascending (ASC is the default when you write nothing) and only breaks ties on `last_name` by `first_name` descending. The keys are applied left to right: the second key is consulted only for rows that are equal on the first, the third only for rows equal on the first two. Mixed directions are perfectly normal — `ORDER BY department, salary DESC` lists departments alphabetically with the best-paid person first inside each one. If you want both keys descending you must write DESC twice: `ORDER BY last_name DESC, first_name DESC`.
code
sql · 10 lines-- Intent: newest first, then Z-to-A by title
-- Wrong: published_on is still ASC
SELECT title, published_on
FROM articles
ORDER BY published_on, title DESC;
-- Right: state the direction on every key
SELECT title, published_on
FROM articles
ORDER BY published_on DESC, title DESC;go deeper
Remember that ASC is the default and that ASC/DESC attaches to one key at a time. Be ready to write a two-key ORDER BY out loud and say which column each keyword affects.
Explain the left-to-right, tie-breaker comparison the key list defines, and why mixed directions like department ASC, salary DESC are ordinary rather than exotic.
Show that you write ASC explicitly in mixed lists for readability, and that you end a key list with a unique column when downstream code, exports or pagination depend on a repeatable order.
Frame sort-key discipline as a contract: reports, exports and API responses that promise an order need that order pinned in the query, not inherited from whatever the plan happened to produce.
## What ORDER BY is asked to do ORDER BY is the only construct in SQL that constrains the order of the rows a query returns. It takes a comma-separated list of *sort specifications*, and each specification has the shape: ```sql <expression> [ ASC | DESC ] [ NULLS FIRST | NULLS LAST ] ``` The crucial detail — and the one this question is really testing — is that ASC and DESC are part of a single sort specification. They attach to the expression they follow, not to the whole ORDER BY list. There is no "set the direction for everything" switch in standard SQL. ## Reading the clause left to right The sort keys form a lexicographic (dictionary-style) comparison. To order two rows, the engine compares them on the first key. If they differ, that decides the order and no further key is examined. Only if they are equal on the first key does the second key get a vote, and so on. Every key after the first is therefore a *tie-breaker*, applied to a progressively smaller set of tied rows. This is exactly how a phone book works: surname decides most of the ordering, and given names only matter among people who share a surname. ## Walking through the example Take these rows: ```text (Adams, Zoe) (Adams, Amy) (Baker, Carl) ``` `ORDER BY last_name, first_name DESC` produces: ```text (Adams, Zoe) (Adams, Amy) (Baker, Carl) ``` The Adams rows come before Baker because `last_name` is ascending. Within the two Adams rows, `first_name DESC` puts Zoe before Amy. Nothing about the DESC touched `last_name`. Swap the intent and you must swap the syntax: ```sql ORDER BY last_name DESC, first_name DESC -- Baker, then Adams/Zoe, then Adams/Amy ``` ## Why the mistake is so easy In English, "sort by last name and first name, descending" naturally reads as if *descending* covers the whole phrase. SQL parses it the other way: the modifier is local. The bug is quiet — the query runs, returns rows, and the first column simply happens to be in the wrong direction — so it usually survives review and is discovered by someone reading a report. ## Mixed directions are a feature, not a workaround Many real orderings genuinely mix directions: ```sql SELECT department, employee_name, salary FROM employees ORDER BY department ASC, salary DESC, employee_name ASC; ``` Departments in alphabetical order; inside each department, highest salary first; and among people on identical salaries, alphabetical by name. Writing ASC explicitly, as above, costs nothing and makes the intent unmistakable to the next reader. ## The direction applies to the whole key expression A sort key can be an expression, and the direction applies to that expression's value, not to any part of it: ```sql ORDER BY (unit_price * quantity) DESC, order_id ASC; ``` There is no way to say "descending on the multiplication but ascending on the price" — if you need that, they are two separate keys. ## What extra keys buy you Each additional key resolves more ties. Rows that remain equal on *every* key you listed are in an unspecified order: SQL makes no promise about their relative position, and the same query can return them differently on a later run. If a stable, repeatable order matters, the last key should be something unique for the row, such as the primary key. ## Practical checklist - Write ASC explicitly when a list mixes directions — it removes all doubt. - Read every comma as "and then, for ties, ...". - Remember that DESC never leaks left to the previous key or right to the next one. - Finish the key list with a unique column when you need the same output every time. ## A note on what ORDER BY does not do ORDER BY orders the rows the query already computed; it does not filter them, does not change which rows appear, and does not affect grouping. Adding a second sort key never adds or removes a row — it only decides which of two tied rows is printed first.
- If two rows are equal on every key you listed, what order do they come back in?Unspecified. ORDER BY constrains only the relative order of rows that differ on some sort key; rows tied on all of them may be returned in any order, and that order can change between runs of the same query. If you need repeatable output, add a key that is unique per row, typically the primary key, as the final tie-breaker.
- Does adding a third sort key ever change which rows appear in the result?No. ORDER BY only arranges the rows the rest of the query already produced — it never filters, adds, or duplicates rows. A third key can only change the relative position of rows that were already tied on the first two keys. Filtering is WHERE's job; row counts are decided before ordering happens.
- Is there a way to set one direction for the whole ORDER BY list at once?No. Standard SQL has no clause-level direction switch; ASC or DESC is part of each individual sort specification, so a five-key descending sort needs DESC written five times. The verbosity is the price of being able to mix directions, which most real orderings need.
Sort keys work like a phone book: the surname decides almost everything, and the given name only matters between people who already share a surname.
saying these in an interview costs you the question
- Thinks DESC at the end applies to every sort key
- Believes the last key listed dominates the sort
- Assumes ORDER BY sorts each column independently
- Says extra sort keys filter or reduce rows
- Claims there is a clause-wide ASC/DESC switch