© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•2y ago•
5 replies
predaytor

Write SQL query in Drizzle?

Hey! How to write a similar query in drizzle? Basically, I need a way to count the total number of pages in a single query (should I?) for offset pagination?
const nodesCount = await context.db.select({ count: count() }).from(user).where(eq(user.role, 'student'));

const users = await context.db.query.user.findMany({
    limit: pageSize,
    offset: pageOffset,
    where: user => eq(user.role, 'student'),
    orderBy: (user, { asc }) => [asc(user.fullName), asc(user.id)],
});
const nodesCount = await context.db.select({ count: count() }).from(user).where(eq(user.role, 'student'));

const users = await context.db.query.user.findMany({
    limit: pageSize,
    offset: pageOffset,
    where: user => eq(user.role, 'student'),
    orderBy: (user, { asc }) => [asc(user.fullName), asc(user.id)],
});

vs. what I found on stackoveflow:
with cte as(
  select count(*) total from table 
)
select *, (select total from cte) total
from table limit 0, 100
with cte as(
  select count(*) total from table 
)
select *, (select total from cte) total
from table limit 0, 100
Stack Overflow
How to count total number of rows when limit is added in android sq...
I am using SQLITE in android and I am having an issue I want to get the total count of row when limit is applied. For example if I apply limit of 100 for pagination but there are around 2000 data. ...
How to count total number of rows when limit is added in android sq...
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

How would you write this SQL query using Drizzle?
Drizzle TeamDTDrizzle Team / help
16mo ago
Drizzle as SQL query generator
Drizzle TeamDTDrizzle Team / help
3y ago
Natively write SQL functions/triggers with Drizzle?
Drizzle TeamDTDrizzle Team / help
16mo ago
Drizzle kit time differences compared to sql query
Drizzle TeamDTDrizzle Team / help
2y ago