Page 168 - SQL
P. 168

FROM ORDERS


        while MySQL (and MariaDB) by default use backticks:


         SELECT
             `Order`,
             id
         FROM orders


        Selecting specified number of records


        The SQL 2008 standard defines the FETCH FIRST clause to limit the number of records returned.


         SELECT Id, ProductName, UnitPrice, Package
         FROM Product
         ORDER BY UnitPrice DESC
         FETCH FIRST 10 ROWS ONLY


        This standard is only supported in recent versions of some RDMSs. Vendor-specific non-standard
        syntax is provided in other systems. Progress OpenEdge 11.x also supports the FETCH FIRST <n>
        ROWS ONLY syntax.

        Additionally, OFFSET <m> ROWS before FETCH FIRST <n> ROWS ONLY allows skipping rows before fetching
        rows.


         SELECT Id, ProductName, UnitPrice, Package
         FROM Product
         ORDER BY UnitPrice DESC
         OFFSET 5 ROWS
         FETCH FIRST 10 ROWS ONLY


        The following query is supported in SQL Server and MS Access:


         SELECT TOP 10 Id, ProductName, UnitPrice, Package
         FROM Product
         ORDER BY UnitPrice DESC


        To do the same in MySQL or PostgreSQL the LIMIT keyword must be used:


         SELECT Id, ProductName, UnitPrice, Package
         FROM Product
         ORDER BY UnitPrice DESC
         LIMIT 10


        In Oracle the same can be done with ROWNUM:


         SELECT Id, ProductName, UnitPrice, Package
         FROM Product
         WHERE ROWNUM <= 10
         ORDER BY UnitPrice DESC





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