Beginner

349. Multiple Column Sorting

Learn how to sort query results using more than one column.

📘 Lesson 349 of 728 ⏱ 10 min 🎯 Intermediate

Multiple-column sorting allows SQL to sort rows by a primary column and then use additional columns to determine the order of rows with equal values.

Real-world reports often need consistent secondary sorting when multiple records have the same primary value.

Sort employees by department alphabetically and then by salary from highest to lowest within each department.

SQL
SELECT
    EmployeeName,
    Department,
    Salary
FROM Employees
ORDER BY
    Department ASC,
    Salary DESC;

Multiple-column sorting is useful for employee reports, sales reports, customer lists, transaction analysis, and operational dashboards.

  • Multiple columns can be used in ORDER BY.
  • The first column has the highest sorting priority.
  • The next column breaks ties.
  • Each column can have its own ASC or DESC direction.
  • It helps create consistent reports.
How does multiple-column ORDER BY work?

SQL sorts by the first column and uses subsequent columns to determine the order of rows with equal values.

Can different columns use different sort directions?

Yes. Each ORDER BY column can specify its own ASC or DESC direction.

Have you finished this lesson?

Introduction to SQL – All Lessons