SQL Server Order BY Clause
⦁ Sorting data is a crucial aspect of data analysis and management, and Microsoft SQL Server provides an efficient way to sort data using the ORDER BY clause. In this blog, we will discuss how to sort data in MS SQL and the syntax for the ORDER BY clause, along with some examples.
Syntax for the ORDER BY clause:
⦁ The ORDER BY clause is used to sort the data in ascending or descending order, based on one or more columns. The basic syntax for the ORDER BY clause is as follows:
Syntax:
SELECT Column1, Column2, …, ColumnN
FROM Table_Name
ORDER BY
column1 [ASC | DESC],
column2 [ASC | DESC], …,
columnN [ASC | DESC];
⦁ In the above syntax, the SELECT statement selects one or more columns from the specified table. The ORDER BY clause sorts the selected data in ascending (ASC) or descending (DESC) order. The columns specified in the ORDER BY clause should also be included in the SELECT statement.
Examples:
Let’s consider a sample table named ‘Employees’ with the following data:

⦁ Sorting by a Single Column:
The following query sorts the Employees table by the ‘Salary’ column in ascending order:
SELECT * FROM Employees ORDER BY Salary ASC ;
This query will return the following result:

⦁ Sorting by Multiple Columns:
We can also sort data by multiple columns. The following query sorts the Employees table by the ‘Age’ and ‘Salary’ columns in ascending order:
SELECT * FROM Employees ORDER BY Age ASC, Salary ASC;
This query will return the following result:

⦁ Sorting by Descending Order:
To sort data in descending order, we can use the DESC keyword. The following query sorts the Employees table by the ‘Salary’ column in descending order:
SELECT * FROM Employees ORDER BY Salary DESC;
This query will return the following result:
