How to Use SQL ORDER BY with Multiple Columns
James Reed
Infrastructure Engineer · Leapcell

Key Takeaways
- The
ORDER BY
clause can sort results by multiple columns. - The order and direction of columns affect the final output.
- Using multiple columns provides more precise data organization.
When working with databases, sorting data in a meaningful order is often necessary for analysis, reporting, or user presentation. SQL provides the ORDER BY
clause to sort query results. While sorting by a single column is straightforward, there are many cases where sorting by multiple columns is required. This article explains how to use ORDER BY
with multiple columns in SQL, why it is useful, and provides examples to illustrate its usage.
Why Sort by Multiple Columns?
Sorting by multiple columns can help you create more organized and easily interpretable results. For example, if you have a list of employees and want to order them first by department and then by salary, using multiple columns in the ORDER BY
clause ensures that your data is grouped and sorted precisely the way you need.
Syntax of ORDER BY with Multiple Columns
The basic syntax to sort by multiple columns in SQL is:
SELECT column1, column2, ... FROM table_name ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;
column1
,column2
, etc., are the columns by which you want to sort.ASC
means ascending order (default), andDESC
means descending order.- You can mix ascending and descending orders as needed.
Example: Sorting by Two Columns
Suppose you have a table named employees
with the following columns: department
, last_name
, and salary
. You want to sort employees by department (alphabetically) and, within each department, by salary (highest to lowest).
SELECT department, last_name, salary FROM employees ORDER BY department ASC, salary DESC;
In this example:
- The results are first sorted by the
department
column in ascending order. - Within each department, rows are further sorted by the
salary
column in descending order.
Example: Sorting by More Than Two Columns
You can add as many columns as needed to the ORDER BY
clause. Here is an example with three columns:
SELECT department, last_name, first_name, salary FROM employees ORDER BY department, last_name, first_name;
This query will:
- Sort all employees by
department
first. - For employees within the same department, sort them by
last_name
. - If there are employees with the same last name in a department, they are sorted by
first_name
.
Practical Tips
- The order of columns in the
ORDER BY
clause matters. SQL will sort by the first column, then by the second only when there are ties, and so on. - If you don’t specify
ASC
orDESC
, SQL will use ascending order by default. - You can use column numbers in the
ORDER BY
clause (e.g.,ORDER BY 2, 3 DESC
), but using column names is recommended for readability.
Conclusion
Sorting by multiple columns in SQL using the ORDER BY
clause is a powerful way to control the order of your query results. By specifying one or more columns and the direction for each, you can tailor your data presentation to fit almost any requirement. Mastering this technique is essential for effective data querying and reporting in SQL.
FAQs
Yes, you can specify ASC or DESC for each column individually.
SQL sorts by the next column specified in the ORDER BY clause.
No, but it is recommended for readability; column numbers can also be used.
We are Leapcell, your top choice for hosting backend projects.
Leapcell is the Next-Gen Serverless Platform for Web Hosting, Async Tasks, and Redis:
Multi-Language Support
- Develop with Node.js, Python, Go, or Rust.
Deploy unlimited projects for free
- pay only for usage — no requests, no charges.
Unbeatable Cost Efficiency
- Pay-as-you-go with no idle charges.
- Example: $25 supports 6.94M requests at a 60ms average response time.
Streamlined Developer Experience
- Intuitive UI for effortless setup.
- Fully automated CI/CD pipelines and GitOps integration.
- Real-time metrics and logging for actionable insights.
Effortless Scalability and High Performance
- Auto-scaling to handle high concurrency with ease.
- Zero operational overhead — just focus on building.
Explore more in the Documentation!
Follow us on X: @LeapcellHQ