Negate an equality filter
Exclude records matching an equality predicate in a select query.
Drizzle ORM · Function
not inverts a Drizzle filter condition, allowing a query to select rows that do not satisfy the wrapped predicate. It is imported from drizzle-orm and commonly used inside .where().
not wraps a filter condition and reverses its result, producing a SQL NOT expression. Use it inside .where() to exclude rows matching a condition, such as users whose id equals a particular value.
not(condition)| Name | Type | Description |
|---|---|---|
condition | SQL boolean expression | A Drizzle filter condition to invert. |
A SQL boolean expression representing the negation of the supplied condition.
not is part of Drizzle ORM's built-in filter and conditional operator set.not from the main drizzle-orm package.not as the argument to a query's .where() method.NOT surrounding the wrapped predicate.import { eq, not } from "drizzle-orm";
const rows = await db
.select()
.from(users)
.where(not(eq(users.id, 42)));This selects users whose id is not 42.
Exclude records matching an equality predicate in a select query.
Express the inverse of a compound filter when a direct comparison operator is insufficient.
Build a custom SQL filter using the same negation concept through Drizzle's SQL expression support.
not operator do?not negates a Drizzle filter condition and generates a SQL NOT expression around it. For example, not(eq(table.column, 5)) produces a condition equivalent to NOT (table.column = 5).
Follow-up: How would you negate a compound condition? Wrap the combined condition in not, using Drizzle's conjunction helpers before applying the negation.
not in a select query?Import not from drizzle-orm and pass it an existing condition, such as eq(users.id, 42). The resulting expression can be supplied to .where() to filter query results.
Follow-up: What alternative is shown for writing the same SQL? Use the sql tagged template, for example sql\not ${users.id} = 42\.
not instead of a direct inequality operator?The operator is useful when exclusion is expressed as the inverse of a positive predicate. not(eq(table.column, 5)) excludes rows where the column equals 5 and is rendered as NOT (table.column = 5).
Follow-up: When should a dedicated operator be preferred? Use operators such as ne when the intended comparison is directly inequality; use not when negating a broader condition.
eqnenotExistsandorsql