Into Statement  «Prev  Next»
Lesson 8Ordering by more than one Column
Objective Given certain specifications, create a SELECT Statement that correctly sorts the information in a Table.

Ordering by more than one Column

You can further refine your ORDER BY clause by including more than one column as sort criteria. In the example we have been using here, it would be nice if the list was sorted first by last name, then by first name.
Here's the SELECT statement we will use:

SELECT * FROM Customer 
ORDER BY Lastname, Firstname

By adding Firstname, that column will now be considered as a secondary sort order for the results. Here are the results from this query:
LastnameFirstnamePhoneNumberZIP Code
AndrewsJulie309-123-456785744
BrennanJeffrey123-321-567804325
CaitlinElizabeth444-231-564295439
WynsoupJulie201-453-764185744
WynsoupStephen520-555-121285744


Now we have put the first name in its place, you can indicate any number of columns in your sort order, and they can be a mix of types. That is, they can be character data, numeric data, and so on. The engine will take care of sorting the information for you and returning the results.

Example of ordering by FirstName descending:

Given the following People table:
 FirstName |  LastName   |  YearOfBirth
----------------------------------------
  Thomas   | Alva Edison |   1847
  Benjamin | Franklin    |   1706
  Thomas   | More        |   1478
  Thomas   | Jefferson   |   1826

After executing the query below:
SELECT * FROM People 
ORDER BY FirstName DESC, YearOfBirth ASC

The result set will be as follows:
 FirstName |  LastName   |  YearOfBirth
----------------------------------------
  Thomas   | More        |   1478
  Thomas   | Jefferson   |   1826
  Thomas   | Alva Edison |   1847
  Benjamin | Franklin    |   1706
	

Order By Clause - Exercise

Click the Exercise link below to use the ORDER BY clause two different ways.
Order By Clause - Exercise