ORDER BY sorts the final result set using one or more columns or expressions. After a JOIN, columns from any available table can generally be used for sorting.
Sorted results make reports easier to understand and help analysts identify highest, lowest, earliest, or latest records.
Display customer orders from the highest order amount to the lowest.
SELECT
c.CustomerName,
o.OrderID,
o.OrderAmount
FROM Customers c
INNER JOIN Orders o
ON c.CustomerID = o.CustomerID
ORDER BY
o.OrderAmount DESC;
Used in reports, dashboards, transaction analysis, customer ranking, and operational monitoring.
- ORDER BY sorts the final result.
- Joined table columns can be used for sorting.
- ASC sorts ascending.
- DESC sorts descending.
- Multiple sort columns can be specified.
Can ORDER BY use columns from joined tables?
Yes. Columns available in the query result can generally be used in ORDER BY.
What is the difference between ASC and DESC?
ASC sorts from lower to higher or earlier to later, while DESC sorts from higher to lower or later to earlier.