Python MySQL Limit
In this page:
cursor.execute("SELECT * FROM table_name LIMIT number")
cursor.execute("SELECT * FROM table_name LIMIT number OFFSET start")
Basic LIMIT Usage
cursor.execute("SELECT * FROM customers LIMIT 5") returns at most the first 5 rows the query would otherwise return, regardless of how many total rows actually match -- useful for quickly previewing a large table without pulling every row.
Example: Basic LIMIT Usage
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute("SELECT * FROM customers LIMIT 5") # caps the result at 5 rows
print("Returns at most 5 rows")
Getting the "Top N" Results
Combining ORDER BY ... DESC with LIMIT n is the standard pattern for retrieving the top N results by some measure -- for example, the 5 most expensive products, or the 10 most recent orders.
Example: Getting the "Top N" Results
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute("SELECT * FROM products ORDER BY price DESC LIMIT 5") # highest prices first, capped at 5
print("The 5 most expensive products")
Skipping Rows with OFFSET
LIMIT n OFFSET m skips the first m matching rows entirely, then returns up to n rows starting after that point -- the fundamental mechanism behind paginated results, like "page 2" of a search results list.
Example: Skipping Rows with OFFSET
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute("SELECT * FROM customers ORDER BY id LIMIT 10 OFFSET 10") # skips the first 10, then takes 10 more
print("Page 2 of results, 10 per page")
Building a Reusable Pagination Function
Wrapping the LIMIT/OFFSET pattern in a small Python function that accepts a page number and page size makes pagination logic reusable and consistent across every place in an application that needs to page through results.
Example: Building a Reusable Pagination Function
def paginate(page_number, page_size=10):
offset = (page_number - 1) * page_size # rows to skip for this page
return f"LIMIT {page_size} OFFSET {offset}"
print(paginate(2))
LIMIT Without OFFSET vs a Full Fetch
Applying LIMIT at the SQL level (letting the database itself return only the needed rows) is far more efficient than fetching an entire table with fetchall() and then slicing the resulting Python list down to size, since the unwanted rows never even cross the network in the first place.
Example: LIMIT Without OFFSET vs a Full Fetch
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute("SELECT * FROM customers LIMIT 5") # efficient: filtered in SQL
print("Only 5 rows ever cross the network")
- Assuming LIMIT alone gives a meaningful 'top N' result without an accompanying ORDER BY -- without sorting first, LIMIT simply returns an arbitrary N rows, not necessarily the ones intended.
- Miscalculating the OFFSET value when implementing pagination, causing rows to be skipped entirely or shown twice across pages.
- Using LIMIT with a very large OFFSET on a huge table, which can be surprisingly slow since MySQL still has to scan past all the skipped rows internally.
- LIMIT n restricts a SELECT statement to returning at most n rows.
- LIMIT n OFFSET m skips the first m rows, then returns up to n rows after that, enabling pagination.
- LIMIT is almost always combined with ORDER BY to get a meaningful "top N" or "most recent N" result.
LIMIT (and OFFSET) syntax is standard MySQL, supported identically across all mysql-connector-python versions.
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first:
- Python MySQL Get Started
- Python MySQL Create Database
- Python MySQL Create Table
- Python MySQL Insert Into Table
- Python MySQL Select From Table
- Python MySQL Select With a Filter
- Python MySQL Order By
- Python MySQL Delete Record
- Python MySQL Drop Table
- Python MySQL Update Table
- Python MySQL Limit
- Python MySQL Join