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 theORDER BY
statement is ascending so if you use neitherASC
norDESC
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:
The following query will sort the result-set in ascending order (because neither ASC nor DESC is present):
The following query will sort the result-set in descending order:
Related Databases job interview questions
What is the difference between an inner join and outer join?
DatabasesMS SQL ServerSQL MediorWhat is the difference between temp tables and table variables in SQL?
DatabasesMS SQL ServerSQL SeniorWhat is ORDER BY statement used for in Microsoft SQL server?
DatabasesMS SQL ServerQuality Assurance (QA) JuniorWhat is WHERE statement good for in SQL server script?
DatabasesMS SQL ServerQuality Assurance (QA) JuniorWhat is DISTINCT and what is good for?
DatabasesMS SQL ServerQuality Assurance (QA) Junior