SQL Server ORDER BY Query

SQL Server ORDER BY

In SQL Server, ORDER BY clause is used to sort the rows in either ascending or descending order.

ORDER BY Syntax

SELECT column_name1[, column_name2, ....]
FROM table_name
[ORDER BY column_name | expression [ASC | DESC]
[, column_name | expression [ASC | DESC]...]

ASC and DESC are the sorting order, ASC is the ascending order and DESC is the descending order.

ASC is the default sort order.

Employee Table

Let us consider the example 'Employee' database table.

Employee table output

ORDER BY ASC Example

To get the rows from Employee table in ascending order based on Country column.

SELECT Name, DeptId, City, Country, Email, Salary
FROM Employee
ORDER BY Country ASC
Output:
ORDER BY ASC output

To get the rows from Employee table in ascending order based on Country and City columns.

SELECT Name, DeptId, City, Country, Email, Salary
FROM Employee
ORDER BY Country, City ASC
Output:
ORDER BY ASC output

ORDER BY DESC Example

To get the rows from Employee table in descending order based on Country column.

SELECT Name, DeptId, City, Country, Email, Salary
FROM Employee
ORDER BY Country DESC
Output:
ORDER BY DESC output

To get the rows from Employee table in descending order based on Country and City columns.

SELECT Name, DeptId, City, Country, Email, Salary
FROM Employee
ORDER BY Country, City DESC
Output:
ORDER BY DESC output



Python installation

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^