Preserve all dependent rows
Return every record from a dependent table while optionally attaching its related parent.
Drizzle ORM · Module
rightJoin performs a SQL RIGHT JOIN in Drizzle’s type-safe query builder. It preserves every row from the joined table and makes the source-side table nullable when no match exists.
Call rightJoin after from with the table to preserve and an SQL join predicate. Drizzle emits a SQL RIGHT JOIN and infers the preserved table as present while the opposite table can be null.
db.select().from(sourceTable).rightJoin(joinedTable, onCondition)| Name | Type | Description |
|---|---|---|
source table | table | The table passed to from, which supplies the left side of the SQL join. |
joined table | table | The table whose rows are preserved by the right join. |
on | SQL condition | The SQL join predicate, such as an equality between related columns. |
A promise resolving to an array of typed row objects; the source-side table may be null while the right-side table is preserved.
rightJoin as one of Drizzle’s supported join operations.from, passing the joined table and an equality predicate.import { drizzle } from 'drizzle-orm/node-postgres';
import { eq } from 'drizzle-orm';
import { users, pets } from './schema';
const db = drizzle(client);
const result = await db
.select()
.from(users)
.rightJoin(pets, eq(users.id, pets.ownerId));
for (const row of result) {
console.log(row.pets.name, row.users?.name ?? 'unknown owner');
}This keeps every pet in the result and matches a user when the join condition succeeds; the user side is nullable when no matching user exists.
Return every record from a dependent table while optionally attaching its related parent.
Represent optional relationships without dropping unmatched rows.
Use a SQL-like join while retaining inferred nullability in TypeScript.
rightJoin preserve, and how does Drizzle type the result?rightJoin combines the source table with another table using a supplied condition and retains rows from the joined table. In the documented example, pets remains present while users is nullable when there is no match.
Follow-up: How would you avoid nullable fields for a large joined table? Use a nested select object so Drizzle can make the whole table object nullable instead of every field.
rightJoin syntax?The method is used as db.select().from(users).rightJoin(pets, eq(users.id, pets.ownerId)). The predicate expresses the SQL ON condition, and Drizzle translates the operation into a RIGHT JOIN.
Follow-up: What SQL does this produce? It produces a query shaped like select ... from "users" right join "pets" on "users"."id" = "pets"."owner_id".
A right join is useful when the joined table is the complete population that must be returned, even if the source table lacks a related row. For example, it can list all pets while optionally attaching their owners.
Follow-up: When would an inner join be preferable? Choose it when unmatched rows from either side should be excluded.
innerJoinleftJoinfullJoincrossJoineqJoins