In MySQL, the ORDER BY clause is used to sort the result set of a query based on one or more columns.
We can specify the column(s) by which we can order the results and whether the sorting should be in ascending (default) or descending order.
SELECT column1, column2, ... FROM table_name ORDER BY column1 [ASC | DESC], column2 [ASC | DESC], ...;
column1, column2, ...: The columns we want to retrieve from the table.
table_name: The name of the table from which we want to retrieve data.
ORDER BY: Specifies the column(s) by which to sort the result set.
ASC: Ascending order (default).
DESC: Descending order.
SELECT * FROM products ORDER BY product_name;
This query retrieves all columns from the products table and sorts the result set in ascending order based on the "product_name" column.
SELECT * FROM products ORDER BY price DESC;
This query retrieves all columns from the products table and sorts the result set in descending order based on the price column.
SELECT * FROM orders ORDER BY order_date DESC, total_amount ASC;
This query retrieves all columns from the "orders" table and sorts the result set first in descending order based on the "order_date" column and then in ascending order based on the "total_amount" column.