Export (0) Print
Expand All
Expand Minimize

TOP (SQL Server Compact)

Specifies that only the first set of rows will be returned from the query result. The set of rows can be either a number, or a percent of the rows. The TOP expression can be used in SELECT statements only.


[
     TOP (expression)
]

Term

Definition

TOP (expression)

Is the numeric expression that specifies the number of rows to be returned. expression is implicitly converted to a bigint.

The following expression types are supported:

- Integer constants.

- Arithmetic expressions on constants of type int or bigint.

- Parameters.

Parentheses that delimit expression in TOP are required. The maximum value expression can be is 2147483648.

If the query includes an ORDER BY clause, the first expression rows, ordered by the ORDER BY clause, are returned. If the query has no ORDER BY clause, the order of the rows is arbitrary.

The PERCENT and WITH TIES options are not supported. The TOP expression cannot be used in UPDATE, DELETE, or INSERT statements.

A sub-query can include an ORDER BY clause only if it has a TOP clause.

For more information, see SQL Server 2008 Books Online topic, "TOP (Transact-SQL)".

Example

Simple TOP Query Ordered by Company

The following example selects the first 10 customers that are listed in the Customers table of the Northwind database and orders them by Company Name.

SELECT TOP (10) * 
FROM Customers 
ORDER BY [Company Name]

Simple and Nested TOP Query

The following example lists the first five employees by using a simple and a nested query.

-- Simple TOP query
SELECT TOP(5) * 
FROM Employees;
-- Nested TOP query
SELECT * 
FROM (SELECT TOP(5) * 
      FROM Employees 
      ORDER BY [Employee Id]) 
AS E;
Show:
© 2014 Microsoft