When working with databases, organizing and retrieving data in a specific order is crucial for efficient analysis and presentation. SQL, or Structured Query Language, provides the `ORDER BY` clause to sort the result-set in ascending or descending order. This clause is essential for managing and understanding the vast amounts of data stored in databases. In this article, we will explore five ways to use the `ORDER BY` clause effectively, demonstrating its versatility and importance in SQL queries.
Key Points
- Sorting data in ascending or descending order using the `ORDER BY` clause.
- Utilizing the `ASC` and `DESC` keywords for specifying sort order.
- Sorting data based on multiple columns to achieve more detailed organization.
- Using aggregate functions with `ORDER BY` to analyze and sort summarized data.
- Applying the `LIMIT` clause in conjunction with `ORDER BY` to restrict the number of rows returned.
1. Basic Sorting with ORDER BY

The most basic use of the ORDER BY
clause is to sort data in either ascending (ASC
) or descending (DESC
) order based on one or more columns. By default, if no sort order is specified, the data is sorted in ascending order. For example, to retrieve all employees from a table sorted by their last name in ascending order, you would use:
SELECT *
FROM Employees
ORDER BY LastName ASC;
This query returns a list of all employees, organized alphabetically by their last name. To sort in descending order, you would replace `ASC` with `DESC`.
Ascending vs. Descending Order
Understanding the difference between ascending and descending order is crucial. Ascending order arranges data from the smallest to the largest value (or alphabetically from A to Z for strings), while descending order does the opposite, arranging data from largest to smallest (or Z to A for strings). This distinction is vital for presenting data in a way that makes the most sense for the intended analysis or presentation.
2. Sorting by Multiple Columns

Sometimes, sorting by a single column is not sufficient, especially when dealing with large datasets where multiple columns can have the same value. In such cases, you can sort by multiple columns by listing them after the ORDER BY
clause, separated by commas. For instance, to sort employees first by their department and then by their salary in descending order, you would use:
SELECT *
FROM Employees
ORDER BY Department, Salary DESC;
This query first groups employees by their department and then, within each department, sorts them by their salary in descending order. Note that the `DESC` keyword only applies to the `Salary` column; if you want to sort the `Department` in descending order as well, you would need to specify `DESC` for it explicitly.
Using Aggregate Functions with ORDER BY
Aggregate functions like SUM
, AVG
, MAX
, MIN
, and COUNT
can be used in conjunction with ORDER BY
to analyze and sort summarized data. For example, to find the top 5 departments with the highest average salary, you could use:
SELECT Department, AVG(Salary) AS AverageSalary
FROM Employees
GROUP BY Department
ORDER BY AverageSalary DESC
LIMIT 5;
This query calculates the average salary for each department, sorts the departments by this average in descending order, and then returns the top 5 departments with the highest average salaries.
3. Limiting Results with ORDER BY
and LIMIT
The LIMIT
clause is often used in conjunction with ORDER BY
to restrict the number of rows returned in the result-set. This is particularly useful for retrieving the top or bottom records based on certain criteria. For instance, to get the 10 employees with the highest salaries, you would use:
SELECT *
FROM Employees
ORDER BY Salary DESC
LIMIT 10;
This query sorts all employees by their salary in descending order and then returns the first 10 records, which correspond to the 10 highest-paid employees.
Combining ORDER BY
with Other Clauses
In real-world scenarios, ORDER BY
is often combined with other SQL clauses like WHERE
, GROUP BY
, and HAVING
to filter, group, and analyze data more effectively. For example, to find the top 3 sales regions with the highest total sales for the year 2022, you could use:
SELECT Region, SUM(Sales) AS TotalSales
FROM SalesData
WHERE Year = 2022
GROUP BY Region
HAVING SUM(Sales) > 1000000
ORDER BY TotalSales DESC
LIMIT 3;
This query filters sales data for the year 2022, groups it by region, filters out regions with total sales less than $1,000,000, sorts the remaining regions by total sales in descending order, and finally returns the top 3 regions.
4. Handling NULL
Values with ORDER BY
When sorting data, NULL
values can pose a challenge. By default, NULL
values are sorted first in ascending order and last in descending order. However, you can control the placement of NULL
values using the NULLS FIRST
or NULLS LAST
keywords, available in some SQL databases. For example:
SELECT *
FROM Employees
ORDER BY Salary DESC NULLS LAST;
This query sorts employees by their salary in descending order, placing those with `NULL` salaries at the end of the list.
5. Using Indexes to Improve ORDER BY
Performance

For large datasets, sorting can be a resource-intensive operation. Creating indexes on the columns used in the ORDER BY
clause can significantly improve query performance. An index allows the database to quickly locate and retrieve the required data, reducing the time it takes to sort and return the result-set. However, the decision to create an index should be based on the query patterns and data distribution, as indexes can also slow down insert, update, and delete operations.
Clause | Description |
---|---|
ORDER BY | Sorts the result-set in ascending or descending order. |
ASC | Sorts in ascending order (default if not specified). |
DESC | Sorts in descending order. |
LIMIT | Restricts the number of rows returned. |
NULLS FIRST/NULLS LAST | Controls the placement of NULL values in the sorted result-set. |

What is the purpose of the ORDER BY
clause in SQL?
+
The ORDER BY
clause is used to sort the result-set in ascending or descending order. It allows for more organized and meaningful presentation of data, which is crucial for analysis and decision-making.
How do I sort data in descending order using the ORDER BY
clause?
+
To sort data in descending order, you use the DESC
keyword after the column name in the ORDER BY
clause. For example: SELECT * FROM Employees ORDER BY Salary DESC;
Can I use the ORDER BY
clause with aggregate functions?
+
Yes, you can use the ORDER BY
clause with aggregate functions like SUM
, AVG
, MAX
, MIN
, and COUNT
. This is particularly useful for analyzing and sorting summarized data. For example: SELECT Department, AVG(Salary) AS AverageSalary FROM Employees GROUP BY Department ORDER BY AverageSalary DESC;