You are currently viewing How To Sort Data Using Order By Clause in SQL

How To Sort Data Using Order By Clause in SQL

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