---Advertisement---

MySQL LIMIT & OFFSET – Efficient Data Pagination with Examples Great 2025

By Manisha

Updated On:

---Advertisement---
MySQL LIMIT

MySQL LIMIT & OFFSET: In MySQL, the LIMIT keyword helps restrict the number of rows returned in a query result. It is widely used in pagination, optimizing query performance, and managing large datasets.

Additionally, the OFFSET keyword works alongside LIMIT to specify the starting row position in the result set.


MySQL LIMIT & OFFSET: The LIMIT keyword is used with SELECT, UPDATE, and DELETE statements to limit the number of records retrieved or affected.

Basic Syntax:

sql

SELECT column_name(s) FROM table_name [WHERE condition] LIMIT N;

Key Components:
MySQL LIMIT & OFFSET: SELECT column_name(s) FROM table_name – Defines the fields and table to query.
[WHERE condition] – Optional condition to filter records.
LIMIT N – Restricts the result set to N rows.


Example: Retrieve the First 2 Rows from the members Table

sql

SELECT * FROM members LIMIT 2;

This query returns only 2 rows from the members table.

Sample Output:

membership_numberfull_namesgenderdate_of_birthemail
1Janet JonesFemale21-07-1980janetjones@yagoo.cm
2Janet Smith JonesFemale23-06-1980jj@fstreet.com

Use Case: Fetching a small subset of data for improved performance.


MySQL LIMIT & OFFSET: If we need the first 10 registered members, we can modify our query:

sql

SELECT * FROM members LIMIT 10;

This will return a maximum of 10 rows from the members table.

🔹 If the total number of records is less than 10, all available records are returned.


MySQL LIMIT & OFFSET: OFFSET is used with LIMIT to specify where to start fetching records.

Syntax:

sql

SELECT * FROM table_name LIMIT offset, row_count;

Key Components:
offset – The starting row index (0-based).
row_count – Number of rows to return.

Example: Fetching 2 Records Starting from the 2nd Row

sql

SELECT * FROM members LIMIT 1, 2;

OFFSET = 1 – Skips the first row.
LIMIT = 2 – Returns the next 2 records.

Sample Output:

membership_numberfull_namesgenderdate_of_birthemail
2Janet Smith JonesFemale23-06-1980jj@fstreet.com
3Robert PhilMale12-07-1989rm@tstreet.com

Use Case: Paginating query results in web applications.


MySQL LIMIT & OFFSET: When displaying large datasets, pagination improves performance.

Example: Show 20 Records Per Page

For page 1 (first 20 records):

sql

SELECT * FROM members LIMIT 0, 20;

For page 2 (next 20 records):

sql

SELECT * FROM members LIMIT 20, 20;

For page 3 (next 20 records):

sql

SELECT * FROM members LIMIT 40, 20;

Use Case: Displaying results on a website with “Next” and “Previous” buttons.


CommandPurposeExample
LIMITRestrict query resultsSELECT * FROM members LIMIT 5;
LIMIT with OFFSETSkip rows and fetch a subsetSELECT * FROM members LIMIT 10, 5;
PaginationFetch results page-wiseSELECT * FROM members LIMIT 20, 20;

MySQL LIMIT & OFFSET: By using LIMIT and OFFSET, you can optimize MySQL queries for better performance and efficient data retrieval!

MYSQL ALTER

Download My SQL

Leave a Comment

Index