Tutorialsteacher

Follow Us

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.

EmpIdFirstNameLastNameEmailPhoneNoSalaryDeptId
1'John''King''[email protected]''650.127.1834'330001
2'James''Bond'1
3'Neena''Kochhar''[email protected]''123.456.4568'170002
4'Lex''De Haan''[email protected]''123.000.4569'150001
5'Amit''Patel'180001
6'Abdul''Kalam''[email protected]''123.123.0000'250002

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;
EmpIdFirstNameLastNameEmailPhoneNoSalaryDeptId
6'Abdul''Kalam''[email protected]''123.123.0000'250002
5'Amit''Patel'180001
2'James''Bond'1
1'John''King''[email protected]''650.127.1834'330001
4'Lex''De Haan''[email protected]''123.456.4569'150001
3'Neena''Kochhar''[email protected]''123.456.4568'170002

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;
EmpIdFirstNameLastName
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.

EmpIdFirstNameLastNamePhoneNoHireDateSalaryDeptId
1'John''King''[email protected]''650.127.1834'330001
2'James''Bond'1
4'Lex''De Haan''[email protected]''123.000.4569'150001
5'Amit''Patel'180001
3'Neena''Kochhar''[email protected]''123.456.4568'170002
6'Abdul''Kalam''[email protected]''123.123.0000'250002

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.

EmpIdFirstNameLastNamePhoneNoHireDateSalaryDeptId
5'Amit''Patel'180001
2'James''Bond'1
1'John''King''[email protected]''650.127.1834'330001
4'Lex''De Haan''[email protected]''123.000.4569'150001
6'Abdul''Kalam''[email protected]''123.123.0000'250002
3'Neena''Kochhar''[email protected]''123.456.4568'170002

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
DepartmentNo of Employees
HR2
Finance4