How To Sort Data Using Order By Clause in SQL
Order By clause is used to sort the retrieved data in ascending or descending order. The data stored in a table may not be arranged while you want to retrieve it in ascending or descending order, then Order By clause is used to sort data. In select statement, by default, data retrieved in ascending order. To get the data in descending order, we use DESC along with Order By.
In this demo, we create a table tblEmployee and sort it in ascending or descending order using ORDER BY clause.

Let’s insert some rows in the table tblEmployee.

We have table tblEmployee with data as below.

ORDER BY with Ascending Order
Let’s arrange the table data by EmpFirstName in ascending order, we get the result as below. In the below example, ASC is optional.

ORDER BY with Descending Order
Let’s arrange the table data by EmpFirstName in descending order, we get the result as below.

ORDER BY with multiple columns
We can also arrange data with multiple columns as shown below.

Recommended Readings
- Advanced SQL Queries For Practice With Solution
- SQL Queries For Practice With Solution
- SQL Interview Questions and Answers
- STORED PROCEDURE in SQL Server
- How To Join Tables Data in SQL Server
- How to use Transaction in SQL Stored Procedure
- Difference Between IN and NOT IN Operators in SQL
- How To Modify Date in SQL Using DATEADD
- How To Get Year From Date in SQL Server
- How To Get Month From Date in SQL Server
- How To Get Day From Date in SQL Server
- How To Use ROW_NUMBER Function in SQL
- Date and Time Functions in SQL Server
- How To Find Nth Highest Salary in SQL Server
- How to Backup Table Using SELECT INTO Statement
- How To Use HAVING Clause in SQL Server
- Aggregate Functions in SQL Server
- How To Group Data Using Group By in SQL Server
- How To Truncate Table in SQL Server
- How To Delete Data From Table in SQL Server
- How To Update Table Data in SQL Server
- How To Sort Data Using Order By Clause in SQL
- How To Select Distinct Records in SQL Server
- How to Filter Data From Table in SQL Server
- Round Off Values Using Ceiling and Floor in SQL
- How To Find Square Root Of A Number in SQL Server
- How To Select Data From Table in SQL Server
- How To Insert Data in SQL Server Table
- How To Add NOT NULL Constraint in SQL Server
- How To Add Check Constraint on SQL Server Table
- How To Add Default Constraint on SQL Server Table
- Unique Key Constraint in SQL Server
- How to add Foreign Key Constraint in SQL Server
- How To Add Identity To SQL Server Table Column
- How to add Primary Key Constraint in SQL Server
- How To Create Alter and Drop Table in SQL Server
- How To Create Alter and Drop Database in SQL