You are currently viewing Knex query builder null and notNull where conditions

Knex query builder null and notNull where conditions

This tutorial explains how to use null and notNull conditions in where clause using Knex query builder

A basic syntax of knex query builder with null and notNull conditions

db.select()
.from("table")
.where({})
.whereNull("table.col1")
.whereNotNull("table.col2")

Let’s have an example of order table which have the following columns of order id, order_placed_date, shipped_date and delivered_date.

Order_IdOrder_Placed_DateShipped_DateDelivered_Date
ABC1232024-01-032023-01-042024-01-16
ABC4562024-02-012024-02-02
ABC7802024-02-022024-02-03

We would like to retrieve all orders that where shipped but not yet delivered

db.select("order_id")
.from("order")
.whereNull("order.delivered_date")
.whereNotNull("order.shipped_date")

The null and notNull conditions can be combined with any where conditions.

This Post Has 3 Comments

  1. Szybkie Wiadomości

    Reading this was like listening to a song that fills you with both peace and a desire for deeper understanding.

Leave a Reply