RadhaPublication
HomeInterviewLearning PathsRole TracksMockBooksPricing
Login / Sign UpLog in

© 2026 Radha Publication. All rights reserved.

Shop on AmazonDonateContactPrivacyTerms
SQL questions
EasySQL#246 min readJul 11, 2026

ORDER BY

practice
learning
Practice modeTest yourself instead of reading straight through

Why interviewers love this: ORDER BY looks tiny, but it tests whether you know the difference between a result set and a predictable result set.

Question: What does ORDER BY do in SQL?

Answer: ORDER BY sorts query results by one or more columns or expressions. By default it sorts in ascending order, and you can switch to descending with DESC. Without ORDER BY, SQL does not guarantee row order, even if a small test seems to come back the same way.

Interview-Ready Answer: I use ORDER BY when I need rows in a predictable sequence. It can sort by one field or several fields, like salary DESC, name ASC, and I usually add a tie-breaker such as an id. One important detail is that if I do not specify ORDER BY, the database is free to return rows in any order, so I never rely on the natural order of a table.

🧠 Memory Map
Memory map — visual summary of this topic

What it is

ORDER BY is the final sorting step of a query. It does not create rows or remove rows; it only decides the sequence of the rows you already selected.

How it works under the hood

  1. The database reads rows from tables and applies WHERE to remove non-matches.
  2. If the query uses GROUP BY or aggregates, it builds grouped rows next.
  3. The SELECT list is computed, including aliases and expressions like salary * 12.
  4. The engine then looks at the ORDER BY keys, such as created_at DESC or department, salary DESC.
  5. If an index already matches that order, the database may read the index in sequence and skip a separate sort. If not, it performs a sort, and for very large results it may spill to disk.
  6. The rows are returned to the client in that final order, and if you also use LIMIT or FETCH FIRST, the order decides which rows make the cut.

ORDER BY vs nearby clauses

ClauseMain jobChanges row count?Typical use
ORDER BYSort rowsNoDisplay, paging
WHEREFilter rowsYesFind matches
GROUP BYCombine rowsYesAggregates

When and why to use it

Use it when the user cares about recency, priority, ranking, alphabetical lists, or stable pagination. If you are showing page 1 of products, you need a sort key that never changes unexpectedly. In practice, a common pattern is ORDER BY created_at DESC, id DESC so the newest row appears first and ties are still deterministic.

Performance notes

A plain sort is usually O(n log n) time because the engine must compare rows repeatedly. Memory depends on row width: 1 million rows at about 200 bytes each is roughly 200 MB before overhead, so wide sorts can become expensive fast. Many engines use an external sort when memory is not enough, which means they write temporary files to disk; that is much slower than an in-memory sort. If the query only needs the top 10 or top 100 rows, some optimizers can use a top-N strategy with a priority queue, which behaves closer to O(n log k), where k is the number of rows you keep.

Important edge cases

  • Without ORDER BY, row order is not guaranteed.
  • Text sorting depends on collation (the language-specific rule for comparing strings), so A, a, and accented letters may sort differently by database or locale.
  • Null placement varies by database; be explicit with NULLS FIRST/LAST where available, or use COALESCE to control the position yourself.
  • Use a unique tie-breaker, such as the primary key, if you need stable pagination.
  • ORDER BY can sort by an alias or expression, but ordinal positions like ORDER BY 1 are fragile because column order changes break them.

Real-world story: In an e-commerce checkout service, the admin page showing recent orders must list the newest orders first. The query was written as SELECT id, customer_id, created_at FROM orders WHERE store_id = ? LIMIT 20 with no ORDER BY, so different database nodes returned rows in different physical storage order.

That caused page 1 to show older orders above newer ones, and pagination became unstable: an order could appear on page 1 during one request and page 2 on the next. Support agents saw confusing behavior, logs showed the same SQL text, and users reported that urgent refunded orders were not visible at the top.

The fix was simple but important: sort by created_at DESC, id DESC. The secondary id tie-breaker made the order deterministic when multiple orders shared the same timestamp.

SQL
-- Re-runnable demo: a small employee table with ties and a NULL value.
DROP TABLE IF EXISTS employees;

CREATE TABLE employees (
    id INTEGER PRIMARY KEY,
    name VARCHAR(50) NOT NULL,
    department VARCHAR(50) NOT NULL,
    salary INTEGER
);

INSERT INTO employees (id, name, department, salary) VALUES
(1, 'Asha', 'Engineering', 120000),
(2, 'Ben', 'Engineering', 120000),
(3, 'Cara', 'Sales', 90000),
(4, 'Diego', 'Sales', 110000),
(5, 'Eli', 'Support', NULL);

