SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
The LIMIT
clause in SQL is used to constrain the number of rows returned by a query. This can be particularly useful in scenarios where you want to retrieve a subset of records, such as pagination. The behavior and syntax might slightly differ based on the database system you're using.
MySQL, PostgreSQL, SQLite, and MariaDB:
To fetch the first 10 rows:
SELECT * FROM table_name LIMIT 10;
You can also specify an offset to indicate where to start the retrieval. This is particularly useful for pagination.
MySQL, PostgreSQL, SQLite, and MariaDB:
To fetch 10 rows, starting from the 11th row:
SELECT * FROM table_name LIMIT 10 OFFSET 10;
Or, using an alternative syntax (especially in MySQL):
SELECT * FROM table_name LIMIT 10, 10;
Here, the first number after LIMIT
is the offset, and the second number is the count of rows to retrieve.
SQL Server:
SQL Server doesn't have a LIMIT
keyword. Instead, you can use TOP
or the OFFSET ... FETCH
clauses:
-- Using TOP SELECT TOP 10 * FROM table_name; -- Using OFFSET and FETCH SELECT * FROM table_name OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY;
Oracle:
Oracle doesn't have a LIMIT
keyword either. For pagination, you would use the ROWNUM
or, in Oracle 12c and later, the FETCH
and OFFSET
clauses:
-- Using ROWNUM SELECT * FROM (SELECT * FROM table_name WHERE ROWNUM <= 20) WHERE ROWNUM >= 11; -- Using OFFSET and FETCH (Oracle 12c and later) SELECT * FROM table_name OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY;
When using the LIMIT
clause, especially for pagination, it's a good practice to use an ORDER BY
clause to ensure consistent ordering, as databases don't guarantee an inherent order of rows unless you specify it.
SQL LIMIT clause examples:
SELECT * FROM employees LIMIT 10;
How to use LIMIT in SQL:
SELECT * FROM orders LIMIT 5;
Retrieving a specific number of rows with LIMIT:
SELECT * FROM products LIMIT 20;
Using OFFSET with LIMIT in SQL:
SELECT * FROM customers OFFSET 10 LIMIT 5;
LIMIT vs. FETCH FIRST in SQL:
SELECT * FROM orders FETCH FIRST 5 ROWS ONLY;
LIMIT with ORDER BY in SQL:
SELECT * FROM products ORDER BY unit_price DESC LIMIT 10;
Dynamic values for LIMIT in SQL:
DECLARE @limit INT = 5; SELECT * FROM customers LIMIT @limit;
LIMIT in subqueries in SQL:
SELECT * FROM orders WHERE customer_id IN (SELECT customer_id FROM customers LIMIT 5);
Combining LIMIT with WHERE clause in SQL:
SELECT * FROM employees WHERE department_id = 2 LIMIT 10;
Limiting results with GROUP BY in SQL:
SELECT department_id, AVG(salary) as avg_salary FROM employees GROUP BY department_id LIMIT 5;
SQL FETCH FIRST vs. TOP vs. LIMIT:
-- Microsoft SQL Server (TOP) SELECT TOP 5 * FROM products; -- IBM Db2 (FETCH FIRST) SELECT * FROM orders FETCH FIRST 5 ROWS ONLY;
Alternatives to LIMIT for row limiting in SQL:
-- Alternative to LIMIT in PostgreSQL SELECT * FROM customers OFFSET 5 ROWS FETCH NEXT 10 ROWS ONLY;
Handling NULL values with LIMIT:
SELECT * FROM products WHERE product_name IS NOT NULL LIMIT 10;