TOP is a proprietary SQL Server and Sybase extension that restricts a SELECT statement to the first N rows returned by the query processor. It is evaluated after the WHERE clause and before the ORDER BY is applied for ties. Because it executes on the server, performance is superior to client-side truncation. TOP may also work with the PERCENT modifier to return a percentage of rows and WITH TIES to include additional rows that match the last ordered value, ensuring deterministic results when duplicates exist. Unlike ANSI-standard LIMIT/OFFSET or FETCH FIRST, TOP is not portable across most databases, so migration requires syntax conversion. Without an ORDER BY clause, the rows returned are nondeterministic.
expression
(integer or subquery) - Number of rows (or percent) to return.PERCENT
(keyword) - Interprets expression as a percentage of the total rows.WITH TIES
(keyword) - Adds additional rows that have the same ORDER BY values as the last row within the TOP limit.LIMIT, FETCH FIRST, OFFSET, ORDER BY, ROW_NUMBER, ROWNUM
Microsoft SQL Server 7.0
TOP limits the number of rows returned by a SELECT query, reducing result size and improving performance.
No. TOP is proprietary to SQL Server and Sybase. Standard SQL uses FETCH FIRST or LIMIT.
Add the PERCENT keyword: `SELECT TOP (5) PERCENT * FROM table;` returns 5 percent of all rows.
Add WITH TIES after TOP to include any extra rows that share the same ORDER BY values as the final row within the specified limit.