The basic syntax of the ORDER BY clause is:
SELECT column1, column2, ...
FROM table_name
ORDER BY column_name [ASC|DESC];
column_name: The column by which the results should be sorted.ASC: Sorts the results in ascending order (default).DESC: Sorts the results in descending order.Let’s say we have a table employees and want to sort the employees by their name in alphabetical order:
SELECT * FROM employees
ORDER BY name ASC;
This query will retrieve all rows from the employees table and sort them by the name column in ascending order.
To sort the results in descending order, you can use DESC:
SELECT * FROM employees
ORDER BY name DESC;
This will display the employees table sorted by the name column in reverse alphabetical order.
The ORDER BY clause is a simple yet powerful tool for sorting your query results in MySQL. Whether you need ascending or descending order, it allows you to organize the data for better clarity. Using ORDER BY is essential for presenting your data in a meaningful and structured way.