MySQL: Using LIMIT and OFFSET to get a subset of records

Updated: January 25, 2024 By: Guest Contributor Post a comment

Introduction

MySQL is a powerful open-source relational database management system that is widely used for web applications. In data handling, retrieving the entire data sets from a database is not always necessary or efficient. Sometimes, we need just a subset of records, such as when implementing pagination on a website. In this tutorial, we’ll explore how to use the LIMIT and OFFSET clauses in MySQL to fetch specific portions of records.

Understanding LIMIT and OFFSET

The LIMIT clause is used in SQL to specify the maximum number of records to return. The OFFSET clause is used to skip a set amount of records before starting to return records from the query results.

Basic Syntax

SELECT column1, column2, ...
FROM table_name
LIMIT number_of_records;
SELECT column1, column2, ...
FROM table_name
LIMIT number_of_records OFFSET number_of_records_to_skip;

Using LIMIT

Let’s begin with a simple example. Suppose we have a table users with many records, and we want to retrieve only the first 10 records.

SELECT id, username
FROM users
LIMIT 10;

The SQL query above will return the first 10 users from our users table.

Combining LIMIT with WHERE clause

You can also combine the LIMIT clause with the WHERE clause to filter the records before applying the limit.

SELECT id, username
FROM users
WHERE active = 1
LIMIT 10;

This query will return the first 10 active users.

Using OFFSET with LIMIT

When you need to skip a certain number of records and then fetch a specified number, use both LIMIT and OFFSET.

SELECT id, username
FROM users
LIMIT 10 OFFSET 20;

This query skips the first 20 records and then retrieves the next 10 records. It’s particularly useful for pagination, where, for example, you might want to show the third page of user records, assuming each page shows 10 records.

Understanding the Implications of OFFSET

The OFFSET value can be costly in terms of performance, especially when dealing with large data sets because even though it skips the first N records, it still has to read through them. As a result, using OFFSET for pagination can be efficient only when working with a small offset value relative to the total number of records.

Ordering Results

When using LIMIT and OFFSET, it’s often important to use an ORDER BY clause to ensure a consistent order in which the records are returned.

SELECT id, username
FROM users
ORDER BY created_at DESC
LIMIT 10 OFFSET 20;

This ensures that you get the same subset of records for the same LIMIT and OFFSET values each time you run the query.

Using LIMIT and OFFSET with Joins

When you have complex queries involving joins, you can still use LIMIT and OFFSET to narrow down the results.

SELECT users.id, users.username, orders.amount
FROM users
JOIN orders ON users.id = orders.user_id
ORDER BY orders.amount DESC
LIMIT 5 OFFSET 10;

This query will return 5 records, skipping the first 10, from the joined table of users and orders sorted by the order amount in descending order.

Using Prepared Statements with LIMIT and OFFSET

Prepared statements are a way to optimize SQL queries, particularly when the same structure of the query is used multiple times with different variables. They can be used with LIMIT and OFFSET to build dynamic pagination or query specific subsets of data repeatedly.

SET @limit_size = 10;
SET @offset_value = 20;
PREPARE statement FROM 'SELECT id, username FROM users ORDER BY id LIMIT ? OFFSET ?;';
EXECUTE statement USING @limit_size, @offset_value;
DEALLOCATE PREPARE statement;

This allows you to change the limit and offset without rewriting the entire query each time.

Optimizing Performance

If you find that using LIMIT and OFFSET is slow, particularly on large tables, consider using a where-clause to constrain the range of the row instead. For example, if your table has a sequential ID, instead of using an OFFSET to skip a large number of rows, you could remember the last ID you processed and query records greater than that ID.

SELECT id, username
FROM users
WHERE id ">" 1000
LIMIT 10;

This could potentially lead to a significant increase in performance, as it avoids the overhead of skipping through many rows.

Conclusion

In this tutorial, we have examined how to use LIMIT and OFFSET in MySQL to retrieve subsets of records. Understanding how to incorporate these clauses effectively can greatly assist developers in managing large datasets and improving response times for database queries.