Exercise 1: One Query for Blog Totals — Possible Solution =========================================================== async function publishedStats(where: Prisma.PostWhereInput = {}) { const s = await prisma.post.aggregate({ where: { published: true, ...where }, _count: { _all: true }, _sum: { viewCount: true }, _avg: { viewCount: true }, _max: { createdAt: true }, }); return { posts: s._count._all, // always a number totalViews: s._sum.viewCount ?? 0, avgViews: s._avg.viewCount === null ? 0 : Math.round(s._avg.viewCount * 10) / 10, latest: s._max.createdAt?.toISOString() ?? null, }; } console.log(await publishedStats()); // { posts: 18, totalViews: 2140, avgViews: 118.9, latest: "2026-09-20T..." } console.log(await publishedStats({ title: { contains: "no-such-title" } })); // { posts: 0, totalViews: 0, avgViews: 0, latest: null } (Import Prisma from your generated client: import { Prisma } from "./generated/prisma/client") WHY THIS WORKS AS AN ANSWER ------------------------------ All four figures come from a single aggregate call, so the database does the work in one round trip. The empty case proves the point from the chapter: _count returns 0 while _sum, _avg and _max return null, so each one gets its own fallback. "latest" is left as null on purpose — there genuinely is no latest post, and a made-up date would mislead.