SQL OFFSET

What is OFFSET?

OFFSET skips a specified number of rows before returning results.

It is commonly used with LIMIT for pagination.

Basic Syntax

SELECT *
FROM table_name
LIMIT number
OFFSET number;

Example

SELECT *
FROM employees
LIMIT 5 OFFSET 5;

Pagination Example

-- Page 1
SELECT * FROM employees LIMIT 5 OFFSET 0;

-- Page 2
SELECT * FROM employees LIMIT 5 OFFSET 5;

-- Page 3
SELECT * FROM employees LIMIT 5 OFFSET 10;

Using with ORDER BY

SELECT *
FROM employees
ORDER BY salary
LIMIT 5 OFFSET 5;

Full Example

SELECT name, salary
FROM employees
ORDER BY salary DESC
LIMIT 10 OFFSET 20;

Why OFFSET is Important

  • Supports pagination
  • Loads data in chunks
  • Improves performance
  • Used in APIs and web apps

Common Mistakes

  • Not using ORDER BY
  • Using very large OFFSET values
  • Confusing OFFSET with LIMIT

Practice

Return the second page of results using LIMIT and OFFSET.

SELECT *
FROM table_name
ORDER BY column_name
LIMIT 5 OFFSET 5;

Need Help?

Ask the AI if you need help understanding or want to dive deeper in any topic