ORDER BY sorts the rows returned by a query based on one or more columns or expressions.
Sorting makes query results easier to understand and helps identify highest, lowest, newest, and oldest records.
Display employees from the highest salary to the lowest salary.
SELECT
EmployeeName,
Salary
FROM Employees
ORDER BY Salary DESC;
ORDER BY is commonly used in ranking reports, sales analysis, salary analysis, date-based reporting, and dashboard datasets.
- ORDER BY sorts query results.
- ASC sorts from low to high or A to Z.
- DESC sorts from high to low or Z to A.
- Multiple columns can be used.
- ASC is the default direction in many SQL systems.
What is ORDER BY used for?
ORDER BY sorts the rows returned by a SQL query.
What is the difference between ASC and DESC?
ASC sorts in ascending order, while DESC sorts in descending order.