Query Patterns
Pack:
drizzle-ormParent skill: Drizzle ORM Queries and SQL Source:drizzle-orm/drizzle-orm-queries-and-sql/references/query-patterns.md
Read this when
Section titled “Read this when”- writing CRUD queries
- using joins, filters, operators, or aggregations
- deciding between partial select, relation queries, or SQL expressions
Core query guidance
Section titled “Core query guidance”- Drizzle always lists selected columns explicitly instead of using
select *. - Partial selects are normal and can mix columns with SQL expressions.
- Filter operators are SQL expressions under the hood and parameterize values automatically.
- Use helpers like
count()when possible instead of hand-writing the same raw SQL repeatedly.
Common query surfaces
Section titled “Common query surfaces”select,selectDistinct, and partial selectsinsert,update,delete- joins and subqueries
- filter operators and conditional filters
- order by, limit/offset, cursor pagination
- CTEs via
$with(...) - relation-aware queries via
db.query.<table>or beta-linedb._query.<table>
Footguns
Section titled “Footguns”- If you use
sql<T>, the generic is only a type hint. It does not transform the returned value. - Aggregation values can need explicit casting or
.mapWith(Number)depending on the driver. - Alias SQL expressions used in CTEs and subqueries if they need to be referenced later.
Source map
Section titled “Source map”https://orm.drizzle.team/docs/data-queryinghttps://orm.drizzle.team/docs/selecthttps://orm.drizzle.team/docs/inserthttps://orm.drizzle.team/docs/updatehttps://orm.drizzle.team/docs/deletehttps://orm.drizzle.team/docs/operatorshttps://orm.drizzle.team/docs/joinshttps://orm.drizzle.team/docs/sqlhttps://orm.drizzle.team/docs/query-utilshttps://orm.drizzle.team/docs/guides/conditional-filters-in-queryhttps://orm.drizzle.team/docs/guides/count-rows