It is assumed that you have gone through the previous tutorials and have a basic understanding of it. It is
also assumed that you have practised on the previous examples shown so that you have grasped the syntax notations
so that writing simple queries is possible by yourself.
Here the 'ORDER
BY'
clause is executed on the 'Employees' table.
The ORDER BY clause
The 'ORDER
BY" clause sorts the results either in ascending or descending order. By default it is ascending unless specified.
It is used in SELECT statements. You can use more than one column in the ORDER BY clause.
The only criteria is that whatever columns you are ordering by, it should be present in the list of columns that you are displaying.
SQL ORDER BY clause in Select Statements.
Select
column_name1,column_name2,....
FROM
table_name
where condition
Order By column_name3,column_name4....
The SELECT Statement Example with ORDER BY clause
The Select statement on the 'Employees' table below displays all the employees.
1 |
Richard Hughes |
4/23/1945 12:00:00 AM |
1 |
1 |
New Orleans |
2 |
Taryn Sinclair |
3/22/1980 12:00:00 AM |
2 |
2 |
San Francisco |
3 |
Ted Horowitz |
3/31/1960 12:00:00 AM |
3 |
4 |
New York |
4 |
Jonathan Douglas |
7/7/1971 12:00:00 AM |
3 |
4 |
Salt Lake City |
5 |
Miranda Leigh |
7/19/1983 12:00:00 AM |
7 |
3 |
New York |
6 |
Jana Rae |
9/23/1976 12:00:00 AM |
8 |
2 |
Houston |
7 |
Lita Rosanna |
9/14/1982 12:00:00 AM |
4 |
6 |
Long Island |
8 |
Colin Flooks |
12/29/1988 12:00:00 AM |
4 |
2 |
Salt Lake City |
9 |
Anthony Frank |
2/22/1988 12:00:00 AM |
9 |
3 |
San Francisco |
10 |
Stephanie Lynn |
7/30/1979 12:00:00 AM |
8 |
2 |
Salt Lake City |
11 |
Jack Martin |
8/25/1985 12:00:00 AM |
NULL |
NULL |
Austin |
The Select statement on the 'Employees' table below displays all the employees but it is arranged according to the employee names in ascending order
as shown
below.
Select EmployeeID,EmployeeName,DateOfBirth,DesignationID,DeptID,City
from
Employees
ORDER BY EmployeeName
9 |
Anthony Frank |
2/22/1988 12:00:00 AM |
9 |
3 |
San Francisco |
8 |
Colin Flooks |
12/29/1988 12:00:00 AM |
4 |
2 |
Salt Lake City |
11 |
Jack Martin |
8/25/1985 12:00:00 AM |
NULL |
NULL |
Austin |
6 |
Jana Rae |
9/23/1976 12:00:00 AM |
8 |
2 |
Houston |
4 |
Jonathan Douglas |
7/7/1971 12:00:00 AM |
3 |
4 |
Salt Lake City |
7 |
Lita Rosanna |
9/14/1982 12:00:00 AM |
4 |
6 |
Long Island |
5 |
Miranda Leigh |
7/19/1983 12:00:00 AM |
7 |
3 |
New York |
1 |
Richard Hughes |
4/23/1945 12:00:00 AM |
1 |
1 |
New Orleans |
10 |
Stephanie Lynn |
7/30/1979 12:00:00 AM |
8 |
2 |
Salt Lake City |
2 |
Taryn Sinclair |
3/22/1980 12:00:00 AM |
2 |
2 |
San Francisco |
3 |
Ted Horowitz |
3/31/1960 12:00:00 AM |
3 |
4 |
New York |
The Select statement on the 'Employees' table below displays all the employees but it is arranged according to the employee names in descending order
as shown below. To do this just add the word DESC to the end of the orderby column name.
Select EmployeeID,EmployeeName,DateOfBirth,DesignationID,DeptID,City
from Employees
ORDER BY EmployeeName desc
3 |
Ted Horowitz |
3/31/1960 12:00:00 AM |
3 |
4 |
New York |
2 |
Taryn Sinclair |
3/22/1980 12:00:00 AM |
2 |
2 |
San Francisco |
10 |
Stephanie Lynn |
7/30/1979 12:00:00 AM |
8 |
2 |
Salt Lake City |
1 |
Richard Hughes |
4/23/1945 12:00:00 AM |
1 |
1 |
New Orleans |
5 |
Miranda Leigh |
7/19/1983 12:00:00 AM |
7 |
3 |
New York |
7 |
Lita Rosanna |
9/14/1982 12:00:00 AM |
4 |
6 |
Long Island |
4 |
Jonathan Douglas |
7/7/1971 12:00:00 AM |
3 |
4 |
Salt Lake City |
6 |
Jana Rae |
9/23/1976 12:00:00 AM |
8 |
2 |
Houston |
11 |
Jack Martin |
8/25/1985 12:00:00 AM |
NULL |
NULL |
Austin |
8 |
Colin Flooks |
12/29/1988 12:00:00 AM |
4 |
2 |
Salt Lake City |
9 |
Anthony Frank |
2/22/1988 12:00:00 AM |
9 |
3 |
San Francisco |