SQL - ORDER BY Clause

The ORDER BY clause can be used in the SELECT query to sort the result in ascending or descending order of one or more columns.

Syntax:

SELECT column1, column2,...columnN 
FROM table_name
[WHERE]
[GROUP BY]
[HAVING]
[ORDER BY column(s) [ASC|DESC]]
ORDER BY Characteristics:
  • The ORDER BY clause is used to get the sorted records on one or more columns in ascending or descending order.
  • The ORDER BY clause must come after the WHERE, GROUP BY, and HAVING clause if present in the query.
  • Use ASC or DESC to specify the sorting order after the column name. Use ASC to sort the records in ascending order or use DESC for descending order. By default, the ORDER BY clause sort the records in ascending order if the order is not specified.

For the demo purpose, we will use the following Employee in all examples.

EmpId FirstName LastName Email PhoneNo Salary DeptId
1 'John' 'King' '[email protected]' '650.127.1834' 33000 1
2 'James' 'Bond' 1
3 'Neena' 'Kochhar' '[email protected]' '123.456.4568' 17000 2
4 'Lex' 'De Haan' '[email protected]' '123.000.4569' 15000 1
5 'Amit' 'Patel' 18000 1
6 'Abdul' 'Kalam' '[email protected]' '123.123.0000' 25000 2

The following query will fetch all the records from the Employee table and sorts the result in ascending order of the FirstName values.

SQL Script: Select Query with QRDER BY Clause
SELECT * FROM Employee
ORDER BY FirstName;
EmpId FirstName LastName Email PhoneNo Salary DeptId
6 'Abdul' 'Kalam' '[email protected]' '123.123.0000' 25000 2
5 'Amit' 'Patel' 18000 1
2 'James' 'Bond' 1
1 'John' 'King' '[email protected]' '650.127.1834' 33000 1
4 'Lex' 'De Haan' '[email protected]' '123.456.4569' 15000 1
3 'Neena' 'Kochhar' '[email protected]' '123.456.4568' 17000 2

The following query will return rows in the descending order of the FirstName value.

SQL Script: Sorting in Descending Order
SELECT EmpId, FirstName, LastName FROM Employee
ORDER BY FirstName DESC;
EmpId FirstName LastName
3 'Neena' 'Kochhar'
4 'Lex' 'De Haan'
1 'John' 'King'
2 'James' 'Bond'
5 'Amit' 'Patel'
6 'Abdul' 'Kalam'

Sort by Multiple Columns

The ORDER BY clause can include multiple columns in different sorting order (ascending or descending). When you include multiple columns with the ORDER BY clause, it will sort the records based on the first column, and if any two or more records have the same value in the first ORDER BY column, it will sort them by the second ORDER BY column.

To understand this, first, sort the result by DeptId column, as shown below.

SQL Script: QRDER BY Clause
SELECT * FROM Employee
ORDER BY DeptId;

The above query would display the following result.

EmpId FirstName LastName PhoneNo HireDate Salary DeptId
1 'John' 'King' '[email protected]' '650.127.1834' 33000 1
2 'James' 'Bond' 1
4 'Lex' 'De Haan' '[email protected]' '123.000.4569' 15000 1
5 'Amit' 'Patel' 18000 1
3 'Neena' 'Kochhar' '[email protected]' '123.456.4568' 17000 2
6 'Abdul' 'Kalam' '[email protected]' '123.123.0000' 25000 2

Now, include the FirstName column in the ORDER BY clause.

SQL Script: Multiple Columns in QRDER BY Clause
SELECT * FROM Employee
ORDER BY DeptId, FirstName;

The above query will first sort the result by DeptId, and then the rows having the same DeptId will be sorted by the FirstName. Remember, we have not included ASC or DESC, So it will sort the result in ascending order by default, as shown below.

EmpId FirstName LastName PhoneNo HireDate Salary DeptId
5 'Amit' 'Patel' 18000 1
2 'James' 'Bond' 1
1 'John' 'King' '[email protected]' '650.127.1834' 33000 1
4 'Lex' 'De Haan' '[email protected]' '123.000.4569' 15000 1
6 'Abdul' 'Kalam' '[email protected]' '123.123.0000' 25000 2
3 'Neena' 'Kochhar' '[email protected]' '123.456.4568' 17000 2

Sort Group of Records

The following query sorts the group of records.

SQL Script: GROUP BY
SELECT dept.Name as 'Department', count(emp.empid) as 'No of Employees'
FROM Employee emp, Department dept
WHERE emp.deptid = dept.DeptId
GROUP by dept.Name
ORDER BY dept.Name DESC
Department No of Employees
HR 2
Finance 4