Where clauses

A WHERE clause allows you to add conditionals into your SQL statement that fetch data only if certain criteria is met.

Basic WHERE clause

When defining a where condition in a SQL statement it typically reads like:

SELECT * FROM table_name WHERE id = 100

Each condition typically follows the format of: column operator value. We offer a number of Operators that help pre-compose the proper order and formatting to make it easier.

const { data, error } = db
    .selectFrom([
        { 
            table: 'users', 
            columns: [
                'id', 
                'created_at', 
                'deleted_at'
            ]
        }
    ])
    .where(isNotNull('deleted_at'))
    .query()

Multiple WHERE clauses

You can chain together multiple WHERE clauses to narrow down the rows that are returned.

const { data, error } = db
    .selectFrom([
        { 
            table: 'users', 
            columns: [
                'id', 
                'created_at', 
                'deleted_at'
            ]
        }
    ])
    .where(isNotNull('deleted_at')) // User is not deleted
    .where(equalsNumber('id', 100)) // User ID is 100
    .query()

Last updated