How To Sorting SQL Order BY Multiple Columns With Practical Example

Summary: in this tutorial, you will learn How To Sorting SQL Order BY Multiple Columns With Practical Example, to sort the result set of a query by one, two or Multiple columns.

Introduction To SQL ORDER BY Multiple Columns

SQL ORDER BY multiple columns is used when you need to sort the data at multiple levels from the existing table.
The clause ORDER BY multiple columns works with SELECT statement only.

Syntax:

SELECT column_name1, column_name2, column_name3,….column_nameN
FROM table_name
WHERE Condition
ORDER BY column_name1, column_name2, …. (ASC OR DESC);
  • column_name1, column_name2, ….., columnN– It specifies the column names used to fetch from table
  • table_name– It specifies the name of the table.

ORDER BY SQL Example

Let us understand How To Order BY Multiple Columns In SQL in SQL, first of all, consider the given below table(s) as example table(s) to frame the SQL query for getting the desired results.

Demo Table: Employee_Details

EmpID EmpName Designation ManagerId Date_Of_Joining Dept_Id Emp_Salary
1001 Aaron Brayden Director 2018-04-01 101 54000
1002 Ethan Driss Manager Emp1 2018-04-03 104 42000
1003 Coinneach Dev Director 2018-04-05 100 52000
1004 Devin Diego Manager Emp2 2018-04-11 104 32000
1005 Sam Rastogi Developer Emp3 2018-04-14 101 30000
1006 Aniket Singh Designer Emp4 2018-04-16 100 30000
1007 Aditya Shukla Sales Manager Emp5 2018-04-21 104 35000
1008 Saumya Tripathi Clark Emp6 2018-04-28 101 25000
1009 Umakant Debey Analyst Emp7 2018-05-02 100 28000
1010 Anand Singh Salesman Emp8 2018-05-10 101 32000
1011 Pradeep Gupta HR Emp9 2018-05-15 104 35000

A) Scenario:- Let us fetch rows by sorting multiple rows in ASC (ascending) and DESC (descending) order.

Explanation:- Requirement- Fetch EmpName, Designation, Dept_Id, Emp_Salary details in ascending order of EmpName and descending order of Emp_Salary. The given below query as follows-


sql order by multiple columns

B) Scenario:- Let us fetch rows by sorting multiple rows in ASC (ascending) order.

Explanation:- Requirement- Fetch EmpName, Designation, Dept_Id, details in ascending order of EmpName and Dept_Id. The given below query as follows-


SQL sorting multiple rows in ascending order

C) Scenario:- Let us fetch rows by sorting multiple rows in DESC (decending) order.

Explanation:- Requirement- Fetch EmpID, EmpName, Date_Of_Joining, Emp_Salary details of employees whose Dept_Id is 104 and descending order of Date_Of_Joining, Emp_Salary.


SQL sorting multiple rows in ascending order

D) How to Order By Two Columns in SQL?

Explanation:- Here, you need to display the records from a given table (Employee_Details) sorted by two columns.
In our database has a table named Employee_Details with the following columns: EmpID, EmpName, Designation, ManagerId, Date_Of_Joining, Dept_Id, Emp_Salary.
Furthermore, let’s display all records for each employee but here we sort the records according to Emp_Salary in descending order first and then by EmpName in ascending order.

Thus, the above query returns sorted records according to two columns: Emp_Salary and EmpName.

sorting Order By Two Columns in SQL

Explanation:

you are free to select all columns records from a table (i.e. Employee_Details) but would like to see them sorted according to two columns i.e. Emp_Salary, EmpName you can do so with ORDER BY. as you can see in the SQL query this clause comes at the end.

After the ORDER BY keyword, add the column name by which you’d like to sort records first (in our example, Emp_Salary). Then, after comes a comma, add the 2nd column name (in our example, EmpName). You can modify the sorting order (ASC or DESC) separately for each column. If you want to use ascending (low to high) order, then you will be using the ASC keyword; this keyword is optional because it is the default order when none is specified. furthermore, if you like to use descending order, then you will be using the DESC keyword after the appropriate column (we used descending order for the Emp_Salary column).

In our demonstration, we 1st sorted the result by Emp_Salary in descending order (i.e. higher salaries to lower ones) and then by EmpName in ascending order within those already sorted records.

Conclusion:

In this article, you have learned How To Sorting SQL Order BY Multiple Columns With Practical Example to sort the result set of a query by one, two or Multiple columns. I hope you will enjoy it!