Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Prisma · Function

prisma.$queryRaw

$queryRaw executes one parameterized SQL statement and returns its records as an array. Use its tagged-template form, or Prisma.sql, to interpolate values more safely.

How it works

Use $queryRaw when a relational-database query must return records that Prisma's model API does not express conveniently. It is invoked as a tagged template, so interpolated values are parameterized and the result is an array of database-record objects.

Syntax

prisma.$queryRaw<T>(query: TemplateStringsArray | Prisma.Sql, ...values: any[]): PrismaPromise<T>

Parameters

NameTypeDescription
queryTemplateStringsArray / Prisma.SqlA tagged template containing one SQL statement, with values interpolated as parameters, or a Prisma.sql helper.

Returns

A promise resolving to an array of database-record objects; the exact field shape depends on the SQL query.

Key points

  • $queryRaw belongs to Prisma Client's relational raw-query API.
  • Choose $queryRaw for record-producing SQL such as SELECT, rather than write-oriented statements.
  • The method returns database rows instead of a write-operation count.
  • Its result is an array, with one object for each returned database record.
  • Rows are represented as objects whose properties correspond to the selected database fields.
  • The tagged-template form uses prepared statements to reduce SQL-injection risk when variables are interpolated.
  • Use tagged-template interpolation for values instead of concatenating user input into SQL text.

Common mistakes

  • A single invocation cannot contain multiple SQL statements; separate them into separate calls.
  • Do not interpolate a PostgreSQL table name as a variable; construct such SQL only with an appropriate unsafe method and strict validation.
  • Pass a tagged template or Prisma.sql; a plain runtime string is not accepted by this method.

Example: Parameterized SELECT with $queryRaw

import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

const userId = 1;
const users = await prisma.$queryRaw<Array<{ id: number; email: string }>>`
  SELECT id, email
  FROM "User"
  WHERE id = ${userId}
`;

console.log(users);
await prisma.$disconnect();

Runs a parameterized SELECT and receives the matching rows as an array.

When to use it

Database-specific SQL

A query uses database-specific SQL that is unavailable through Prisma's model query API.

Record retrieval

A SELECT needs to return rows from a relational database.

Custom projections

A query must work with fields or expressions that are difficult to represent through generated Prisma queries.

Interview questions

QWhat does $queryRaw return and when would you use it?

$queryRaw is Prisma Client's raw SQL method for returning records from a relational database. It is used as a tagged template, and its result is an array in which each object represents a database record.

Follow-up: How are interpolated values handled? Tagged-template interpolation is converted into prepared-statement parameters, which helps mitigate SQL injection; SQL syntax itself must still be valid for the target database.

QWhy is the tagged-template form safer than string concatenation?

The method accepts a template string or the Prisma.sql helper. Interpolated variables are escaped and sent through prepared statements, rather than concatenated into the SQL text.

Follow-up: Can a variable represent a table name? Not for PostgreSQL with $queryRaw; dynamic identifiers are not supported by this method.

QWhat limitation applies to the number of SQL statements per call?

Each call can execute only one query. A string containing multiple statements, such as select 1; select 2;, will not work.

Follow-up: What should be done when several statements are required? Issue separate calls, while considering transaction requirements and the database's behavior between them.

  • $executeRaw
  • $queryRawUnsafe
  • $executeRawUnsafe
  • Prisma.sql
  • TypedSQL

Source: Safely using $queryRaw and $queryRawUnsafe. Checked 2026-10-16.