Sql Query to retrieve data using Order By in Ascending and Descending Order in priority


To retrieve a record in multiple orders for different columns from the existing created table. Then we can use an ' ORDER BY ' Clause multiple times with ASC or DESC differently depending upon the requirement.

Suppose if we want to retrieve a record in ascending order of a first_name, And there are two or more records with the same first_name. So for the same first_name record, we want to retrieve its last_name in descending order.

Then we have to 1st declare first_name in ascending and 2nd declare last_name in descending.

In this 'ORDER BY first_name ASC, last_name DESC ' and 'ORDER BY last_name DESC, first_name ASC' both look the same, but both will retrieve different data

Enter - Select * from table_name;
SQL Query
SELECT 'Here 1st priority will always be given to first_name in Ascending Order, 2nd priority to last_name in Descending Order'; SELECT first_name,last_name FROM employees ORDER BY first_name ASC, last_name DESC; SELECT 'Here 1st priority will always be given to last_name in Descending Order, 2nd priority to first_name in Ascending Order'; SELECT first_name,last_name FROM employees ORDER BY last_name DESC, first_name ASC;
Output