LIMIT & OFFSET: Concept
Module: SQL Fundamentals
LIMIT restricts the number of rows returned by a query. OFFSET skips a specified number of rows before returning results. Together they enable pagination - loading data in manageable chunks.
**LIMIT Basics:**
- LIMIT n: Return only first n rows
- Always use with ORDER BY for consistent results
- Without ORDER BY, results are unpredictable
**OFFSET for Pagination:**
- OFFSET m: Skip first m rows
- Used with LIMIT for page navigation
- Formula: OFFSET = (page_number - 1) * page_size
**Cross-Database Syntax:**
- MySQL/PostgreSQL/SQLite: LIMIT n OFFSET m
- SQL Server: TOP n, OFFSET-FETCH (2012+)
- Oracle: FETCH FIRST n ROWS ONLY
**Performance Considerations:**
- Large OFFSET is slow (database still scans skipped rows)
- Example: OFFSET 10000 scans 10000 rows before returning results
- Solution: Keyset pagination (WHERE id > last_id)
LIMIT is essential for web development - APIs return paginated results, dashboards load data in chunks, mobile apps use infinite scroll. Understanding pagination performance prevents slow queries at scale. Large OFFSET values cause performance issues - keyset pagination is the solution for deep pages.
Every web application uses pagination - product listings show 20 items per page, search results display 10 per page, infinite scroll loads 50 items at a time. LIMIT and OFFSET enable efficient data loading, preventing slow queries that return millions of rows. Critical for scalable UIs and APIs.