Tutorial

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.

Anuj SainiOct 28, 20263 min read

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

sql
SELECT employee_id, first_name, salary
FROM employees
ORDER BY salary DESC;

When you sort with DESC, different data types behave as follows:

Data TypeASC (Default)DESC (Descending)Example
NumbersSmallest to largest (100 $\rightarrow$ 500)Largest to smallest (500 $\rightarrow$ 100)Top revenues, highest salaries
Dates / TimestampsOldest to newest (Jan 1 $\rightarrow$ Dec 31)Newest to oldest (Dec 31 $\rightarrow$ Jan 1)Latest orders, activity feeds
Text / VarcharAlphabetical (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:

idnamedepartmentsalaryhire_datebonus_rate
1Alice ChenEngineering1200002024-03-150.15
2Bob MillerEngineering950002025-01-10NULL
3Clara DiazMarketing1100002023-08-010.10
4David KimMarketing850002026-05-200.05
5Emma WatsonEngineering1200002022-11-120.20

3. Core Examples

Example 1: Sort by a Single Column (Numbers & Dates)

To retrieve employees with the highest salary first:

sql
SELECT name, salary
FROM employees
ORDER BY salary DESC;

Output:

namesalary
Alice Chen120000
Emma Watson120000
Clara Diaz110000
Bob Miller95000
David Kim85000

To sort by newest hire date first, apply DESC to the date column:

sql
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:

sql
-- Sort Department alphabetically (A-Z), then Salary from highest to lowest
SELECT department, salary, name
FROM employees
ORDER BY department ASC, salary DESC;

Output:

departmentsalaryname
Engineering120000Alice Chen
Engineering120000Emma Watson
Engineering95000Bob Miller
Marketing110000Clara Diaz
Marketing85000David 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 NULL as larger than any value. In DESC sorting, NULLs appear at the top by default.
  • MySQL / SQL Server: Treat NULL as smaller than any value. In DESC sorting, NULLs appear at the bottom by default.

Portable Fixes

To guarantee NULL values stay at the bottom across PostgreSQL and Oracle:

sql
-- 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:

sql
-- MySQL / SQL Server syntax
SELECT name, bonus_rate
FROM employees
ORDER BY (bonus_rate IS NULL) ASC, bonus_rate DESC;

5. Summary Cheat Sheet

ScenarioSyntax BlueprintResult
Highest to Lowest (Numbers)ORDER BY salary DESCHighest values at top
Newest to Oldest (Dates)ORDER BY created_at DESCMost recent dates at top
Alphabetical ReverseORDER BY name DESCZ to A ordering
Multi-Column CombinedORDER BY dept ASC, salary DESCDept alphabetical, salary high-to-low
Push NULLs to Bottom (Postgres)ORDER BY bonus DESC NULLS LASTNon-null values first, NULLs at end

6. Next Steps & Interactive Practice

Practice sorting and query execution on live datasets:

Master SQL with Interactive Challenges

Solve 190+ SQL interview and analytics questions with instant feedback and live database execution in DataBuoy.

Start Practicing SQL Free

Frequently 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.

Anuj Saini

Written by

Anuj SainiFounder & Lead Instructor

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.