The ORDER BY
clause in SQL is used to sort the result set in ascending (ASC) or descending (DESC) order based on one or more columns.
SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;
SELECT name, marks FROM Students ORDER BY marks DESC;
This query shows students ordered by their marks in descending order (highest marks first).
ProductID | Name | Category | Price |
---|---|---|---|
101 | Laptop | Electronics | 55000 |
102 | Mobile | Electronics | 30000 |
103 | Chair | Furniture | 1500 |
104 | Table | Furniture | 2500 |
Write a query to list all products ordered by price in ascending order.
You can sort using more than one column. For example:
SELECT name, department, salary FROM Employees ORDER BY department ASC, salary DESC;
This query first sorts employees by department (A-Z), then by salary within each department (high to low).
ORDER BY
lets you sort query results.ASC
(default) sorts from lowest to highest.DESC
sorts from highest to lowest.Next ➡️: Learn to filter and sort together using WHERE
+ ORDER BY
!
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!