SQL SELECT TOP Clause
Hereβs a clear and practical guide to the SQL SELECT TOP clause, including differences across database engines.
β Basic Usage (SQL Server / MS Access)
This returns the first 10 rows based on the default ordering (or a specified ORDER BY).
π₯ SELECT TOP with ORDER BY
Always combine with ORDER BY to ensure predictable results:
π§ SELECT TOP with PERCENT
Returns a percentage of rows:
β Other Databases Donβt Use TOP
Different SQL engines use different syntax:
PostgreSQL / MySQL / SQLite β Use LIMIT
Oracle β Use FETCH FIRST
π Best Practices
β Always pair TOP/LIMIT with an ORDER BY
β Avoid using TOP without specifying sorting (non-deterministic)
β For pagination, use OFFSET + LIMIT (PostgreSQL/MySQL/SQLite) or OFFSET with FETCH (SQL Server/Oracle)
