AdventureWorks Database: Sort the BusinessEntityID in descending or ascending order

SQL Query - AdventureWorks: Exercise-126 with Solution

126 From the following table write a query in SQL to sort the BusinessEntityID in descending order for those employees that have the SalariedFlag set to 'true' and in ascending order that have the SalariedFlag set to 'false'. Return BusinessEntityID, and SalariedFlag.

Sample table: HumanResources.Employee

Click to view Full table

Sample Solution:

SELECT BusinessEntityID, SalariedFlag  
FROM HumanResources.Employee  
         CASE when SalariedFlag  = 'true' 
           THEN BusinessEntityID END 
        ,CASE WHEN SalariedFlag = 'false'
           THEN BusinessEntityID END;

Sample Output:

               4|false       |
              11|false       |
              12|false       |
              13|false       |
              17|false       |
              18|false       |
              19|false       |
              20|false       |
              21|false       |
              22|false       |
              23|false       |
              24|false       |

SQL AdventureWorks Editor:

Practice Online

Contribute your code and comments through Disqus.

Previous: Return the highest hourly wage for each job title.
Next: Display the list price as a comment based on the price range.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Share this Tutorial / Exercise on : Facebook and Twitter

SQL: Tips of the Day

Convert Timestamp to date in MySQL Query:

DATE_FORMAT(FROM_UNIXTIME(`user.registration`), '%e %b %Y') AS 'date_formatted'

Database: MySQL

Ref : https://bit.ly/3EJPnMQ