Join tables

JOIN statements in SQL are used to combine rows from two or more tables based on a related column between them. They allow you to retrieve data that is spread across multiple tables, providing a way to create a comprehensive dataset.

There are different types of JOINs, including INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN, each serving different purposes for how they handle matching and non-matching rows from the involved tables.

To run a JOIN query on your table you can format it by pulling from multiple tables and joining between a column on each of them.

const { data, error } = db
    .selectFrom([
        { table: 'table_1', columns: ['id'] },
        { table: 'table_2', columns: ['column_a'] }
    ])
    .leftJoin('table_1', equalsColumn('table_2.column_a', 'table_1.id'))
    .query()

Last updated