MySQL DESC Keyword
The MySQL ORDER BY statement is used to sort the result table in ascending or descending order. By default, ORDER BY keyword sorts the result in ascending order, however it can be specified using ASC keyword. To sort the result in descending order, DESC keyword is used.
Syntax
The syntax for using ORDER BY statement in MySQL is given below:
SELECT column1, column2, column3, ... FROM table_name ORDER BY column1, column2, ... ASC|DESC;
Example:
Consider a database table called Employee with the following records:
EmpID | Name | City | Age | Salary |
---|---|---|---|---|
1 | John | London | 25 | 3000 |
2 | Marry | New York | 24 | 2750 |
3 | Jo | Paris | 27 | 2800 |
4 | Kim | Amsterdam | 30 | 3100 |
5 | Ramesh | New Delhi | 28 | 3000 |
6 | Huang | Beijing | 28 | 2800 |
-
To fetch the Employee table sorted by Age (descending order), the query is:
SELECT * FROM Employee ORDER BY Age DESC;
This will produce the result as shown below:
ID Name City Age Salary 4 Kim Amsterdam 30 3100 5 Ramesh New Delhi 28 3000 6 Huang Beijing 28 2800 3 Jo Paris 27 2800 1 John London 25 3000 2 Marry New York 24 2750 -
To fetch all fields of the Employee table sorted by Age (ascending order) and Salary (descending order), the query will be:
SELECT * FROM Employee ORDER BY Age ASC, Salary DESC;
This result of the following code will be:
ID Name City Age Salary 2 Marry New York 24 2750 1 John London 25 3000 3 Jo Paris 27 2800 5 Ramesh New Delhi 28 3000 6 Huang Beijing 28 2800 4 Kim Amsterdam 30 3100
❮ MySQL Keywords