To limit the number of rows returned by a MySQL query to 50, use the LIMIT clause. This is one of the most common and efficient ways to control result set size, especially for pagination or performance optimization.
Basic Syntax
SELECT [columns] FROM [table_name] [WHERE conditions] ORDER BY [column] LIMIT 50;
Examples
1. Basic LIMIT (first 50 rows)
SELECT * FROM users LIMIT 50;
Returns the first 50 rows from the users table (order is not guaranteed without ORDER BY).
2. With ORDER BY (top 50 highest-priced products)
SELECT * FROM products ORDER BY price DESC LIMIT 50;
Returns the 50 most expensive products.
3. With WHERE Clause (filter before limiting)
SELECT * FROM logs WHERE severity = 'error' LIMIT 50;
Returns 50 error logs.
4. Pagination (rows 51–100)
Use OFFSET to skip a number of rows:
SELECT * FROM orders ORDER BY date DESC LIMIT 50 OFFSET 50;
Returns the second page of 50 rows (rows 51–100) when sorted by date.
5. Join with LIMIT
SELECT u.name, o.total
FROM users u
JOIN orders o ON u.id = o.user_id
ORDER BY o.total DESC
LIMIT 50;
Returns the 50 highest-value user orders.
Key Notes
-
Order Matters:
Always use ORDER BY with LIMIT if you need consistent results. Without it, the order is arbitrary.
Example:
SELECT * FROM customers ORDER BY created_at ASC LIMIT 50; -- Oldest 50 customers
-
Performance:
LIMIT stops scanning once it finds enough rows, making it fast for large tables.
- For best performance, combine with indexed columns in
WHERE and ORDER BY.
-
Deterministic Results:
Use a unique column in ORDER BY to avoid ambiguous results:
SELECT * FROM products ORDER BY price DESC, id LIMIT 50;
-- Ensures consistent results if prices are tied
-
MySQL-Specific:
LIMIT is MySQL-specific. Other databases use different syntax (e.g., TOP in SQL Server, FETCH FIRST in PostgreSQL).
Common Use Cases
| Use Case | Query Example |
|---|
| Top N Results | SELECT * FROM scores ORDER BY points DESC LIMIT 10; |
| Random Sampling | SELECT * FROM data ORDER BY RAND() LIMIT 50; |
| Data Exploration | SELECT * FROM large_table LIMIT 50; (preview data) |
| Soft Delete Check | SELECT * FROM users WHERE deleted_at IS NOT NULL LIMIT 50; |
| Audit Logs | SELECT * FROM audit_log ORDER BY timestamp DESC LIMIT 50; |
Gotchas
-
No Index on Filtered Column:
If you use WHERE without an index, LIMIT may still scan the entire table.
Fix: Add indexes on frequently filtered or sorted columns:
CREATE INDEX idx_severity ON logs(severity);
-
Large OFFSETs:
Avoid large OFFSET values (e.g., LIMIT 50 OFFSET 100000) for pagination. It can be slow because MySQL scans and discards rows.
Alternative: Use a "seek method" with indexed columns:
SELECT * FROM users
WHERE id > 1000
ORDER BY id
LIMIT 50;
Pro Tip: Use LIMIT 1 for Existence Checks
SELECT 1 FROM users WHERE email = 'test@example.com' LIMIT 1;
Stops scanning after finding the first match, improving performance for existence checks.
Summary
- Use
LIMIT 50 to restrict results to 50 rows.
- Combine with
ORDER BY for predictable sorting.
- Use
OFFSET for pagination (e.g., LIMIT 50 OFFSET 100).
- Always optimize with indexes for performance.