How to Sorts the result set in descending order in SQL ?
To sort the result set in descending order in SQL, we can use the ORDER BY
clause along with the DESC
keyword.
Syntax:
SELECT column1, column2, ...
FROM table_name
ORDER BY column_name DESC;
Example:
Consider a table named students
with columns id
, name
, age
, and marks
. To sort the result set in descending order based on the marks
column, we can use the following query:
SELECT * FROM students
ORDER BY marks DESC;
This will return all the rows from the students
table sorted in descending order based on the marks
column.
We can also sort the result set in descending order based on multiple columns. For example, to sort the result set in descending order based on the marks
column and then the age
column, we can use the following query:
SELECT * FROM students
ORDER BY marks DESC, age DESC;
This will return all the rows from the students
table sorted in descending order based on the marks
column and then the age
column.