Page 146 - SQL
P. 146

DisplayName      Reputation

          BalusC           650237


          Hans Passant     625870

          Marc Gravell     601636



        Remarks

        Some versions of SQL (such as MySQL) use a LIMIT clause at the end of a SELECT, instead of TOP
        at the beginning, for example:


         SELECT DisplayName, Reputation
         FROM Users
         ORDER BY Reputation DESC
         LIMIT 5


        Sorting by multiple columns



         SELECT DisplayName, JoinDate, Reputation
         FROM Users
         ORDER BY JoinDate, Reputation


          DisplayName      JoinDate       Reputation


          Community        2008-09-15     1

          Jeff Atwood      2008-09-16     25784


          Joel Spolsky     2008-09-16     37628


          Jarrod Dixon     2008-10-03     11739

          Geoff Dalgas     2008-10-03     12567



        Sorting by column number (instead of name)


        You can use a column's number (where the leftmost column is '1') to indicate which column to
        base the sort on, instead of describing the column by its name.

        Pro: If you think it's likely you might change column names later, doing so won't break this code.


        Con: This will generally reduce readability of the query (It's instantly clear what 'ORDER BY
        Reputation' means, while 'ORDER BY 14' requires some counting, probably with a finger on the
        screen.)

        This query sorts result by the info in relative column position 3 from select statement instead of
        column name Reputation.




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