Raw SQL Queries in Prisma

From the Prisma ORM cheat sheet · Transactions & Raw Queries · verified Nov 2026

Raw SQL Queries

Execute raw SQL queries when Prisma Client methods are not sufficient

typescript
import { Prisma } from './generated/prisma/client'

// Raw query with type safety
type User = { id: number; email: string; name: string | null }
const users = await prisma.$queryRaw<User[]>`
  SELECT * FROM "User" 
  WHERE email LIKE ${searchTerm + '%'}
`

// Execute raw SQL (returns affected rows)
const result = await prisma.$executeRaw`
  UPDATE "User" SET "updatedAt" = NOW() 
  WHERE "lastLogin" < NOW() - INTERVAL '30 days'
`

// $queryRawUnsafe with parameters (never interpolate user input into the string)
const query = Prisma.sql`SELECT * FROM "User" WHERE id = ${userId}`
const usersById = await prisma.$queryRawUnsafe(query.text, ...query.values)
💡 Use template literals for safe parameterization
⚠️ $queryRawUnsafe is vulnerable to SQL injection
📌 BigInt values need conversion for JSON
✅ Always validate and sanitize user inputs

Continue with Prisma ORM

Save the full cheat sheet or work through every related task.

More Prisma tasks

Back to the full Prisma ORM cheat sheet