</>
Skip to content
MySQL lessons (19/48)

MySQL — Limit

Basic LIMIT

SELECT * FROM users LIMIT 10;

LIMIT with OFFSET

-- Skip first 5, return next 10
SELECT * FROM users LIMIT 10 OFFSET 5;

-- Alternative syntax
SELECT * FROM users LIMIT 5, 10;

LIMIT Examples

-- Get first 10 users
SELECT * FROM users
ORDER BY created_at DESC
LIMIT 10;

-- Pagination (page 2, 10 items per page)
SELECT * FROM users
ORDER BY id
LIMIT 10 OFFSET 10;

-- Get top 5 products by price
SELECT * FROM products
ORDER BY price DESC
LIMIT 5;

-- Random row
SELECT * FROM users
ORDER BY RAND()
LIMIT 1;

Pagination Example

-- Page 1
SELECT * FROM users ORDER BY id LIMIT 10 OFFSET 0;

-- Page 2
SELECT * FROM users ORDER BY id LIMIT 10 OFFSET 10;

-- Page 3
SELECT * FROM users ORDER BY id LIMIT 10 OFFSET 20;

Mini Practice

Write MySQL code that:

  1. Limits results to 10 rows
  2. Uses OFFSET for pagination
  3. Gets top 5 records
  4. Gets a random row

Up Next

Next: Learn about Sorting Data.

Related Topics

Frequently Asked Questions about Limit

What is Limit in MySQL?

Limit is a fundamental concept in MySQL. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Limit?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Limit.

Why is Limit important in MySQL?

Limit is essential for MySQL development. Understanding this concept will help you write better code and solve real-world problems more effectively.