01 / 21

Multi-Column
Ordering in SQL

Master the ORDER BY clause with multiple columns and tie-breaker logic to control exactly how your query results are sorted.

SQL

What is ORDER BY?

SQL databases store rows in no guaranteed order. When you run a SELECT query, the rows can come back in any arrangement. The ORDER BY clause tells the database exactly how to sort the result set before returning it to you.

SELECT name, grade FROM students ORDER BY grade DESC;

Why Sort by Multiple Columns?

When your first sort column has duplicate values (ties), the database doesn't know how to order those tied rows. A second column acts as a tie-breaker, giving you predictable and meaningful results.

Single Column Sort dept name Eng Zara Eng Alice Eng Marcus Sales Bob Random order! Multi-Column Sort dept name Eng Alice Eng Marcus Eng Zara Sales Bob A→Z

Think of It Like Filing Papers

1st Sort
Group papers into folders by department: Engineering, Sales, Marketing.
2nd Sort
Within each folder, arrange by last name alphabetically: Adams, Baker, Clark.
3rd Sort
If last names match, arrange by first name: John Adams before Sarah Adams.

The Syntax Rule

SELECT column1, column2, ... FROM table_name ORDER BY column_a [ASC|DESC], column_b [ASC|DESC], column_c [ASC|DESC];
1
Comma-separated columns after ORDER BY, listed in priority order.
2
Each column gets its own direction: ASC or DESC. You can mix them freely.
3
Left to right = highest to lowest priority. The first column is the primary sort; the rest are tie-breakers.

How the Engine Processes It

Step 1 Sort ALL rows by column_a Step 2 Within ties, sort by column_b Step 3 Still ties? Sort by column_c Example: ORDER BY dept ASC, salary DESC, name ASC dept salary name Eng 95000 Alice Eng 85000 Bob Eng 85000 Carla Sales 90000 Dan Same dept Tie-breaker

ASC vs DESC: Mixing Directions

Each column in the ORDER BY clause gets its own direction. This lets you create sorting patterns like "highest salary first, but alphabetical within the same salary."

ORDER BY salary DESC, name ASC;
ASC (Default)
Numbers: 1 → 99
Text: A → Z
Dates: oldest → newest
DESC (Explicit)
Numbers: 99 → 1
Text: Z → A
Dates: newest → oldest

Column Names vs Positions

SQL allows you to reference columns in ORDER BY by name or by their ordinal position in the SELECT list (1-based). Both produce identical results, but have different trade-offs.

By Name By Position
Syntax ORDER BY dept, name ORDER BY 1, 3
Readability High: intent is clear Low: must count columns
Maintainability Safe: survives SELECT changes Fragile: breaks if SELECT order changes
Best Use Production code Quick ad-hoc queries

How to Apply Multi-Column Ordering

1
Identify the primary sort: What is the most important grouping? (e.g., department, category, date)
2
Anticipate ties: Will multiple rows share the same value in the primary column?
3
Choose a tie-breaker: Pick a secondary column that distinguishes tied rows meaningfully.
4
Set each direction: Decide ASC or DESC for each column independently.
5
Write and test: List columns in ORDER BY separated by commas, then verify the output.

Example 1: Student Grades

Goal: List students sorted by grade level (ascending), then by last name (alphabetical) within each grade.

SELECT first_name, last_name, grade FROM students ORDER BY grade ASC, last_name ASC;
first_namelast_namegrade
EmmaAdams9
LiamChen9
NoahDavis9
OliviaBrown10
SophiaKim10
AvaLee11

Example 2: Mixed Directions

Goal: Show products by category (A→Z), then by price (highest first) within each category.

SELECT product, category, price FROM products ORDER BY category ASC, price DESC;
productcategoryprice
Laptop ProElectronics1299
Tablet AirElectronics599
Mouse PadElectronics12
Winter CoatFashion180
T-ShirtFashion25

Example 3: Three-Level Sort

Goal: Sort employees by department (A→Z), then hire date (newest first), then last name (A→Z).

SELECT last_name, dept, hire_date FROM employees ORDER BY dept ASC, hire_date DESC, last_name ASC;
last_namedepthire_date
GarciaEng2024-03-15
BakerEng2024-01-10
ChenEng2024-01-10
AdamsSales2024-06-01

What About NULL Values?

When a sort column contains NULL values, different database engines handle them differently. This matters when NULLs appear in your tie-breaker columns.

PostgreSQL / Oracle
NULLs sort last in ASC, first in DESC. You can override with NULLS FIRST / NULLS LAST.
MySQL / SQLite
NULLs are treated as the smallest value: first in ASC, last in DESC. No NULLS FIRST/LAST syntax.
-- PostgreSQL: force NULLs to the end ORDER BY bonus DESC NULLS LAST, name ASC;

Sorting by Expressions

You are not limited to raw column names. You can sort by computed expressions, function calls, and aliases (in most databases).

SELECT first_name, last_name, score_math + score_reading AS total FROM test_scores ORDER BY total DESC, last_name ASC;

Common Mistakes

✕ Wrong Column Order
Writing ORDER BY name, dept when you meant ORDER BY dept, name. The first column is always the primary sort.
✕ Assuming Default DESC
Forgetting that ASC is the default. If you want highest-first sorting, you must write DESC explicitly.
✕ Single Direction for All
Writing ORDER BY a, b DESC and thinking DESC applies to both. It only applies to b: column a remains ASC.

Quick Reference Summary

Concept Syntax Behavior
Single column sort ORDER BY col Sorts all rows by one column
Multi-column sort ORDER BY col1, col2 col2 breaks ties in col1
Mixed directions ORDER BY col1 ASC, col2 DESC Each column independent
Positional reference ORDER BY 1, 2 Uses SELECT list position
Expression sort ORDER BY col1 + col2 Sorts by computed value
NULL handling NULLS FIRST / LAST DB-dependent defaults

Question 1 of 5

Given the query:
ORDER BY category ASC, price DESC

If two products are both in the "Books" category, which product appears first?

Question 2 of 5

What is the default sort direction when you write
ORDER BY name without specifying ASC or DESC?

Question 3 of 5

In the query:
ORDER BY dept ASC, salary DESC, name ASC

Which column acts as the second tie-breaker?

Question 4 of 5

A developer writes:
ORDER BY 2 DESC, 1 ASC

What do the numbers 2 and 1 refer to?

Question 5 of 5

Consider this ORDER BY clause:
ORDER BY a, b DESC

Which statement is true?