Sorting and Pagination
Objectives
By the end of this lesson, you should be able to:
- Sort query results with
orderBy - Paginate results with
takeandskip - Combine filtering, sorting, and pagination in one query
💡 Why this matters: Node.js & Express Foundations covered pagination, filtering, and sorting for a REST API backed by an in-memory array. This lesson covers the exact same ideas, now backed by a real database.
⚠️ A note on verification: as throughout this module, the Prisma CLI can’t run inside this course’s own sandboxed tooling. The code below reflects Prisma Client’s stable, current, documented query API. Try it yourself, on your own machine, against a real database.
Sorting With orderBy
const students = await prisma.student.findMany({
orderBy: { grade: 'desc' }
});
console.log(students);
[
{ id: 2, name: 'Jordan', grade: 10 },
{ id: 1, name: 'Erin', grade: 9 }
]
orderBy: { grade: 'desc' } is the direct equivalent of SQL’s ORDER BY grade DESC, 'asc' sorts ascending (the default if omitted).
Pagination With take and skip
const page1 = await prisma.student.findMany({
orderBy: { id: 'asc' },
take: 2,
skip: 0
});
const page2 = await prisma.student.findMany({
orderBy: { id: 'asc' },
take: 2,
skip: 2
});
take limits how many rows come back, skip offsets where the results start, the same pattern REST API pagination (Node.js & Express Foundations, Module 9) uses with a page number and page size:
function getPage(pageNumber, pageSize) {
return prisma.student.findMany({
orderBy: { id: 'asc' },
take: pageSize,
skip: (pageNumber - 1) * pageSize
});
}
Combining Filtering, Sorting, and Pagination
All three combine in a single call, exactly the shape a real API endpoint needs:
async function getStudents({ grade, page = 1, pageSize = 10 } = {}) {
return prisma.student.findMany({
where: grade ? { grade } : undefined,
orderBy: { name: 'asc' },
take: pageSize,
skip: (page - 1) * pageSize
});
}
const result = await getStudents({ grade: 10, page: 1, pageSize: 10 });
console.log(result);
[ { id: 2, name: 'Jordan', grade: 10 } ]
where: grade ? { grade } : undefined only applies the filter when grade was actually provided, undefined in a Prisma query object is simply ignored, a clean way to make a filter optional.
Getting a Total Count
Pagination usually needs a total count too, for calculating how many pages exist:
const total = await prisma.student.count({ where: { grade: 10 } });
console.log(total);
1
count takes the same where shape as findMany, and returns just a number.
Try It
- Fetch every student sorted by
gradedescending. - Fetch two pages of results (
take: 2each) usingskip, and confirm they don’t overlap. - Write a function combining
where,orderBy,take, andskipin one call, matching thegetStudentsexample above. - Use
prisma.student.count()to get a total, and calculate how many pages of 10 that total would need.
Recap
orderBy: { field: 'asc' | 'desc' }sorts results,takeandskippaginate them, both compose cleanly withwhere.undefinedin a query object is ignored, a clean way to make a filter conditional.count()returns a total matching awhereclause, without fetching the actual rows, useful for calculating page counts.
Next lesson: comparing this module’s Prisma queries directly against the raw SQL from Modules 1 and 2.