Selecting specified number of records

suggest change

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 older Oracle, filter ROWNUM only after the rows have been ordered. WHERE ROWNUM <= 10 before ORDER BY keeps an arbitrary 10 rows and then sorts those:

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

Results: 10 records.

Id    ProductName               UnitPrice             Package
38    Côte de Blaye             263.50                12 - 75 cl bottles
29    Thüringer Rostbratwurst   123.79                50 bags x 30 sausgs.
9    Mishi Kobe Niku            97.00                 18 - 500 g pkgs.
20    Sir Rodney's Marmalade    81.00                 30 gift boxes
18    Carnarvon Tigers          62.50                 16 kg pkg.
59    Raclette Courdavault      55.00                 5 kg pkg.
51    Manjimup Dried Apples     53.00                 50 - 300 g pkgs.
62    Tarte au sucre            49.30                 48 pies
43    Ipoh Coffee               46.00                 16 - 500 g tins
28    Rössle Sauerkraut         45.60                 25 - 825 g cans

Vendor Nuances:

TOP and FETCH are applied after ORDER BY. A ROWNUM test in the same query block as ORDER BY is applied before the sort, so it does not mean “the first 10 rows of the ordered result.”

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents