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


  

Section Menu 

Are you a University or College bookstore and want to order multiple books?  Email us at alphapress@hotmail.com or call us at 518-250-3890.  We will be happy to talk to you.  

Are you a professor and you need a sample copy?  We will be happy to send you a pdf or printed copy!  Email us at alphapress@hotmail.com.

Are you outside the United States?  We can ship multiple books worldwide in most countries.

  

 

 

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: