Code has been added to clipboard!

Correct Use of MySQL LIMIT OFFSET Clauses: Limiting and Paginating

Reading time 3 min
Published Aug 8, 2017
Updated Oct 15, 2019

When you start working with a database management system such as MySQL, you inevitably reach the point when the amount of data grows significantly. It becomes hard to handle and use for both the developer and website or application.

MySQL has inbuilt functionalities for making the management easier. With MySQL LIMIT OFFSET clauses, you can control the number of records that are returned. In other words, when writing a specific query, you can use special clauses to limit MySQL data results to a specified number.

This set limitation helps you paginate the results, making it easier for the server to handle information. Using limit is highly recommended for handling large tables.

MySQL LIMIT OFFSET: Main Tips

  • LIMIT is a special clause used to limit MySQL records a particular query can return.
  • It can prove extremely useful if you want to paginate your query results, or manage queries on large tables. Returning significant amounts of data in those cases may affect performance.
  • MySQL OFFSET is used to specify which row should be fetched first.

Usage of LIMIT

Imagine you are building an admin interface and you want to display a list of users. However, it seems endless. You'd like to paginate the query results to make it easier to navigate through.

To limit MySQL query results to just twenty on the first page, you can write a query using the SELECT statement:

$sql = "SELECT * FROM users LIMIT 20";

OFFSET Clause Explained

Say you'd like the second page to display the following twenty results. To do this, we have to use the the MySQL OFFSET clause. It will make the selection start from a specified entry. In our case, we will skip straight to the entry 21. Let's look at the statement below:

$sql = "SELECT * FROM users LIMIT 20 OFFSET 20";

There is also a way to make the statement shorter and achieve the same result. Note that in this case, we write the MySQL SELECT value first and the LIMIT value second:

$sql = "SELECT * FROM users LIMIT 20, 20";

MySQL LIMIT OFFSET: Summary

  • When you only need a query to return a certain number of entries, you can use LIMIT clause to set that limitation.
  • You can use it to paginate table rows or otherwise manage large amounts of information without disturbing the performance.
  • If you want the query to return entries starting from a particular line, you can use OFFSET clause to tell it where it should start.
Tutorial
Introduction
Installation
Syntax
Variable
Superglobals
Data Types
String
Array
Multidimensional Array
Sort Array
Constant
Operators
Cookies
Sessions
DateTime
Error Handling
Exception Handling
File
Write and Create File
File Open, Read and Close
File Upload
Filtering
Redirecting
Advanced Filters
Forms
Form Required Field
Validate Email/URL
Form Validation
Form Action
Function
Prepared Statements
JSON
Calendar
ZIP File
FTP
HTTP Response
DateTime Functions
Error Functions
File Function
Filter
Math Functions
Mail Function
Miscellaneous Functions
Date Format
String Functions
Array Functions
Directory Functions
MySQL Database
MySQL Connection
MySQL Create Database
MySQL Create Table
MySQL Delete Data
MySQL Insert Data
MySQL Get Last Record ID
MySQL Insert Multiple Records
MySQL Select Data
MySQL Limit Data
MySQL Update Data
MySQLi Functions
AJAX and MySQL
AJAX Search
AJAX Poll
RSS Reader
Read XML File in PHP
XML Parser
SimpleXML Parser
SimpleXML: Node and Attribute
Expat XML Parser
DOMDocument
Libxml Functions
SimpleXML Functions
XML Parsing Functions
PHP isset
PHP echo and print
PHP if else and elseif
PHP switch case
PHP include File
PHP while Loop
PHP for and foreach
PHP mail()
PHP explode()
PHP substr()
PHP str_replace()
PHP array_push
PHP count()