# Write your own SQL (/docs/orm/v7/prisma-client/using-raw-sql)

> For the complete Prisma documentation index, see [llms.txt](https://www.prisma.io/docs/llms.txt). A markdown version of any docs page is available by appending `.md` to its URL.

Learn how to use raw SQL queries in Prisma Client

Location: ORM > v7 > Prisma Client > Write your own SQL

The Prisma Client API covers most queries, but some situations call for raw SQL.

This can happen for various reasons, such as the need to optimize the performance of a specific query or because your data requirements can't be fully expressed by Prisma Client's query API.

In most cases, [TypedSQL](#writing-type-safe-queries-with-prisma-client-and-typedsql) allows you to express your query in SQL while keeping typed inputs and outputs. 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`](https://www.prisma.io/docs/orm/v7/prisma-client/using-raw-sql/raw-queries#queryraw) or [`$executeRaw`](https://www.prisma.io/docs/orm/v7/prisma-client/using-raw-sql/raw-queries#executeraw), or their unsafe counterparts.

## Writing type-safe queries with Prisma Client and TypedSQL [#writing-type-safe-queries-with-prisma-client-and-typedsql]

### What is TypedSQL? [#what-is-typedsql]

TypedSQL lets you write queries in `.sql` files and import them into your TypeScript code as functions with fully typed inputs and outputs.

With TypedSQL, you can:

1. Write complex SQL queries using familiar syntax
2. Benefit from full IDE support and syntax highlighting for SQL
3. Import your SQL queries as fully typed functions in your TypeScript code
4. Maintain the flexibility of raw SQL with the safety of Prisma's type system

TypedSQL is particularly useful for:

* Complex reporting queries that are difficult to express using Prisma's query API
* Performance-critical operations that require fine-tuned SQL
* Leveraging database-specific features not yet supported in Prisma's API

For a detailed guide on how to get started with TypedSQL, including setup instructions and usage examples, please refer to our [TypedSQL documentation](https://www.prisma.io/docs/orm/v7/prisma-client/using-raw-sql/typedsql).

## Raw queries [#raw-queries]

While not as ergonomic as [TypedSQL](#writing-type-safe-queries-with-prisma-client-and-typedsql), raw queries are still supported and useful when TypedSQL queries are not possible due to features not yet supported in TypedSQL or when the query is dynamically generated.

### Alternative approaches to raw SQL queries in relational databases [#alternative-approaches-to-raw-sql-queries-in-relational-databases]

Prisma ORM supports four methods to execute raw SQL queries in relational databases:

* [`$queryRaw`](https://www.prisma.io/docs/orm/v7/prisma-client/using-raw-sql/raw-queries#queryraw)
* [`$executeRaw`](https://www.prisma.io/docs/orm/v7/prisma-client/using-raw-sql/raw-queries#executeraw)
* [`$queryRawUnsafe`](https://www.prisma.io/docs/orm/v7/prisma-client/using-raw-sql/raw-queries#queryrawunsafe)
* [`$executeRawUnsafe`](https://www.prisma.io/docs/orm/v7/prisma-client/using-raw-sql/raw-queries#executerawunsafe)

These commands are similar to using TypedSQL, but they are not type-safe and are written as strings in your code rather than in dedicated `.sql` files.

### Alternative approaches to raw queries in document databases [#alternative-approaches-to-raw-queries-in-document-databases]

For MongoDB, Prisma ORM supports three methods to execute raw queries:

* [`$runCommandRaw`](https://www.prisma.io/docs/orm/v7/prisma-client/using-raw-sql/raw-queries#runcommandraw)
* [`<model>.findRaw`](https://www.prisma.io/docs/orm/v7/prisma-client/using-raw-sql/raw-queries#findraw)
* [`<model>.aggregateRaw`](https://www.prisma.io/docs/orm/v7/prisma-client/using-raw-sql/raw-queries#aggregateraw)

These methods allow you to execute raw MongoDB commands and queries, providing flexibility when you need to use MongoDB-specific features or optimizations.

`$runCommandRaw` is used to execute database commands, `<model>.findRaw` is used to find documents that match a filter, and `<model>.aggregateRaw` is used for aggregation operations.

Similar to raw queries in relational databases, these methods are not type-safe and require manual handling of the query results.

## Related pages

- [`Fields & types`](https://www.prisma.io/docs/orm/v7/prisma-client/special-fields-and-types): Learn how to use about special fields and types with Prisma Client
- [`Type safety Overview`](https://www.prisma.io/docs/orm/v7/prisma-client/type-safety): Prisma Client provides full type safety for queries, even for partial queries or included relations. This page explains how to leverage the generated types and utilities
- [`What are Client Extensions`](https://www.prisma.io/docs/orm/v7/prisma-client/client-extensions): Extend the functionality of Prisma Client