What is the meaning of ASC and DESC keywords in SQL server?

Experience Level: Junior
Tags: DatabasesQuality Assurance (QA)

Answer

Both keywords are used in SELECT query together with ORDER BY statement. They define how the result-set will be sorted.

  • ORDER BY columnName ASC sorts the result-set in ascending order. The default order for the ORDER BY statement is ascending so if you use neither ASC nor DESC keyword, the result-set will be sorted in ascending order.
  • ORDER BY columnName DESC sorts the result-set in descending order.

Examples:

The following query will sort the result-set in ascending order:

Example
SELECT * FROM Customers ORDER BY CustomerName ASC

The following query will sort the result-set in ascending order (because neither ASC nor DESC is present):

Example
SELECT * FROM Customers ORDER BY CustomerName

The following query will sort the result-set in descending order:

Example
SELECT * FROM Customers ORDER BY CustomerName DESC
Related Databases job interview questions

Comments

No Comments Yet.
Be the first to tell us what you think.
MS SQL Server for Testers
MS SQL Server for Testers

Are you learning Databases ? Try our test we designed to help you progress faster.

Test yourself