Skip to main content
Kysely supports all common SQL join types: inner join, left join, right join, full join, cross join, and lateral join. All join methods are available on select, update, and delete query builders.

Inner Join

Simple Inner Join

Simple inner joins can be done by providing a table name and two columns to join:
The generated SQL (PostgreSQL):

Aliased Inner Join

You can give an alias for the joined table:
The generated SQL (PostgreSQL):

Complex Join Conditions

You can provide a function as the second argument to get a join builder for creating more complex joins:
The generated SQL (PostgreSQL):

Subquery Join

You can join a subquery by providing a callback:
The generated SQL (PostgreSQL):

Left Join

Left join works exactly like inner join but adds a LEFT JOIN instead:
The generated SQL (PostgreSQL):
All the same patterns work:

Right Join

Right join works exactly like inner join but adds a RIGHT JOIN instead:
The generated SQL (PostgreSQL):

Full Join

Full join is supported by PostgreSQL, MS SQL Server, and SQLite:
The generated SQL (PostgreSQL):

Cross Join

Cross join produces a Cartesian product of rows:
The generated SQL (PostgreSQL):

Lateral Join

Lateral joins allow subqueries to reference columns from preceding tables:
The generated SQL (PostgreSQL):
You can also use leftJoinLateral:

Join Builder Methods

The join builder (the callback function in complex joins) has the following methods:

on

Add a condition to the ON clause:

onRef

Compare two columns in the ON clause:

onTrue

Add on true (useful for lateral joins):
You can combine multiple conditions:

Joins in Update Queries

PostgreSQL: From Clause

MySQL: Direct Joins

PostgreSQL: Using Join Methods

Joins in Delete Queries

API Reference

Join Methods

  • innerJoin(table, column1, column2) - Simple inner join
  • innerJoin(table, callback) - Inner join with complex conditions
  • leftJoin(table, column1, column2) - Simple left join
  • leftJoin(table, callback) - Left join with complex conditions
  • rightJoin(table, column1, column2) - Simple right join
  • rightJoin(table, callback) - Right join with complex conditions
  • fullJoin(table, column1, column2) - Simple full join
  • fullJoin(table, callback) - Full join with complex conditions
  • crossJoin(table) - Cross join
  • innerJoinLateral(callback, joinCallback) - Lateral inner join
  • leftJoinLateral(callback, joinCallback) - Lateral left join

Join Builder Methods

  • on(column, operator, value) - Add ON condition
  • onRef(column1, operator, column2) - Compare two columns
  • onTrue() - Add ON true