In MySQL, the ORDER BY clause is used in a SELECT statement to sort the result set based on one or more columns.
The basic syntax is:
SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC | DESC], column2 [ASC | DESC], ...;
- column1, column2, ...: The columns you want to retrieve.
- ORDER BY: Specifies the sorting order.
- ASC: Ascending order (default if not specified).
- DESC: Descending order.
Here's a simple example:
-- Retrieve employee names and salaries from the "employees" table, sorted by salary in descending order
SELECT employee_name, salary
FROM employees
ORDER BY salary DESC;