SQL Select Top

Limit the number of rows returned by a query using TOP (SQL Server) or LIMIT (MySQL/MariaDB).

On this page

SQL SELECT TOP / LIMIT

Sometimes you do not need all rows from a table. You can limit the number of returned rows using TOP (SQL Server) or LIMIT (MySQL/MariaDB).

SQL Server: SELECT TOP

SELECT TOP 10 *
FROM customers;

This returns the first 10 rows.

MySQL / MariaDB: LIMIT

SELECT *
FROM customers
LIMIT 10;

This also returns 10 rows.

Using ORDER BY with LIMIT or TOP

Without ORDER BY, the database may return rows in an unpredictable order. Always combine LIMIT or TOP with ORDER BY when you want meaningful results.

SELECT name, price
FROM products
ORDER BY price DESC
LIMIT 5;

This returns the 5 most expensive products.

Using OFFSET (Pagination)

LIMIT can be combined with OFFSET to skip rows. This is useful for pagination.

SELECT *
FROM customers
ORDER BY id
LIMIT 10 OFFSET 20;

This skips the first 20 rows and returns the next 10.

SQL Server OFFSET Example

SELECT *
FROM customers
ORDER BY id
OFFSET 20 ROWS
FETCH NEXT 10 ROWS ONLY;

Common Mistakes

  • Using LIMIT without ORDER BY
  • Assuming row order is guaranteed without sorting
  • Confusing LIMIT syntax between database systems

Next Step

Continue with SQL Aggregate Functions to learn how to summarize data using COUNT, SUM, AVG, MIN, and MAX.