LIMIT and OFFSET
Learn how to restrict the number of rows returned by a query.
Learn how to restrict the number of rows returned by a query. This hands-on tutorial focuses on practical implementation of limit and offset concepts.
Limiting Results
The LIMIT clause is used to specify the number of records to return. This is useful on large tables to improve performance or when you only need a sample of data.
[!NOTE] Different databases use different syntax for limiting rows. MySQL, PostgreSQL, and SQLite use
LIMIT. SQL Server usesTOP. Oracle usesFETCH FIRST. We will use the standardLIMITsyntax here (supported by our playground).
Syntax
SELECT column_names
FROM table_name
LIMIT number;
Example: Top 3 Products
Get the first 3 products from the table.
Pagination with OFFSET
The OFFSET clause allows you to skip a specific number of rows before returning the result. This is commonly used for pagination (e.g., "Show results 11-20").
Syntax
SELECT column_names
FROM table_name
LIMIT number OFFSET number;
Example: Skip First 2, Take Next 3
This query skips the first 2 customers and returns the next 3 (customers 3, 4, and 5).