Raw SQL & Dynamic Queries in Drizzle ORM

From the Drizzle ORM cheat sheet · Prepared Statements & Raw SQL · verified Jul 2026

Raw SQL & Dynamic Queries

Use raw SQL expressions and build dynamic queries conditionally.

typescript
import { sql, eq } from 'drizzle-orm';

// Raw SQL query
const result = await db.execute(
  sql`SELECT * FROM users WHERE id = ${userId}`
);
💡 sql tag auto-parameterizes to prevent injection
⚡ Use sql<Type> to type raw SQL results
📌 $dynamic() enables conditional query building
🟢 Drizzle escapes all interpolated values safely
raw-sqldynamicperformance

More Drizzle ORM tasks

Back to the full Drizzle ORM cheat sheet