Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Drizzle ORM · Module

.rightJoin

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.

How it works

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.

Syntax

db.select().from(sourceTable).rightJoin(joinedTable, onCondition)

Parameters

NameTypeDescription
source tabletableThe table passed to from, which supplies the left side of the SQL join.
joined tabletableThe table whose rows are preserved by the right join.
onSQL conditionThe SQL join predicate, such as an equality between related columns.

Returns

A promise resolving to an array of typed row objects; the source-side table may be null while the right-side table is preserved.

Key points

  • The query builder exposes rightJoin as one of Drizzle’s supported join operations.
  • Invoke the method after from, passing the joined table and an equality predicate.
  • The builder maps the method call to a SQL RIGHT JOIN with the supplied condition as its ON clause.
  • Drizzle marks the source-side result object nullable because the right-side table is preserved.
  • The preserved right-side table is represented as a non-null object in the inferred result type.

Common mistakes

  • The row object for the non-preserved side is nullable, so access it with a null check or optional chaining.

Example: Keep every pet with a right join

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.

When to use it

Preserve all dependent rows

Return every record from a dependent table while optionally attaching its related parent.

Handle missing relationships

Represent optional relationships without dropping unmatched rows.

Keep join results type-safe

Use a SQL-like join while retaining inferred nullability in TypeScript.

Interview questions

QWhat does 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.

QWhat is the basic 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".

QWhen would you choose a right join instead of an inner join?

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.

  • innerJoin
  • leftJoin
  • fullJoin
  • crossJoin
  • eq
  • Joins

Source: Joins - Drizzle ORM. Checked 2026-10-16.