Skip to main content

Overview

Drizzle ORM provides a type-safe query builder for all CRUD operations. Queries are built using a fluent, chainable API that mirrors SQL syntax.

Select Queries

Basic Select

Select all columns and rows from a table:

Select Specific Columns

Choose which columns to return:

Where Conditions

Filter results using conditions:

Ordering

Limit and Offset

Distinct

Joins

Inner Join

Left Join

Multiple Joins

Insert Queries

Insert Single Row

Insert Multiple Rows

Upsert (On Conflict)

Update Queries

Update Rows

Update with Expressions

Delete Queries

Delete Rows

Be careful when deleting without a WHERE clause - it will delete all rows in the table!

Aggregations

Count, Sum, Avg

Group By

Having

Relational Queries

With schema relations defined, use the simpler relational query API:

Subqueries

Raw SQL

Execute raw SQL when needed:

Prepared Statements

Optimize repeated queries:

Best Practices

  • Type safety: Let TypeScript infer types from your queries
  • Prepared statements: Use for frequently executed queries
  • Batch operations: Insert/update multiple rows in a single query
  • Indexes: Add indexes for frequently filtered/joined columns
  • Select only needed columns: Reduces data transfer and processing
  • Always use parameterized queries to prevent SQL injection
  • Be cautious with DELETE and UPDATE without WHERE clauses
  • Use transactions for operations that must succeed or fail together

Next Steps

Relations

Define and query table relationships

Transactions

Learn about database transactions