Page 167 - SQL
P. 167

Id  FName      LName       PhoneNumber

          3   Michael    Williams    1357911131



         SELECT * FROM Employees ORDER BY LName DESC


        Or


          SELECT * FROM Employees ORDER BY LName ASC


        This statement changes the sorting direction.

        One may also specify multiple sorting columns. For example:


         SELECT * FROM Employees ORDER BY LName ASC, FName ASC


        This example will sort the results first by LName and then, for records that have the same LName, sort
        by FName. This will give you a result similar to what you would find in a telephone book.


        In order to save retyping the column name in the ORDER BY clause, it is possible to use instead the
        column's number. Note that column numbers start from 1.


         SELECT Id, FName, LName, PhoneNumber FROM Employees ORDER BY 3


        You may also embed a CASE statement in the ORDER BY clause.


         SELECT Id, FName, LName, PhoneNumber FROM Employees ORDER BY CASE WHEN LName='Jones` THEN 0
         ELSE 1 END ASC


        This will sort your results to have all records with the LName of "Jones" at the top.


        Select columns which are named after reserved keywords


        When a column name matches a reserved keyword, standard SQL requires that you enclose it in
        double quotation marks:


         SELECT
             "ORDER",
             ID
         FROM ORDERS


        Note that it makes the column name case-sensitive.

        Some DBMSes have proprietary ways of quoting names. For example, SQL Server uses square
        brackets for this purpose:


         SELECT
             [Order],
             ID



        https://riptutorial.com/                                                                             149
   162   163   164   165   166   167   168   169   170   171   172