Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

Take this example:

SELECT SalesOrderID,
       CustomerID,
       SalesPersonID,
       OrderDate
FROM Sales.SalesOrderHeader
ORDER BY OrderDate,
         SalesOrderID DESC
OFFSET 0 ROWS
FETCH NEXT 10 ROWS ONLY;

I understand ORDER BY when one column is specified but how does the ORDER BY clause order the table by two or three columns? Which one is ordered first? What if the highest value in the SalesOrderID column does not correlate with the highest value in the OrderDate column?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

Think of the way telephone directories are (typically) ordered.

If they were generated by an SQL query it would be

ORDER BY LastName ASC,
         FirstName ASC

The names are ordered by LastName first.

The secondary column is only used for ordering within groups of people sharing the same last name.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.