ORDER BY can contain multiple columns, allowing SQL to apply a secondary or additional sort when values in the previous column are equal.
Multiple-column sorting is useful when reports need consistent ordering across several business attributes.
Sort employees first by department and then by salary from highest to lowest.
SELECT
EmployeeName,
Department,
Salary
FROM Employees
ORDER BY
Department ASC,
Salary DESC;
Analysts use multiple-column sorting for employee reports, sales reports, customer lists, product analysis, and operational reporting.
- ORDER BY can contain multiple columns.
- The first column is the primary sort.
- The next column breaks ties.
- Each column can have its own sort direction.
- Multiple-column sorting creates more organized results.
Can ORDER BY contain multiple columns?
Yes. Multiple columns can be specified to create primary and secondary sorting.
What happens when two rows have the same first sort value?
SQL uses the next ORDER BY column to determine their relative order.