OFFSET for Pagination in SQL
📖 Scenario: You are working on a simple online bookstore database. The books table contains a list of books with their id, title, and author. You want to show the books to users in pages, each page showing a fixed number of books.
🎯 Goal: Build a SQL query that uses OFFSET to skip a certain number of rows and LIMIT to show a fixed number of books per page. This helps in creating pagination for the book list.
📋 What You'll Learn
Create a table called
books with columns id, title, and author.Insert exactly 5 books with given details into the
books table.Set a variable
page_size to 2 to represent the number of books per page.Write a SQL query to select
title and author from books ordered by id, using LIMIT and OFFSET to get the second page of results.💡 Why This Matters
🌍 Real World
Pagination is used in websites and apps to show large lists of items in smaller chunks, improving user experience and performance.
💼 Career
Knowing how to use <code>LIMIT</code> and <code>OFFSET</code> is essential for database querying and backend development roles.
Progress0 / 4 steps