
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.
📌What is the LIMIT Keyword?
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.
📌 Using LIMIT in SELECT Queries
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_number | full_names | gender | date_of_birth | |
1 | Janet Jones | Female | 21-07-1980 | janetjones@yagoo.cm |
2 | Janet Smith Jones | Female | 23-06-1980 | jj@fstreet.com |
✅ Use Case: Fetching a small subset of data for improved performance.
📌 Fetching a Specific Number of Records
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.
📌 Using OFFSET with LIMIT – Skipping Rows
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_number | full_names | gender | date_of_birth | |
2 | Janet Smith Jones | Female | 23-06-1980 | jj@fstreet.com |
3 | Robert Phil | Male | 12-07-1989 | rm@tstreet.com |
✅ Use Case: Paginating query results in web applications.
📌Using LIMIT for Pagination
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.
📌 Summary of MySQL LIMIT & OFFSET
Command | Purpose | Example |
LIMIT | Restrict query results | SELECT * FROM members LIMIT 5; |
LIMIT with OFFSET | Skip rows and fetch a subset | SELECT * FROM members LIMIT 10, 5; |
Pagination | Fetch results page-wise | SELECT * 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!