AlphaPress Publishing
P.O. Box 5942 Albany, NY 12205
Tel: 518-250-3890
Email: alphapress@hotmail.com  


  
Click to buy this book from Amazon

Section Menu 

News

Pindaro is now working on a comprehensive book on SQL Server 2014.  For any comments you can email the author at alphapress@hotmail.com.

  

 

 

Use the ORDER BY clause to sort multiple columns

To contact the author click here

This article is from the book "Access 2007 Pure SQL

To download the sample database click here


Use the ORDER BY clause to sort multiple columns

Create a customer list sorting by city and last name simultaneously

Discussion:

We can sort records on the values of multiple columns.  For example, we might want to obtain a report which will include a list of customers sorted first by city and then by last name.  In this case, we can look at our customers by city and then by last name.  To explain this point further, let us say we have some customers in Albany and some customers in Boston.  

Sorting ascending on both city and last name fields will create the following result:  The records with the city of Albany will appear first in the result set followed by the records with the city of Boston.  Now, within the Albany records, customers will be sorted alphabetically and in ascending order.  Within the city of Boston records, customers will again be sorted alphabetically and in ascending order.  You should not be surprised to see a customer from Boston whose last name starts with an "A" being listed after a customer in Albany whose last name starts with a "Z".    

Code:

SELECT city, lastname, firstname

FROM Customers

ORDER BY city, lastname ASC
 

Result: