Page 147 - SQL
P. 147

SELECT DisplayName, JoinDate, Reputation
         FROM Users
         ORDER BY 3


          DisplayName      JoinDate       Reputation


          Community        2008-09-15     1

          Jarrod Dixon     2008-10-03     11739


          Geoff Dalgas     2008-10-03     12567

          Joel Spolsky     2008-09-16     25784


          Jeff Atwood      2008-09-16     37628



        Order by Alias


        Due to logical query processing order, alias can be used in order by.


         SELECT DisplayName, JoinDate as jd, Reputation as rep
         FROM Users
         ORDER BY jd, rep


        And can use relative order of the columns in the select statement .Consider the same example as
        above and instead of using alias use the relative order like for display name it is 1 , for Jd it is 2
        and so on


         SELECT DisplayName, JoinDate as jd, Reputation as rep
         FROM Users
         ORDER BY 2, 3


        Customizeed sorting order


        To sort this table Employee by department, you would use ORDER BY Department. However, if you want
        a different sort order that is not alphabetical, you have to map the Department values into different
        values that sort correctly; this can be done with a CASE expression:


          Name     Department


          Hasan    IT

          Yusuf    HR


          Hillary  HR


          Joe      IT

          Merry    HR



        https://riptutorial.com/                                                                             129
   142   143   144   145   146   147   148   149   150   151   152