Sorting Results with ORDER BY
Introduction
In SQL, sorting results with ORDER BY is one of the most powerful techniques to organize and present data clearly. Whether you’re a student learning SQL or a professional working with databases or Power BI, the ORDER BY clause is essential for displaying sorted results.
In this article, we will break down:
- How to sort in ascending and descending order
- How to perform multi-column sorting
- Common mistakes to avoid
Let’s explore these concepts with examples.
What is ORDER BY in SQL?
The ORDER BY clause is used to sort the output of your query by one or more columns. By default, sorting results with ORDER BY happens in ascending order unless explicitly specified.
Sorting Results: ASC vs DESC in SQL
- ASC – Ascending order (A to Z, 0 to 9) — default
- DESC – Descending order (Z to A, 9 to 0)
Example use cases:
- Sort products by price from low to high
- List customers from Z to A
ORDER BY for Multi-Column Sorting in SQL
You can sort data by multiple columns. SQL applies the sort from left to right.
Use cases:
- Sort employees by department first, then by salary
- Sort invoices by due date and then by amount
SELECT * FROM customers
ORDER BY name ASC;
SELECT * FROM orders
ORDER BY total_amount DESC;
SELECT * FROM employees
ORDER BY department ASC, salary DESC;
Conclusion
Sorting your results is an essential part of organizing and analyzing data efficiently. The ORDER BY clause gives you control over how your data appears, helping you create meaningful insights, reports, and dashboards.
Practice sorting with real datasets to strengthen your SQL skills.
Internal Blog References
Join Our Course
Want to learn SQL hands-on with real business use cases?
Join FaisalSir.com’s Data Analytics Course and start your tech journey now!
Q1: What is the default sorting order in SQL?
By default, SQL sorts results in ASC (ascending) order unless DESC is specified.
Q2: Can I sort by column that is not in SELECT?
Yes, you can use ORDER BY even if the column isn’t shown in SELECT, as long as it’s in the table.