TOP n is a Teradata extension to the ANSI SQL:2011 standard.
Other SQL dialects use similar operators with names such as:
You can use the TOP n operator in these ways:
SELECT TOP 10 *
FROM orders;
SELECT TOP 5 *
FROM orders
ORDER BY ship_date DESC;
You cannot specify the TOP n operator in any of these SQL statements or statement components:
You cannot specify these options in a SELECT statement that specifies the TOP n operator:
This restriction refers to the WITH clause you can specify for summary lines and breaks. See “WITH Clause” on page 218. The nonrecursive WITH statement modifier that can precede the SELECT keyword can be included in statements that also specify the TOP n operator. See “WITH Statement Modifier” on page 43.
You cannot specify the n value of a TOP n specification as a USING parameter for iterated array processing. For an example, see “Example 12: Non-Support for Iterated Requests With TOP n” on page 530.
The system evaluates the TOP n operator after all other clauses in the SELECT statement have been evaluated.
The QUALIFY clause with the RANK or ROW_NUMBER ordered analytical functions returns the same results as the TOP n operator.
For best performance, use the TOP option instead of the QUALIFY clause with RANK or ROW_NUMBER. In best case scenarios, the TOP n operator provides better performance; in worst case scenarios, the TOP n operator provides equivalent performance.
For example, these two statements have the same semantics, but the statement that specifies TOP n performs better than the statement that specifies QUALIFY ROW_NUMBER.
SELECT TOP 10 *
FROM sales
ORDER BY county;
SELECT *
FROM sales
QUALIFY ROW_NUMBER() OVER (ORDER BY COUNTY) <= 10;
Similarly, these two statements have the same semantics, but the statement that specifies TOP n performs better than the statement that specifies QUALIFY RANK.
SELECT TOP 10 WITH TIES *
FROM sales ORDER BY county;
SELECT *
FROM sales
QUALIFY RANK() OVER (ORDER BY county) <= 10;
Consider this data in the orders table:
SELECT *
FROM orders;
order_date customer product quantity
---------- ------------ ------------ -----------
04/05/10 Bestway JR-0101 10
04/04/28 Bestway SW-0022 25
04/05/10 Bestway QR-7737 10
04/04/28 Samstone JR-0101 35
04/05/10 Bestway SW-0023 10
04/04/28 Samstone KB-6883 20
04/05/10 Finelity JR-0101 12
04/04/28 Samstone SW-0023 12
04/05/10 Finelity SW-0021 24
04/05/10 Finelity KB-8883 24
The following statement selects the top three orders with the largest quantities:
SELECT TOP 3 *
FROM orders
ORDER BY quantity DESC;
order_date customer product quantity
---------- ------------ ------------ -----------
04/04/28 Samstone JR-0101 35
04/04/28 Bestway SW-0022 25
04/05/10 Finelity SW-0021 24
To include any orders with the same quantity as the third-largest, use the WITH TIES option:
SELECT TOP 3 WITH TIES *
FROM orders
ORDER BY quantity DESC;
order_date customer product quantity
---------- ------------ ------------ -----------
04/04/28 Samstone JR-0101 35
04/04/28 Bestway SW-0022 25
04/05/10 Finelity SW-0021 24
04/05/10 Finelity KB-8883 24
For more information about options and functions related to the TOP n operator, see: