How to Use SQL ORDER BY DESC: Syntax, Examples & Traps
Master SQL ORDER BY DESC with syntax rules, multi-column sorting, date ordering, and dialect-specific NULLS FIRST vs LAST handling.
In relational databases, rows have no default order. Without an explicit ORDER BY clause, query engines return records in whatever order storage pages are read. To rank highest-paying salaries, surface recent orders, or sort names in reverse alphabetical order, use ORDER BY DESC.
1. Basic Syntax and Sorting Rules
SELECT employee_id, first_name, salary
FROM employees
ORDER BY salary DESC;When you sort with DESC, different data types behave as follows:
| Data Type | ASC (Default) | DESC (Descending) | Example |
|---|---|---|---|
| Numbers | Smallest to largest (100 $\rightarrow$ 500) | Largest to smallest (500 $\rightarrow$ 100) | Top revenues, highest salaries |
| Dates / Timestamps | Oldest to newest (Jan 1 $\rightarrow$ Dec 31) | Newest to oldest (Dec 31 $\rightarrow$ Jan 1) | Latest orders, activity feeds |
| Text / Varchar | Alphabetical (A $\rightarrow$ Z) | Reverse alphabetical (Z $\rightarrow$ A) | Product lists, directory rosters |
According to the PostgreSQL Documentation on ORDER BY, sorting evaluates near the end of the SQL logical execution pipeline—right after SELECT—which allows you to sort by column aliases and computed fields.
2. Sample Dataset: employees
We will run the following examples against a sample employees table:
| id | name | department | salary | hire_date | bonus_rate |
|---|---|---|---|---|---|
| 1 | Alice Chen | Engineering | 120000 | 2024-03-15 | 0.15 |
| 2 | Bob Miller | Engineering | 95000 | 2025-01-10 | NULL |
| 3 | Clara Diaz | Marketing | 110000 | 2023-08-01 | 0.10 |
| 4 | David Kim | Marketing | 85000 | 2026-05-20 | 0.05 |
| 5 | Emma Watson | Engineering | 120000 | 2022-11-12 | 0.20 |
3. Core Examples
Example 1: Sort by a Single Column (Numbers & Dates)
To retrieve employees with the highest salary first:
SELECT name, salary
FROM employees
ORDER BY salary DESC;Output:
| name | salary |
|---|---|
| Alice Chen | 120000 |
| Emma Watson | 120000 |
| Clara Diaz | 110000 |
| Bob Miller | 95000 |
| David Kim | 85000 |
To sort by newest hire date first, apply DESC to the date column:
SELECT name, hire_date
FROM employees
ORDER BY hire_date DESC;Example 2: Sort by Multiple Columns (ASC + DESC)
When multiple rows share the same value (like Alice and Emma both earning 120,000), use a secondary column as a tie-breaker. You can mix directions:
-- Sort Department alphabetically (A-Z), then Salary from highest to lowest
SELECT department, salary, name
FROM employees
ORDER BY department ASC, salary DESC;Output:
| department | salary | name |
|---|---|---|
| Engineering | 120000 | Alice Chen |
| Engineering | 120000 | Emma Watson |
| Engineering | 95000 | Bob Miller |
| Marketing | 110000 | Clara Diaz |
| Marketing | 85000 | David Kim |
Independent Column Modifiers
The DESC keyword applies only to the column immediately preceding it. In ORDER BY department, salary DESC, department defaults to ASC. Always declare both explicitly for clarity.
4. The NULL Handling Trap: NULLS FIRST vs NULLS LAST
A common gotcha when sorting descending is where missing (NULL) values land:
- PostgreSQL / Oracle / Snowflake: Treat
NULLas larger than any value. InDESCsorting, NULLs appear at the top by default. - MySQL / SQL Server: Treat
NULLas smaller than any value. InDESCsorting, NULLs appear at the bottom by default.
Portable Fixes
To guarantee NULL values stay at the bottom across PostgreSQL and Oracle:
-- PostgreSQL / Oracle syntax
SELECT name, bonus_rate
FROM employees
ORDER BY bonus_rate DESC NULLS LAST;For MySQL and SQL Server (which do not support NULLS LAST), use a boolean condition:
-- MySQL / SQL Server syntax
SELECT name, bonus_rate
FROM employees
ORDER BY (bonus_rate IS NULL) ASC, bonus_rate DESC;5. Summary Cheat Sheet
| Scenario | Syntax Blueprint | Result |
|---|---|---|
| Highest to Lowest (Numbers) | ORDER BY salary DESC | Highest values at top |
| Newest to Oldest (Dates) | ORDER BY created_at DESC | Most recent dates at top |
| Alphabetical Reverse | ORDER BY name DESC | Z to A ordering |
| Multi-Column Combined | ORDER BY dept ASC, salary DESC | Dept alphabetical, salary high-to-low |
| Push NULLs to Bottom (Postgres) | ORDER BY bonus DESC NULLS LAST | Non-null values first, NULLs at end |
6. Next Steps & Interactive Practice
Practice sorting and query execution on live datasets:
- Interactive Question: Try Customer Spend Ranking to practice sorting transactions.
- Topic Hub: Drill more queries in the Basic SQL Practice Hub.
- Execution Order: Learn where sorting happens in our guide to SQL Order of Execution.
Master SQL with Interactive Challenges
Solve 190+ SQL interview and analytics questions with instant feedback and live database execution in DataBuoy.
Start Practicing SQL FreeFrequently Asked Questions
What does SQL ORDER BY DESC do?
The SQL ORDER BY DESC clause sorts query results in descending order (highest to lowest for numbers, newest to oldest for dates, and Z to A for strings). If omitted, SQL defaults to ascending (ASC) order.
Can you combine ASC and DESC in one query?
Yes. You can specify sort directions per column (e.g., ORDER BY department ASC, salary DESC). The database sorts by the first column first, using the second column as a tie-breaker.
How does SQL ORDER BY DESC handle NULL values?
In PostgreSQL and Oracle, DESC puts NULLs first by default unless you write NULLS LAST. In MySQL and SQL Server, NULLs are treated as the lowest values, so DESC places them last by default.
Can you use column aliases in an ORDER BY DESC clause?
Yes. In the SQL order of execution, ORDER BY runs after SELECT, so calculated column aliases are fully accessible.

Written by
Founder at Topfolio with 6+ years in data & analytics across JPMC, Ultrahuman, and high-growth startups. Sat on hiring panels, reviewed 500+ resumes, and writes practical SQL & data guides.
Related Articles
How to Use Data Engineer Skills: Complete Guide & Examples
Master modern data engineer skills with a practical tutorial. Learn SQL data modeling, Python ETL pipelines, dbt transforms, and Airflow orchestration.
How to Use Concat In Sql: Complete Guide & Examples
Master concat in sql across PostgreSQL, MySQL, SQL Server, Snowflake, and Oracle. Learn CONCAT vs CONCAT_WS syntax, NULL handling traps, and indexing tips.
VSCode SQLTools PostgreSQL Guide
Master vscode sqltools postgresql setup in 4 steps. Connect databases, configure schemas, fix query traps, and run SQL directly in Visual Studio Code.