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.
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.
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.
Think of It Like Filing Papers
The Syntax Rule
How the Engine Processes It
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."
Text: A → Z
Dates: oldest → newest
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
Example 1: Student Grades
Goal: List students sorted by grade level (ascending), then by last name (alphabetical) within each grade.
| first_name | last_name | grade |
|---|---|---|
| Emma | Adams | 9 |
| Liam | Chen | 9 |
| Noah | Davis | 9 |
| Olivia | Brown | 10 |
| Sophia | Kim | 10 |
| Ava | Lee | 11 |
Example 2: Mixed Directions
Goal: Show products by category (A→Z), then by price (highest first) within each category.
| product | category | price |
|---|---|---|
| Laptop Pro | Electronics | 1299 |
| Tablet Air | Electronics | 599 |
| Mouse Pad | Electronics | 12 |
| Winter Coat | Fashion | 180 |
| T-Shirt | Fashion | 25 |
Example 3: Three-Level Sort
Goal: Sort employees by department (A→Z), then hire date (newest first), then last name (A→Z).
| last_name | dept | hire_date |
|---|---|---|
| Garcia | Eng | 2024-03-15 |
| Baker | Eng | 2024-01-10 |
| Chen | Eng | 2024-01-10 |
| Adams | Sales | 2024-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.
Sorting by Expressions
You are not limited to raw column names. You can sort by computed expressions, function calls, and aliases (in most databases).
Common Mistakes
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?