01 / 22

SQL ORDER BY

Master the art of sorting query results using ascending and descending order to make your data meaningful and organized.

SQL

What is ORDER BY?

When you run a SQL query, the database returns rows in no guaranteed order. The ORDER BY clause lets you sort the result set by one or more columns so the data comes back in a predictable, useful sequence.

Why Sorting Matters

★ Readability
Alphabetical names, chronological dates, and ranked scores are far easier to scan than random order.
★ Analysis
Finding the top 10, the bottom 5, or spotting trends requires sorted data as a foundation.
★ Reporting
Business reports, leaderboards, and invoices all depend on a consistent, deterministic row order.

Unsorted vs Sorted

Unsorted name score Charlie 72 Alice 95 Eve 88 Bob 61 Dana 84 ORDER BY Sorted by score DESC name score Alice 95 Eve 88 Dana 84 Charlie 72 Bob 61

The Syntax

SELECT column1, column2 FROM table_name WHERE condition ORDER BY column1 [ASC | DESC];
ASC
Ascending order (\(A \to Z, 1 \to 99\)). This is the default if you omit the keyword.
DESC
Descending order (\(Z \to A, 99 \to 1\)). You must write it explicitly.

ASC vs DESC

ASC (Default) Smallest → Largest 10 30 50 80 DESC Largest → Smallest 80 50 30 10

Where Does ORDER BY Go?

ORDER BY is always the last clause in a SELECT statement (before LIMIT, if present). The database filters and groups first, then sorts the final result.

SELECT FROM WHERE (optional) GROUP BY (optional) ORDER BY ← Always last!

Step-by-Step Approach

1
Write your SELECT and FROM: decide which columns you need and from which table.
2
Add WHERE if needed: filter the rows before sorting.
3
Append ORDER BY column_name: pick the column you want to sort on.
4
Choose ASC or DESC: omit for ascending (default) or write DESC for descending.
5
Add more sort columns: separate with commas for tie-breaking (e.g., ORDER BY grade DESC, name ASC).

Example 1: Alphabetical Sort

SELECT name, age FROM students ORDER BY name ASC;
name ↑ age
Alice16
Bob17
Charlie16
Dana15
Eve17

Example 2: Highest Score First

SELECT name, score FROM exam_results ORDER BY score DESC;
name score ↓
Alice95
Eve88
Dana84
Charlie72
Bob61

Example 3: Multi-Column Sort

SELECT grade, name, score FROM students ORDER BY grade ASC, score DESC;
grade ↑ name score ↓
9Dana84
9Bob61
10Alice95
10Eve88
11Charlie72

Sorting Text vs Numbers

Text (VARCHAR)
Sorted alphabetically (lexicographic): A before B, uppercase before lowercase (varies by database collation).
Numbers (INT, FLOAT)
Sorted by numeric value: 5 before 12 before 100. The natural math ordering you expect.

How NULLs Are Sorted

NULL values represent missing data. Where they appear in sorted results depends on the database:

Database ASC Order DESC Order
MySQL NULLs first NULLs last
PostgreSQL NULLs last NULLs first
SQLite NULLs first NULLs last

Combining WHERE and ORDER BY

SELECT name, score FROM exam_results WHERE score >= 80 ORDER BY score DESC;
name score ↓
Alice95
Eve88
Dana84

Sorting by Column Position

Instead of the column name, you can use its position number in the SELECT list. Column 1 is the first column listed, column 2 is the second, and so on.

-- These two queries are identical: SELECT name, score FROM students ORDER BY score; SELECT name, score FROM students ORDER BY 2;

Common Mistakes

✕ ORDER BY Before WHERE
ORDER BY must come after WHERE, not before. SQL has a strict clause order.
✕ Expecting Stable Order
Without ORDER BY, row order is undefined. Never assume the database returns rows in insertion order.
✕ Text Numbers Surprise
Sorting a VARCHAR column of numbers gives lexicographic order: \( \text{"9"} > \text{"100"} \). Cast to INT first.

Summary Table

Feature ASC DESC
Direction Low \(\to\) High High \(\to\) Low
Text \(A \to Z\) \(Z \to A\)
Numbers \(1, 2, 3, \dots\) \(\dots, 3, 2, 1\)
Dates Oldest \(\to\) Newest Newest \(\to\) Oldest
Default? Yes No (must specify)
Multi-column Mix freely: ORDER BY a ASC, b DESC

Question 1 of 5

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

Question 2 of 5

Which query correctly returns student names sorted from highest score to lowest?

Question 3 of 5

Given the query:
ORDER BY grade ASC, name DESC
Which column is the primary sort key?

Question 4 of 5

A column named price is stored as VARCHAR. You run ORDER BY price ASC. Which order is correct?

Question 5 of 5

Where must the ORDER BY clause be placed in a SQL query?