- Aggregate functions
- AVERAGE function
- BETWEEN operator
- CASE expression
- CAST() function
- COALESCE() function
- Comment
- Common table expression
- Constraints
- CONVERT function
- Cursor
- Data types
- Date functions
- DELETE statement
- DROP TABLE statement
- EXISTS operator
- HAVING clause
- IF statement
- Index
- IS NOT NULL condition
- IS NULL condition
- Joins
- LAG function
- LENGTH() function
- LIKE operator
- MERGE statement
- Normalization
- Not equal
- Operators
- ORDER BY clause
- Partition
- Pivot table
- Regex
- REPLACE function
- ROUND function
- SELECT DISTINCT clause
- SELECT statement
- Set operators
- Stored procedure
- String functions
- Subquery
- Substring
- Temporary table
- Transaction
- Trigger
- TRUNCATE TABLE
- UPDATE statement
- Views
- WHERE clause
- Window functions
SQL
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:
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.
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
.
SELECT name, age
FROM users
ORDER BY age DESC;
You can also sort by multiple columns:
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.
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.
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.
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
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
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
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
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
.
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:
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:
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:
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:
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?
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:
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.
Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.