-- Sort by salary descending; name breaks ties so equal salaries are predictable.
SELECT id, name, department, salary
FROM employees
ORDER BY salary DESC, name ASC;

-- Edge case: NULL salaries. COALESCE turns NULL into a small number so missing values sort last.
SELECT id, name, department, salary
FROM employees
ORDER BY COALESCE(salary, -1) DESC, name ASC;

-- ORDER BY can use a computed alias from the SELECT list.
SELECT name, salary, COALESCE(salary, 0) * 12 AS annual_salary
FROM employees
ORDER BY annual_salary DESC, name ASC;

Follow-up & Tricky Questions:

  • Can ORDER BY use aliases? Yes. Because it runs after SELECT, you can sort by a computed alias like annual_salary. That keeps repeated expressions out of the sort clause and makes queries easier to read.
  • Can ORDER BY sort by a column that is not in the SELECT list? Usually yes, as long as the query shape allows it. The database sorts by the underlying data, not just by what you display.
  • What is the difference between ASC and DESC? ASC means low to high and is the default; DESC means high to low. If you sort by dates, DESC is common for newest first.
  • How do you make paging stable? Add a deterministic tie-breaker, usually a unique column such as the primary key. For example, ORDER BY created_at DESC, id DESC avoids duplicates or missing rows across pages.
  • How can ORDER BY affect performance? It can force the database to sort many rows before returning anything, which may be expensive. If an index matches the sort order, the engine may avoid a full sort and read rows in order instead.
  • Tricky: If rows appear in insertion order in my test, can I rely on that? No. That is an accident of the current storage and plan, not a promise from SQL. Once the table changes, the order can change too.
  • Tricky: Is ORDER BY 1 a good shortcut? It works in many databases because it means first selected column, but it is brittle. If someone reorders the select list later, the sort silently changes.
  • Tricky: Does ORDER BY happen before WHERE? Logically, no. WHERE filters first, then the remaining rows are sorted, so you should think of sorting as a late step in the query.

Common Mistakes:

  • Assuming natural order is real. Fix: always add ORDER BY when the order matters, even if the test data looks stable.
  • Sorting on a non-unique column only. Fix: add a tie-breaker such as the primary key so pagination stays deterministic.
  • Using ORDER BY 1 in important queries. Fix: spell out column names or aliases so refactors do not silently change the sort.
  • Ignoring text and null rules. Fix: remember that collation affects strings, and be explicit about null handling when the default is not what you need.

Memory Hook: Think of a nightclub: WHERE is the bouncer who decides who gets in, and ORDER BY is the usher who lines everyone up inside. Filtering comes first, sorting comes after.

Cheat Sheet:

  • ORDER BY sorts the final rows.
  • Default direction is ASC.
  • Use multiple keys for tie-breaks.
  • It can sort by aliases and expressions.
  • No ORDER BY means no guaranteed order.
  • Add a unique key for stable pagination.

Practice Tasks:

  • Sort a customers table by last_purchase_date DESC and then id ASC.
  • Write a query that places NULL scores last using COALESCE.
  • Build a paginated query that is stable across repeated runs by adding a unique tie-breaker.
Previous
Back to Questions←→to navigate
Next

Recommended Resources

Recommended Book

Cracking the Coding Interview

189 Programming Questions and Solutions

Recommended Book

Cracking the Coding Interview

189 Programming Questions and Solutions

-- Re-runnable demo: a small employee table with ties and a NULL value. DROP TABLE IF EXISTS employees; CREATE TABLE employees ( id INTEGER PRIMARY KEY, name VARCHAR(50) NOT NULL, department VARCHAR(50) NOT NULL, salary INTEGER ); INSERT INTO employees (id, name, department, salary) VALUES (1, 'Asha', 'Engineering', 120000), (2, 'Ben', 'Engineering', 120000), (3, 'Cara', 'Sales', 90000), (4, 'Diego', 'Sales', 110000), (5, 'Eli', 'Support', NULL); -- Sort by salary descending; name breaks ties so equal salaries are predictable. SELECT id, name, department, salary FROM employees ORDER BY salary DESC, name ASC; -- Edge case: NULL salaries. COALESCE turns NULL into a small number so missing values sort last. SELECT id, name, department, salary FROM employees ORDER BY COALESCE(salary, -1) DESC, name ASC; -- ORDER BY can use a computed alias from the SELECT list. SELECT name, salary, COALESCE(salary, 0) * 12 AS annual_salary FROM employees ORDER BY annual_salary DESC, name ASC;