Skip to the page

SQL interview question · hard

How can you implement pagination in SQL? Discuss different approaches and their performance implications.

The short answerPagination in SQL is used to break large datasets into smaller, more manageable chunks for efficient querying, especially in web applications.

The full answer

Pagination in SQL is used to break large datasets into smaller, more manageable chunks for efficient querying, especially in web applications. Different approaches to pagination exist, each with varying performance implications.

Approaches for Implementing Pagination

ApproachDescriptionPerformance Considerations
LIMIT & OFFSETSkips rows using OFFSET and retrieves a fixed number of rows using LIMIT.Works well for small datasets but slows down for large offsets due to row skipping.
Keyset Pagination (Seek Method)Uses an indexed column (e.g., id) to fetch the next set of rows.More efficient for large datasets since it avoids scanning skipped rows.
Using ROW_NUMBER()Assigns row numbers and filters based on range conditions.Useful for paginating large datasets efficiently.
Caching & PreloadingStores paginated results in memory for frequently accessed pages.Reduces database load but may serve stale data.

1. Using LIMIT & OFFSET (Basic Approach)

SELECT * FROM orders
ORDER BY order_date DESC
LIMIT 10 OFFSET 20;

Pros:

  • Simple to implement.
  • Works well for small datasets.

Cons:

  • Becomes slow for large offsets since the database must scan and discard skipped rows.
  • Performance degrades as OFFSET increases.

2. Using Keyset Pagination (Seek Method) – More Efficient for Large Datasets

SELECT * FROM orders
WHERE order_date < '2023-12-01'
ORDER BY order_date DESC
LIMIT 10;

Pros:

  • Faster than OFFSET since it does not scan skipped rows.
  • Works well for infinite scrolling and real-time applications.

Cons:

  • Requires an indexed column for efficient filtering.
  • Does not allow jumping to arbitrary pages (only next/previous navigation).

3. Using ROW_NUMBER() for Flexible Pagination

WITH paginated_orders AS (
    SELECT *, ROW_NUMBER() OVER (ORDER BY order_date DESC) AS row_num
    FROM orders
)
SELECT * FROM paginated_orders
WHERE row_num BETWEEN 21 AND 30;

Pros:

  • More efficient than OFFSET for paginating large datasets.
  • Works well when combined with filtering.

Cons:

  • Requires an additional query layer (WITH or subquery).
  • Can be slower than keyset pagination if not properly optimized.

Nuances to Consider

  • Indexing is Essential
    • Indexing on the sorting column (e.g., order_date) significantly improves performance.
  • High Offset Values Can Be Problematic
    • Instead of deep offsets, prefer keyset pagination or caching frequently accessed pages.
  • Jumping Between Pages vs. Infinite Scrolling
    • Keyset pagination is best for infinite scrolling, while ROW_NUMBER() works well for jump-to-page functionality.

Tecyfy Takeaway
Choosing the right pagination approach depends on dataset size, user experience needs, and query performance. While LIMIT & OFFSET is simple, keyset pagination is more efficient for large datasets. Using ROW_NUMBER() provides flexibility, but indexing and query optimization are crucial for maintaining performance.

Last updated 1 February 2025