Share via


TOP (Entity SQL)

The SELECT clause can have an optional TOP sub-clause following the optional ALL/DISTINCT modifier. The TOP sub-clause specifies that only the first set of rows will be returned from the query result.

[ TOP (n) ]

Arguments

  • n
    The numeric expression that specifies the number of rows to be returned. n could be a single numeric literal or a single parameter.

Remarks

The TOP expression must be either a single numeric literal or a single parameter. If a constant literal is used, the literal type must be implicitly promotable to Edm.Int64 (byte, int16, int32 or int64 or any provider type that maps to a type that is promotable to Edm.Int64) and its value must be greater than or equal to zero. Otherwise an exception will be raised. If a parameter is used as an expression, the parameter type must also be implicitly promotable to Edm.Int64, but there will be no validation of the actual parameter value during compilation because the parameter values are late bounded.

The following is an example of constant TOP expression:

select distinct top(10) c.a1, c.a2 from T as a

The following is an example of parametrized TOP expression:

select distinct top(@topParam) c.a1, c.a2 from T as a

TOP is non-deterministic unless the query is sorted. If you require a deterministic result, use the SKIP and LIMIT sub-clauses in the ORDER BY clause. The TOP and SKIP/LIMIT are mutually exclusive.

See Also

Reference

SELECT (Entity SQL)
SKIP (Entity SQL)
LIMIT (Entity SQL)
ORDER BY (Entity SQL)

Concepts

Entity SQL Reference