SQL Server Tutorial SQL Server Advanced SQL Server Database SQL Server References

SQL Server - DESC Keyword



The SQL Server (Transact-SQL) ORDER BY statement is used to sort the result table in ascending or descending order. By default, ORDER BY keyword sorts the result in ascending order, however it can be specified using ASC keyword. To sort the result in descending order, DESC keyword is used.

Syntax

The syntax for using ORDER BY statement in SQL Server (Transact-SQL) is given below:

SELECT column1, column2, column3, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

Example:

Consider a database table called Employee with the following records:

EmpIDNameCityAgeSalary
1JohnLondon253000
2MarryNew York242750
3JoParis272800
4KimAmsterdam303100
5RameshNew Delhi283000
6HuangBeijing282800

  • To fetch the Employee table sorted by Age (descending order), the query is:

    SELECT * FROM Employee
    ORDER BY Age DESC;
    

    This will produce the result as shown below:

    IDNameCityAgeSalary
    4KimAmsterdam303100
    5RameshNew Delhi283000
    6HuangBeijing282800
    3JoParis272800
    1JohnLondon253000
    2MarryNew York242750
  • To fetch all fields of the Employee table sorted by Age (ascending order) and Salary (descending order), the query will be:

    SELECT * FROM Employee
    ORDER BY Age ASC, Salary DESC;
    

    This result of the following code will be:

    IDNameCityAgeSalary
    2MarryNew York242750
    1JohnLondon253000
    3JoParis272800
    5RameshNew Delhi283000
    6HuangBeijing282800
    4KimAmsterdam303100

❮ SQL Server Keywords