Skip to main content
join and leftJoin are methods for connecting multiple tables to query relational data. They support type-safe table joins and complex join conditions.

Basic Usage

INNER JOIN

LEFT JOIN

INNER JOIN: Returns only matching records from both tables LEFT JOIN: Returns all records from the left table (right side can be null)

Table Aliases

Using Aliases

Joining Same Table Multiple Times

Complex Join Conditions

Using Callback Function

You can combine multiple conditions.

OR Conditions

Comparison with Values

Use onVal() instead of on() when comparing a column with a literal value. on() treats its arguments as column references, so onVal() is required to bind values as parameters.

Value Binding (onVal)

onVal safely compares a column with a literal value using parameter binding in join conditions. While on is used for column-to-column comparisons, onVal is used for column-to-value comparisons.
You can also specify an operator.

andOnVal / orOnVal

andOnVal is an explicit AND version of onVal, and orOnVal compares values with OR conditions.
on vs onVal: on("col1", "col2") compares two columns. onVal("col", value) compares a column with a value, binding the value as a SQL parameter.

Subquery Joins

You can join subqueries as tables.

Multiple Table Joins

You can sequentially join multiple tables.

Real-World Examples

Join Condition Grouping

You can group complex join conditions.

NULL Handling (LEFT JOIN)

LEFT JOIN results are automatically inferred as nullable types.

Performance Optimization

1. Select Only Needed Columns

2. Join Order

3. LEFT JOIN vs INNER JOIN

Type Safety

Columns from joined tables support automatic type completion and validation.

Cautions

1. Alias Required (Subqueries)

2. Check Join Condition Tables

3. Duplicate Column Names

4. N+1 Problem

JOIN vs Loader

Puri provides two methods for loading relational data.

JOIN (1:1 Relationship)

Loader (1:N Relationship)

JOIN multiplies rows, so use Loader for 1:N relationships.

Next Steps

where

Filter conditions

select

Select fields to retrieve

order-by

Sort results

Subset

Load relational data with Subsets