ORDER BY
In MySQL, the ORDER BY clause is used to sort the result set of a query based on one or more columns. It is typically used with the SELECT statement to arrange the rows in a specific order.
Syntax
SELECT column1, column2
FROM table_name
WHERE condition
ORDER BY column1 [ASC | DESC], column2 [ASC | DESC];Here’s a brief explanation of the components:
- ASC: Ascending order (default). Rows are sorted from the smallest value to the largest value.
- DESC: Descending order. Rows are sorted from the largest value to the smallest value.
Example
Suppose we have an Employees table as shown below:
Image scaled to 60%
and we want to retrieve the employee’s records with age arranged in ascending order.
SELECT *
FROM Employee
ORDER BY Age ASC;Database Exercise
Database Schema:
-- Database schema would be rendered hereExercise Script:
-- Exercise script would be rendered hereAvailable actions: Execute
The result of this query would be a table showing the employee’s record in the following order:
Image scaled to 60%
Last updated on