RTFM: https://www.prisma.io/docs/concepts/components/prisma-client/raw-database-access#tagged-template-helpers

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

const ids = [1, 3, 5, 10, 20];
const result = await prisma.$queryRaw`SELECT * FROM User WHERE id IN (${Prisma.join(
  ids
)})`;
Answer from Michael Dausmann on Stack Overflow
🌐
Prisma
prisma.io › home › raw queries › raw queries › raw queries › raw queries
Raw queries | Prisma Documentation
For relational databases, Prisma Client exposes four methods that allow you to send raw queries. You can use: $queryRaw to return actual records (for example, using SELECT).
Discussions

Execute $queryRaw query from a variable
It would be cool to have either an overloaded signature for prisma.$queryRaw function that accepts a query with values. More on github.com
🌐 github.com
4
June 26, 2023
Queryraw condition joining
Question I need to use a procedure and then filter on this from a sqlserver database. I am having some issues constructing a query that is dynamic and safe. Prisma.join is only for joining values, ... More on github.com
🌐 github.com
2
2
July 23, 2024
How often do you write a raw query using Prisma
you can read more here but you can do aggregations with the query builder https://www.prisma.io/docs/orm/prisma-client/queries/aggregation-grouping-summarizing More on reddit.com
🌐 r/nextjs
15
4
July 8, 2024
Can't order by when using queryRaw and interpolated variable.
const users = await prisma.$queryRaw`SELECT * FROM "user" ORDER BY id DESC LIMIT 5` More on github.com
🌐 github.com
4
February 2, 2022
🌐
Atomic Spin
spin.atomicobject.com › prisma-queryraw
Building an SQL Query from Variables with prisma.$queryRaw
July 19, 2024 - On a recent project, I needed to break out Prisma’s query builder to write a Postgres-specific query. Using prisma.$queryRaw, I was able to write a basic query that returned the data I needed.
🌐
Prisma
prisma.io › blog › announcing-typedsql-make-your-raw-sql-queries-type-safe-with-prisma-orm
Announcing TypedSQL: Make your raw SQL queries type-safe with Prisma ORM
August 27, 2024 - If your SQL query has arguments, they are provided to the query function passed to $queryRawTyped · // only give me conversion results from TrackingEvent version 5 const result = await prisma.$queryRawTyped(conversionByVariantByVersion(5))
🌐
GitHub
github.com › prisma › prisma › issues › 19970
Execute $queryRaw query from a variable · Issue #19970 · prisma/prisma
June 26, 2023 - const query = "SELECT * FROM tasks WHERE user_id = $1" const variables = ["123"] await result = await prisma.$queryRaw(query, variables) // this is impossible
Author   prisma
🌐
GitHub
github.com › prisma › prisma › discussions › 24838
Queryraw condition joining · prisma/prisma · Discussion #24838
July 23, 2024 - const whereClause = [] if (zipCodeLike && zipCodeLike.length > 0) { const clauses = zipCodeLike.map((_, index) => index === 0 ? ` recentZIPCode LIKE ` : ` OR recentZIPCode LIKE `, ); whereClause.push(Prisma.sql([...clauses, ""], ...zipCodeLike)); } if (nameLike && nameLike.length > 0) { const clauses = nameLike.map((_, index) => index === 0 ? ` name LIKE ` : ` OR name LIKE `, ); whereClause.push(Prisma.sql([...clauses, ""], ...nameLike)); } How do I construct the where clause into a queryRaw in a way that they have brackets and AND between them?
Author   prisma
Find elsewhere
🌐
Nodejs-security
nodejs-security.com › blog › prisma-raw-query-sql-injection
Prisma Raw Query Leads to SQL Injection? Yes and No
Another reason why this code is confusing is the use of the raw keyword in the function name. The queryRaw function would suggest that the query is executed as a raw SQL query, which is typically vulnerable to SQL injection attacks.
🌐
MCP Servers
mcpservers.org › home › agent skills library › prisma-client-api-raw-queries
prisma-client-api-raw-queries | Agent Skills Library
const users = await prisma.$queryRaw` SELECT * FROM "User" WHERE email LIKE ${'%@prisma.io'} ` type User = { id: number; email: string; name: string | null } const users = await prisma.$queryRaw<User[]>` SELECT id, email, name FROM "User" WHERE role = ${'ADMIN'} ` Use Prisma.raw() for identifiers (not safe for user input): import { Prisma } from '../generated/client' const column = 'email' const users = await prisma.$queryRaw` SELECT ${Prisma.raw(column)} FROM "User" ` Build queries dynamically: import { Prisma } from '../generated/client' const email = '[email protected]' const query = Prisma
🌐
Reddit
reddit.com › r/nextjs › how often do you write a raw query using prisma
r/nextjs on Reddit: How often do you write a raw query using Prisma
July 8, 2024 -

