Order By Desc in SQL
ORDER BY DESC in SQL
The ORDER BY DESC clause sorts the result set of a query in descending order — from the largest to smallest values.
? Syntax:
SELECT column1, column2, ...FROM table_nameORDER BY column_name DESC;DESCmeans descending order.For numbers, it sorts from highest to lowest.
For text, it sorts from Z to A.
For dates, it sorts from newest to oldest.
? Example:
Get all employees ordered by salary from highest to lowest:
SELECT employee_name, salaryFROM employeesORDER BY salary DESC;Multiple columns example:
Order by department ascending and within department, order employees by salary descending:
SELECT employee_name, department_id, salaryFROM employeesORDER BY department_id ASC, salary DESC;Notes:
If
DESCis omitted, the default order isASC(ascending).Can be combined with other clauses like
WHERE,GROUP BY, etc.Useful for getting top values, e.g., highest salaries, latest dates.
Want me to show how to get the top N rows after sorting with ORDER BY DESC?