Basic row filtering
Retrieve only rows matching a column comparison.
Drizzle ORM · Module
.where() filters rows in Drizzle ORM queries using SQL expressions such as eq(), and(), and or(). Custom sql expressions can express arbitrary predicates.
.where() adds a SQL filter to a query. Pass a filter operator such as eq() or gt(), combine conditions with and() or or(), or use sql for a custom expression.
db.select().from(table).where(condition)| Name | Type | Description |
|---|---|---|
filter | SQL expression | A Drizzle filter expression, commonly created with operators such as eq, lt, gte, or ne. |
A query builder containing the SQL WHERE condition; executing the builder returns the filtered rows.
.where() to restrict returned rows with Drizzle filter operators.sql expression system.sql expression for arbitrary predicates or reusable operator implementations.and() and or() to represent compound filtering logic..where() only once; combine predicates with and() or or() instead..$dynamic() when a reusable function must add query clauses incrementally.import { eq, and, gt } from 'drizzle-orm';
const adults = await db
.select()
.from(users)
.where(and(eq(users.active, true), gt(users.age, 17)));Filters active users whose age is greater than 17.
Retrieve only rows matching a column comparison.
Require several predicates or allow alternatives.
Implement a predicate not covered by the built-in operators.
.where() do in Drizzle ORM?.where() applies filtering to a query, and its argument is typically a predicate built with operators such as eq, lt, gte, or ne. The resulting SQL contains the corresponding WHERE clause.
Follow-up: How do you express multiple conditions? Use and() or or() to combine filter operators before passing the result to .where().
.where() conditions?Use and() and or() to logically combine filter operators. For example, where(and(eq(users.active, true), gt(users.age, 17))) requires both conditions.
Follow-up: Can .where() be called repeatedly? Normally no; switch the builder to dynamic mode with .$dynamic() when incremental composition is required.
sql with .where()?Drizzle’s filter operators are implemented with the sql function. You can therefore pass a custom sql expression to .where() when the built-in operators do not express the needed filter.
Follow-up: What does this enable beyond custom filtering? It also allows developers to create their own filter operators.
and()or()sqleq()lt()gte()ne().$dynamic()