Return only the first N rows
Restricting result size
Drizzle ORM · Module
.limit() adds a SQL LIMIT clause and caps the rows returned by a query. Pair it with .offset() for pagination, and use a stable unique ordering for consistent pages.
The .limit() query-builder method adds a SQL LIMIT clause, restricting how many rows a query returns. Combine it with .offset() when skipping rows for pagination; the relational query API also accepts limit values for top-level and nested queries.
.limit(count)| Name | Type | Description |
|---|---|---|
limit | number | SQL placeholder | Maximum number of rows to return; in relational-query configuration, this is supplied as limit. |
offset | number | SQL placeholder | Number of rows to skip when used with pagination. |
A query builder when chaining .limit(); executing the completed query returns rows constrained by the LIMIT clause.
.limit(count) on a select builder to add a SQL LIMIT clause..from(...), with .offset() optional.const rows = await db.select().from(users).limit(10);Returns at most 10 rows from users.
Restricting result size
Page-number pagination
Nested relational loading
.limit() do in a Drizzle select query?.limit() adds a LIMIT clause and caps the number of returned rows. For example, .select().from(users).limit(10) returns at most ten rows.
Follow-up: How would you paginate? Add .offset() for the number of rows to skip, and order by a unique column so page boundaries remain stable.
.limit() and .offset() used together?Use .limit(pageSize).offset((page - 1) * pageSize) to implement page-number pagination. The limit is the page size, while the offset skips earlier rows.
Follow-up: What ordering rule matters? Order by a unique column, or include a unique column after a non-unique sort key.
Relational queries expose limit in the findMany configuration, including nested relations. This lets a top-level query cap its rows and a relation cap its associated rows.
Follow-up: Can prepared relational queries parameterize the limit? Yes; use a SQL placeholder as the limit value and provide it when executing the prepared query.
offsetorderByfindMany$dynamicsql.placeholder