Course

SQL ORDER BY Clause: Syntax, Usage, and Examples

The SQL ORDER BY clause lets you sort query results based on one or more columns. By default, it arranges the rows in ascending order, but you can explicitly set it to sort descending as well. Whether you’re building reports or cleaning data for analysis, ORDER BY in SQL helps you present information in a clear and useful way.

How to Use SQL ORDER BY

The basic syntax looks like this:

sql
SELECT column1, column2 FROM table_name ORDER BY column1 [ASC|DESC];
  • Use ASC for ascending order (lowest to highest), which is the default.
  • Use DESC for descending order (highest to lowest).

You can apply ORDER BY to one or more columns.

sql
SELECT name, age FROM users ORDER BY age;

This query sorts users by their age in ascending order. If you want the oldest users listed first, just add DESC.

sql
SELECT name, age FROM users ORDER BY age DESC;

You can also sort by multiple columns:

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

This sorts employees by department alphabetically, and within each department, by salary from highest to lowest.

When to Use ORDER BY in SQL

Create Clean Reports

When you use SQL ORDER BY in dashboards or reports, readers can digest the data more easily. Sorting by totals, names, or categories gives your audience a structured view.

Prioritize Data

Want to show top-performing products or newest signups? Sorting by date, quantity, or revenue helps highlight what matters most.

sql
SELECT product_name, total_sales FROM sales ORDER BY total_sales DESC;

Now you’ve got a list with best-sellers at the top.

Use with Aggregates

Combine ORDER BY with aggregate functions like SUM() or COUNT() to rank results.

sql
SELECT department, COUNT(*) AS employee_count FROM employees GROUP BY department ORDER BY employee_count DESC;

This shows departments with the most employees first.

Use in Pagination

If you’re limiting rows using LIMIT, combine it with ORDER BY to display sorted chunks of data.

sql
SELECT name, signup_date FROM users ORDER BY signup_date DESC LIMIT 10;

This retrieves the 10 most recently registered users.

Examples of SQL ORDER BY

Sort Alphabetically

sql
SELECT name FROM students ORDER BY name;

The database returns names in A–Z order. You don’t need to specify ASC because it’s the default.

SQL ORDER BY Descending

sql
SELECT name, score FROM exam_results ORDER BY score DESC;

This displays top scores at the top. Sorting in descending order makes high-performing entries stand out.

Sort by Multiple Columns

sql
SELECT first_name, last_name FROM customers ORDER BY last_name, first_name;

If two customers have the same last name, the query breaks the tie using the first name.

Use Aliases in ORDER BY

sql
SELECT product, price * quantity AS total FROM orders ORDER BY total DESC;

Even though total doesn’t exist in the table, you can sort using the alias defined in the SELECT clause.

ORDER BY in SQL with Window Functions

Window functions often rely on ordering. For example, using RANK() or ROW_NUMBER() requires ORDER BY.

sql
SELECT name, department, salary, RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS dept_rank FROM employees;

Here, you rank employees within each department by salary. The ORDER BY the window function pattern gives you ranking, rolling totals, and moving averages—great tools for analytics.

Learn More About ORDER BY SQL

Order by Ascending SQL (Explicit)

Even though SQL defaults to ascending, you can write it explicitly:

sql
SELECT name FROM users ORDER BY name ASC;

This adds clarity, especially when mixed with descending orders on other fields.

SQL ORDER BY Multiple Columns with Mixed Directions

Sorting by more than one column? You can use different directions for each:

sql
SELECT department, salary FROM employees ORDER BY department ASC, salary DESC;

You’ll get departments in A–Z order and salaries in descending order within each group.

Use ORDER BY with NULLS FIRST / LAST

Some databases let you decide how to treat NULL values when sorting:

sql
SELECT name, last_login FROM users ORDER BY last_login DESC NULLS LAST;

This moves NULL entries (users who’ve never logged in) to the end.

ORDER BY with Expressions

You’re not limited to columns—you can sort by calculated values too:

sql
SELECT name, (price - discount) AS final_price FROM products ORDER BY final_price;

Sorting based on logic you define gives you control over how the data appears.

Combine ORDER BY with LIMIT for Top-N Queries

Need just the top 5 entries?

sql
SELECT name, score FROM leaderboard ORDER BY score DESC LIMIT 5;

This is perfect for leaderboard views, recent logs, or summary cards in applications.

Order by Non-Selected Columns

You don’t have to show the column you’re sorting by:

sql
SELECT title FROM articles ORDER BY published_at DESC;

Even if you leave out published_at from the result, you can still sort by it.