Q:

Write a MySQL statement to select data of all departments in descending order by age

belongs to collection: MySQL Exercises

0

MySQL Order By Exercise

Write a MySQL statement to select data of all departments in descending order by age

Suppose the table is -

 +----+--------------+------------+-----+
| id | name         | department | age |
+----+--------------+------------+-----+
|  1 | Maria Gloria | CS         |  22 |
|  2 | John Smith   | IT         |  23 |
|  3 | Gal Rao      | CS         |  22 |
|  4 | Jakey Smith  | EC         |  24 |
|  5 | Rama Saho    | IT         |  22 |
|  6 | Maria Gaga   | EC         |  23 |
+----+--------------+------------+-----+

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Solution

The default sort order is ascending, with smallest values first. To sort the data in descending order, add the DESC keyword to the name of the column you are sorting by -

The following statement returns student data of all departments in descending order by age.

 mysql> SELECT * FROM students
     -> ORDER BY age DESC;

Output of the above statement 

+----+--------------+------------+-----+
| id | name         | department | age |
+----+--------------+------------+-----+
|  4 | Jakey Smith  | EC         |  24 |
|  2 | John Smith   | IT         |  23 |
|  6 | Maria Gaga   | EC         |  23 |
|  1 | Maria Gloria | CS         |  22 |
|  3 | Gal Rao      | CS         |  22 |
|  5 | Rama Saho    | IT         |  22 |
+----+--------------+------------+-----+

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

MySQL Exercises

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a MySQL statement to select data of all depa... >>
<< Write a mysql statement to select data of only stu...