Page through query results
Skip earlier records while retrieving a bounded result set.
Drizzle ORM · Module
.offset() adds an offset clause to a Drizzle select query, skipping a specified number of rows. It is commonly paired with .limit() and ordering to implement pagination.
.offset() adds an offset clause that skips a specified number of rows before the query returns results. Pair it with .limit() for page-sized results, and order by a unique column so page boundaries remain stable.
.offset(value)| Name | Type | Description |
|---|---|---|
offset | number | Number of rows to skip before returning query results. |
A query builder with an offset clause; when executed, it returns rows after the specified number of rows are skipped.
.offset() with .limit() when a query needs to skip earlier rows and cap the returned page..limit() as the page size and .offset() as the number of rows skipped..orderBy() before .offset() because OFFSET belongs to the ORDER BY clause.import { asc } from "drizzle-orm";
import { users } from "./schema";
const page = 2;
const pageSize = 10;
const rows = await db
.select()
.from(users)
.orderBy(asc(users.id))
.limit(pageSize)
.offset((page - 1) * pageSize);Fetches the second page by skipping the first page's rows and returning the next page-sized batch.
Skip earlier records while retrieving a bounded result set.
Start reading after a known number of rows in an ordered query.
Apply page-number arithmetic in a reusable query helper.
.offset() do in Drizzle?.offset() specifies how many rows to skip before returning results. For pagination, calculate it as (page - 1) * pageSize and combine it with .limit(pageSize).
Follow-up: Why should pagination order by a unique column? Because non-unique ordering can make rows cross page boundaries between executions; append a unique column when needed.
The offset is the number of rows to skip, while the limit is the number of rows to return. A typical page query orders rows, applies the page size with .limit(), and skips prior pages with .offset().
Follow-up: How can a non-unique sort be made safer? Add a unique column, such as the primary key, after the non-unique sort column.
.offset()?In MSSQL, OFFSET and FETCH are part of the ORDER BY clause, so .orderBy() must precede .offset(). Drizzle can then emit an OFFSET clause, optionally followed by .fetch().
Follow-up: What should a reusable MSSQL pagination helper do? Accept a query builder and apply .offset((page - 1) * pageSize).fetch(pageSize) after ordering.
limitorderByfetchfindManydeferred join