SQL SELECT TOP

SQL SELECT TOP n Statement

A SELECT TOP n statement returns the first n records.

SELECT TOP is useful when working with large datasets.

Other databases use keywords like LIMIT, OFFSET, and ROWNUM.

Example

#

Problem: List the top 5 largest orders, sorted by amount.

SELECT TOP 5 *
  FROM [Order]
 ORDER BY TotalAmount DESC

Syntax

TOP syntax.

SELECT TOP n column-names
  FROM table-name

More Examples

SQL SELECT TOP 10

Problem: List the top 10 most expensive products sorted by price.
SELECT TOP 10 Id, ProductName, UnitPrice, Package
  FROM Product
 ORDER BY UnitPrice DESC
Result:  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

You may also like



Guides