|
ORDER BY clause is used to order the data sets retrieved from a SQL database. The ordering of the selected data can be done by one or more columns in a table. If we want to sort our Users table by the FirstName column, we'll have to use the following ORDER BY SQL statement:
SELECT * FROM Users ORDER BY FirstName;
The result of the ORDER BY statement above will be the following:
FirstName |
LastName |
DateOfBirth |
Email |
City |
David |
Stonewall |
01/03/1954 |
david@sql-tutorial.com |
San Francisco |
John |
Smith |
12/12/1969 |
john.smith@john-here.com |
New York |
Paul |
O'Neil |
09/17/1982 |
paul.oneil@pauls-email.com |
New York |
Stephen |
Grant |
03/03/1974 |
sgrant@sgrantemail.com |
Los Angeles |
Susan |
Grant |
03/03/1970 |
susan.grant@sql-tutorial.com |
Los Angeles |
As you can see the rows are ordered alphabetically by the FirstName column. You can use ORDER BY to order the retrieved data by more than one column. For example, if you want to order by both LastName and City columns, you would do it with the following ORDER BY statement:
SELECT * FROM Users ORDER BY LastName, DateOfBirth;
Here is the result of this ORDER BY statement:
FirstName |
LastName |
DateOfBirth |
Email |
City |
Susan |
Grant |
03/03/1970 |
susan.grant@sql-tutorial.com |
Los Angeles |
Stephen |
Grant |
03/03/1974 |
sgrant@sgrantemail.com |
Los Angeles |
Paul |
O'Neil |
09/17/1982 |
paul.oneil@pauls-email.com |
New York |
John |
Smith |
12/12/1969 |
john.smith@john-here.com |
New York |
David |
Stonewall |
01/03/1954 |
david@sql-tutorial.com |
San Francisco |
When using ORDER BY with more than one column, you need to separate the list of columns following ORDER BY with commas. What will happen if we reverse the order of the columns specified after the ORDER BY statement like in the statement below?
SELECT * FROM Users ORDER BY DateOfBirth, LastName;
This ORDER BY statement will return the same results as the one with the reversed columns order, but they will be ordered differently. Here is the result:
FirstName |
LastName |
DateOfBirth |
Email |
City |
David |
Stonewall |
01/03/1954 |
david@sql-tutorial.com |
San Francisco |
John |
Smith |
12/12/1969 |
john.smith@john-here.com |
New York |
Susan |
Grant |
03/03/1970 |
susan.grant@sql-tutorial.com |
Los Angeles |
Stephen |
Grant |
03/03/1974 |
sgrant@sgrantemail.com |
Los Angeles |
Paul |
O'Neil |
09/17/1982 |
paul.oneil@pauls-email.com |
New York |
The ORDER BY clause first sorts the retrieved data by the first column, then the next one, and so forth. In all the ORDER BY examples so far, we were sorting alphabetically for character columns (FirstName, LastName) and from earlier to later date for the DateOfBirth column. What do we do if we want to order our data alphabetically but this time backwards? In order to accomplish that we need to use the DESC SQL keyword:
SELECT * FROM Users ORDER BY FirstName DESC;
Here is the result:
FirstName |
LastName |
DateOfBirth |
Email |
City |
Susan |
Grant |
03/03/1970 |
susan.grant@sql-tutorial.com |
Los Angeles |
Stephen |
Grant |
03/03/1974 |
sgrant@sgrantemail.com |
Los Angeles |
Paul |
O'Neil |
09/17/1982 |
paul.oneil@pauls-email.com |
New York |
John |
Smith |
12/12/1969 |
john.smith@john-here.com |
New York |
David |
Stonewall |
01/03/1954 |
david@sql-tutorial.com |
San Francisco |
|