I want to use an ORM so that it can handle optimization and provide nice abstractions

So I started learning Prisma, but every time I want to do something just little involved I end up needing to write a raw query for it.

for example, how can I write this without using the raw query:

  const rawResults = await db.$queryRaw`
    select
      age,
      round(avg(todep), 2) as avg_phq9,
      round(avg(tosc), 2) as avg_scs,
      round(avg(toas), 2) as avg_asiss,
      round(avg(todep + tosc + toas), 2) as total_score

    from students
    where inter_dom = 'Inter'
    group by age
    order by total_score desc
  `;

I don't want to waste time learning other stuff, I just wanna know is that normal for you guys to use raw queries so often with Prisma.

does Drizzle solve this problem?

I'm aware of the extends and query extensions... if $extends is capable of doing aggregate and all that could be nice.

🌐
GitHub
github.com › prisma › prisma › discussions › 17158
Joined Relations with $queryRaw · prisma/prisma · Discussion #17158
You can type the results of a $queryRaw to match the expected return type of a Prisma model, Prisma does not automatically handle the nesting of related records when using raw queries.
Author   prisma
🌐
Medium
medium.com › javarevisited › prisma-vs-raw-sql-i-measured-query-performance-for-30-days-b97c0ed5aa7d
Prisma vs Raw SQL: I Measured Query Performance for 30 Days. | by Devrim Ozcay |Production System Engineer | Founder | Javarevisited | Medium
January 12, 2026 - Prisma vs Raw SQL: I Measured Query Performance for 30 Days. The ORM everyone loves vs the queries everyone fears — with actual production numbers that surprised me Look, I need to tell you …
🌐
Prisma
prisma.io › home › write your own sql › write your own sql › write your own sql
Write Your Own SQL in Prisma Client | Prisma Documentation
In most cases, TypedSQL allows ... from Prisma Client's excellent user experience. However, since TypedSQL is statically typed, it may not handle certain scenarios, such as dynamically generated WHERE clauses. In these cases, you will need to use $queryRaw or $executeRaw, ...
🌐
GitHub
github.com › prisma › prisma › issues › 11584
Can't order by when using queryRaw and interpolated variable. · Issue #11584 · prisma/prisma
February 2, 2022 - const users = await prisma.$queryRaw`SELECT * FROM "user" ORDER BY id DESC LIMIT 5`
Author   prisma
🌐
Aixlpj
aixlpj.digital › !ik14lyve2e › lxnsgzoc26766.jspx
Prisma queryraw - aixlpj.digital
March 26, 2026 - We cannot provide a description for this page right now
🌐
Idnlym
idnlym.watch › ac1jb4gnq2ectdnowvw2u6766.jsp
Prisma queryraw - idnlym.watch
March 26, 2026 - We cannot provide a description for this page right now
🌐
GitHub
github.com › prisma › prisma › issues › 7142
Revisit prisma.$queryRaw("...") vs. prisma.$queryRaw template literal · Issue #7142 · prisma/prisma
May 18, 2021 - Problem Right now it's really easy to write a raw query that's susceptible to SQL injections // Raw query. Susceptible to SQL injections! prisma.$queryRaw(`SELECT \* FROM User WHERE email = ${email}`); // Prepared query. Not susceptible ...
Author   prisma
🌐
DEV Community
dev.to › jquagliatini › improving-prisma-raw-query-typing-nhd
Improving Prisma raw queries typing. - DEV Community
March 1, 2023 - const prisma = new PrismaClient(); const clientId = '7d2c979c-f356-4ac0-8005-2b1f1f196a31'; const [{ paidAmount }] = await prisma.$queryRaw<readonly [{ paidAmount: bigint }]>` SELECT SUM("${P.Payment.fields.amount}") AS "paidAmount" FROM "${P.Payment.tableName}" WHERE "${P.Payment.fields.clientId}" = ${clientId} `; Of course, this is an extremely simplified use case where Prisma DSL is far better.
🌐
Prisma
prisma.io › client
Prisma Client | Type-Safe Query Builder for Node.js and TypeScript
Generate a type-safe database client from your Prisma schema to query PostgreSQL, MySQL, SQLite, SQL Server, MongoDB, and CockroachDB with confidence.
🌐
GitHub
github.com › prisma › prisma › issues › 3828
Feature request: Allow `$queryRaw` and `$executeRaw` in transactions · Issue #3828 · prisma/prisma
October 1, 2020 - As of today, it's not possible yet, to do the following: await prisma.$transaction([ prisma.$queryRaw`SELECT 1` ]) This is a feature request to both allow $queryRaw and $executeRaw being execut...
Author   prisma