SQL Order By
The SQL ORDER BY keyword is used when you want to sort the order of your results by a specific column. It can be used in conjunction with any SQL query.
SQL ORDER BY Syntax
FROM “table_name”
WHERE “condition”
ORDER BY “column_name” [ASC|DESC]
By adding the SQL ORDER BY keyword to a SQL query it will order the results based upon the specified column name. You can specifiy more than one column to sort by and you can chose to sort the ordering of your results in either ascending or descending format.
SQL ORDER BY Examples
To demonstrate the above example assume that we have the following table called Games
| ID | Title | Release Date | Genres | Platform |
|---|---|---|---|---|
| 1001 | GTA IV | April 29, 2008 | Action-adventure | PS3,XBOX360 |
| 1002 | Gran Turismo 5 | November 24, 2010 | Racing | PS3 |
| 1003 | Assassin’s Creed | November 14, 2007 | Third person | PS3,XBOX360 |
| 1004 | Battlefield: Bad Company | June 26, 2008 | First person shooter | PS3,XBOX360 |
| 1005 | LittleBigPlanet | October 01, 2008 | Puzzle platformer | PS3 |
To SELECT all the records from the Games table and ORDER BY the Title we would use the following query:
FROM Games
ORDER BY Title
Once executed the resultset would look like this:
| ID | Title | Release Date | Genres | Platform |
|---|---|---|---|---|
| 1003 | Assassin’s Creed | November 14, 2007 | Third person | PS3,XBOX360 |
| 1004 | Battlefield: Bad Company | June 26, 2008 | First person shooter | PS3,XBOX360 |
| 1002 | Gran Turismo 5 | November 24, 2010 | Racing | PS3 |
| 1001 | GTA IV | April 29, 2008 | Action-adventure | PS3,XBOX360 |
| 1005 | LittleBigPlanet | October 01, 2008 | Puzzle platformer | PS3 |
The results have been alphabetically sorted by the Title values. As no sort direction was supplied in the SQL query it by default sorted the results in ascending format. As the above example shows you how simple it is to use the SQL ORDER BY keyword in your query.
Earlier I mentioned that you can sort your data by specifying multiple columns and below is an example of sorting on two columns (Platform and ID).
FROM Games
ORDER BY [Platform], ID
Once executed the resultset would look like this:
| ID | Title | Release Date | Genres | Platform |
|---|---|---|---|---|
| 1002 | Gran Turismo 5 | November 24, 2010 | Racing | PS3 |
| 1005 | LittleBigPlanet | October 01, 2008 | Puzzle platformer | PS3 |
| 1001 | GTA IV | April 29, 2008 | Action-adventure | PS3,XBOX360 |
| 1003 | Assassin’s Creed | November 14, 2007 | Third person | PS3,XBOX360 |
| 1004 | Battlefield: Bad Company | June 26, 2008 | First person shooter | PS3,XBOX360 |
First the results are sorted by Platform and the grouped results are then sorted by ID.
This SQL ORDER BY example is exactly the same as the first example above but I have specified that the results be sorted in descending order. Notice the keyword DESC applied after the specified column name:
FROM Games
ORDER BY Title DESC
Once executed the resultset would look like this:
| ID | Title | Release Date | Genres | Platform |
|---|---|---|---|---|
| 1005 | LittleBigPlanet | October 01, 2008 | Puzzle platformer | PS3 |
| 1001 | GTA IV | April 29, 2008 | Action-adventure | PS3,XBOX360 |
| 1002 | Gran Turismo 5 | November 24, 2010 | Racing | PS3 |
| 1004 | Battlefield: Bad Company | June 26, 2008 | First person shooter | PS3,XBOX360 |
| 1003 | Assassin’s Creed | November 14, 2007 | Third person | PS3,XBOX360 |
Give it a go and once you feel comfortable using the SQL ORDER BY keyword move on to our next tutorial SQL INSERT.