The ORDER BY
clause is used to sort the result set of a query by one or more columns. You can sort in ascending (ASC
) or descending (DESC
) order.
SELECT column1, column2, ... FROM table_name ORDER BY column1 [ASC | DESC], column2 [ASC | DESC], ...;
Get all employees sorted by their last_name
in alphabetical order:
SELECT first_name, last_name, department FROM employees ORDER BY last_name ASC;
Get employees sorted by salary
from highest to lowest:
SELECT first_name, last_name, salary FROM employees ORDER BY salary DESC;
Sort employees first by department
(ascending), then by salary
(descending) within each department:
SELECT first_name, last_name, department, salary FROM employees ORDER BY department ASC, salary DESC;
ASC
or DESC
is not specified, sorting defaults to ascending.SELECT
statement.Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!