Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. SQL Dictionary
  3. LIMIT / OFFSET

LIMIT / OFFSET

Since: MySQL Extension

Clauses that specify the number of rows to retrieve and the starting position. Commonly used to implement pagination.

Syntax

Limits the number of rows returned (MySQL, PostgreSQL, SQLite, etc.).

SELECT column FROM table_name LIMIT count;

Specifies a starting position and row count (OFFSET is zero-based).

SELECT column FROM table_name LIMIT count OFFSET start;

MySQL shorthand syntax (LIMIT start, count).

SELECT column FROM table_name LIMIT start, count;

Syntax Overview

SyntaxDescription
LIMIT countSpecifies the maximum number of rows to return. Available in MySQL, PostgreSQL, and SQLite.
OFFSET startSpecifies the row position at which to start retrieval. 0 refers to the first row. Used together with LIMIT.
LIMIT start, countMySQL shorthand syntax. LIMIT 0, 10 returns the first 10 rows.
FETCH FIRST count ROWS ONLYStandard SQL syntax (SQL:2008 and later). Available in Oracle, DB2, PostgreSQL, and others.

Sample Code

The following 'employees' table is used in the examples below.

employees id name salary hire_date 1 user_a 380000 2019-09-15 2 user_c 350000 2020-06-01 3 user_e 320000 2021-04-01 4 user_b 300000 2022-01-10 5 user_d 270000 2023-04-01 5 rows in set

Retrieves the top 3 employees by salary (highest first).

sample_limit_offset.sql
SELECT name, salary FROM employees ORDER BY salary DESC LIMIT 3;
+--------+--------+
| name   | salary |
+--------+--------+
| user_a | 380000 |
| user_c | 350000 |
| user_e | 320000 |
+--------+--------+
3 rows in set

Pagination example (3 rows per page, retrieves page 2).

sample_limit_offset.sql
SELECT name, hire_date FROM employees ORDER BY hire_date DESC LIMIT 3 OFFSET 3;
+--------+------------+
| name   | hire_date  |
+--------+------------+
| user_c | 2020-06-01 |
| user_a | 2019-09-15 |
+--------+------------+
2 rows in set

Same result using MySQL shorthand syntax.

sample_limit_offset.sql
SELECT name, hire_date FROM employees ORDER BY hire_date DESC LIMIT 3, 3;
+--------+------------+
| name   | hire_date  |
+--------+------------+
| user_c | 2020-06-01 |
| user_a | 2019-09-15 |
+--------+------------+
2 rows in set

Retrieves the earliest hired employee.

sample_limit_offset.sql
SELECT name, hire_date FROM employees ORDER BY hire_date ASC LIMIT 1;
+--------+------------+
| name   | hire_date  |
+--------+------------+
| user_a | 2019-09-15 |
+--------+------------+
1 row in set

Syntax by Database

PostgreSQL and SQLite support the same LIMIT / OFFSET syntax as MySQL.

SELECT name, salary FROM employees ORDER BY salary DESC LIMIT 3 OFFSET 3;

Oracle uses FETCH FIRST (version 12c and later).

SELECT name, salary FROM employees ORDER BY salary DESC
OFFSET 3 ROWS FETCH NEXT 3 ROWS ONLY;

Notes

LIMIT and OFFSET are essential for implementing pagination. Given a page number ($page) and a per-page count ($per_page), the offset can be calculated as ($page - 1) * $per_page.

Using LIMIT / OFFSET without ORDER BY does not guarantee the order of results. Always pair them with ORDER BY to ensure consistent ordering when paginating.

With large datasets, performance degrades as the OFFSET value increases. For high-volume data, consider cursor-based pagination (filtering by the last seen ID) as an alternative.

If you find any errors or copyright issues, please .