LogoKnex Dart
knex-dartkartikey321/knex-dart 999999

WHERE Clauses

Complete guide to all WHERE clause variants in Knex Dart

WHERE Clauses#

Knex Dart supports 23 WHERE methods covering the major Knex.js WHERE variants.

Basic WHERE#

// Simple equality
knex('users').where('name', '=', 'John');
// where "name" = $1

// Multiple WHERE (AND)
knex('users')
  .where('active', '=', true)
  .where('role', '=', 'admin');
// where "active" = $1 and "role" = $2

OR WHERE#

knex('users')
  .where('role', '=', 'admin')
  .orWhere('role', '=', 'moderator');
// where "role" = $1 or "role" = $2

WHERE IN#

// List of values
knex('users').whereIn('id', [1, 2, 3]);
// where "id" in ($1, $2, $3)

// Subquery
knex('users').whereIn('id',
  knex('orders').select(['user_id'])
);
// where "id" in (select "user_id" from "orders")

WHERE NOT IN#

knex('users').whereNotIn('status', ['banned', 'deleted']);
// where "status" not in ($1, $2)

knex('users').orWhereNotIn('id', [1, 2, 3]);
// or "id" not in ($1, $2, $3)

WHERE NULL#

knex('users').whereNull('deleted_at');
// where "deleted_at" is null

knex('users').whereNotNull('email');
// where "email" is not null

knex('users').orWhereNull('middle_name');
// or "middle_name" is null

WHERE BETWEEN#

knex('users').whereBetween('age', [18, 65]);
// where "age" between $1 and $2

knex('users').whereNotBetween('score', [0, 50]);
// where "score" not between $1 and $2

knex('users').orWhereBetween('created_at', ['2024-01-01', '2024-12-31']);
// or "created_at" between $1 and $2

WHERE COLUMN#

Compare two columns:

knex('users').whereColumn('updated_at', '>', 'created_at');
// where "updated_at" > "created_at"

knex('users').orWhereColumn('first_name', '=', 'last_name');
// or "first_name" = "last_name"

WHERE NOT#

knex('users').whereNot('status', '=', 'deleted');
// where not "status" = $1

knex('users').orWhereNot('active', '=', false);
// or not "active" = $1

WHERE EXISTS#

Check for existence of subquery results:

knex('users').whereExists(
  knex('orders')
    .select([client.raw('1')])
    .whereColumn('orders.user_id', '=', 'users.id')
);
// where exists (select 1 from "orders" where "orders"."user_id" = "users"."id")

knex('users').whereNotExists(
  knex('orders').select([client.raw('1')])
);
// where not exists (select 1 from "orders")

WHERE WRAPPED#

Group conditions:

knex('users').whereWrapped((qb) {
  qb.where('role', '=', 'admin')
    .orWhere('role', '=', 'moderator');
}).where('active', '=', true);
// where ("role" = $1 or "role" = $2) and "active" = $3

Operators#

Supported operators:

  • = - Equals
  • != - Not equals
  • <> - Not equals (SQL standard)
  • < - Less than
  • <= - Less than or equal
  • > - Greater than
  • >= - Greater than or equal
  • like - Pattern matching
  • ilike - Case-insensitive pattern matching (PostgreSQL)
knex('users').where('email', 'like', '%@gmail.com');
// where "email" like $1

Full-Text Search (whereFullText)#

Cross-dialect full text search. Compiles down to to_tsvector in PostgreSQL, MATCH AGAINST in MySQL, and MATCH in SQLite.

knex('articles').whereFullText(['title', 'body'], 'flutter dart');
// PG: where to_tsvector("title" || ' ' || "body") @@ to_tsquery($1)
// MySQL: where match("title", "body") against($1)
// SQLite: where "articles" match $1

JSON Operators (PostgreSQL)#

If using the PostgresClient, you can use Native JSON operators:

knex('users').whereJsonObject('metadata', {'theme': 'dark'});
// where "metadata" @> $1::jsonb AND "metadata" <@ $1::jsonb

knex('users').whereJsonPath('metadata', '\$.name', 'John');
// where jsonb_path_query_first("metadata", $1) = $2

knex('users').whereJsonSupersetOf('roles', ['admin', 'user']);
// where "roles" @> $1::jsonb

knex('users').whereJsonSubsetOf('roles', ['superadmin', 'admin', 'user']);
// where "roles" <@ $1::jsonb

All WHERE Methods#

MethodDescription
where()Basic WHERE
orWhere()OR condition
whereIn()IN list/subquery
whereNotIn()NOT IN
orWhereIn()OR IN
orWhereNotIn()OR NOT IN
whereNull()IS NULL
whereNotNull()IS NOT NULL
orWhereNull()OR IS NULL
orWhereNotNull()OR IS NOT NULL
whereBetween()BETWEEN
whereNotBetween()NOT BETWEEN
orWhereBetween()OR BETWEEN
orWhereNotBetween()OR NOT BETWEEN
whereColumn()Compare columns
orWhereColumn()OR column comparison
whereNot()NOT condition
orWhereNot()OR NOT
whereExists()EXISTS subquery
whereNotExists()NOT EXISTS
orWhereExists()OR EXISTS
orWhereNotExists()OR NOT EXISTS
whereWrapped()Grouped conditions
whereFullText()Full-Text search match
orWhereFullText()OR Full-Text search
whereJsonObject()Match JSON object exactly
whereJsonPath()Query JSON by path
whereJsonSupersetOf()JSON Superset (@>)
whereJsonSubsetOf()JSON Subset (<@)

Total: 29 WHERE methods providing complete flexibility for query filtering.