17.05 - What the TOP n Operator Does - Advanced SQL Engine - Teradata Database

Teradata Vantage™ - SQL Data Manipulation Language

Product
Advanced SQL Engine
Teradata Database
Release Number
17.05
Release Date
January 2021
Content Type
Programming Reference
Publication ID
B035-1146-175K
Language
English (United States)
You can use the TOP n operator in these ways:
  • The TOP n operator returns only a subset of the rows in the query result set. For example, this query returns 10 rows from the orders table:
         SELECT TOP 10 *
         FROM orders;
  • To obtain a subset of the data from an ordered set, specify the TOP n operator with an ORDER BY clause. For example, this query returns the last five orders shipped:
         SELECT TOP 5 *
         FROM orders
         ORDER BY ship_date DESC;
  • If the TOP n operator specifies a number greater than the number of rows in the query result set, then the query returns all of the rows in the query result set without returning an error.
  • As the size of n increases, the performance of TOP n gradually degrades.
  • You can use parameters to pass values for n from:
    • CREATE MACRO, see Teradata Vantage™ - SQL Data Definition Language Syntax and Examples, B035-1144.
    • CREATE PROCEDURE (SQL Form), see Teradata Vantage™ - SQL Data Definition Language Detailed Topics , B035-1184 .
    • USING Request Modifier, see USING Request Modifier.