Join Overview
INNER JOIN
Data existing in both tablesjoin()
LEFT JOIN
Left table based, right is OptionalleftJoin()
Self JOIN
Join same tableUsing alias
M:N JOIN
Many-to-many relationship joinUsing junction table
INNER JOIN
Queries only data that exists in both tables.Basic JOIN
When to use INNER JOIN:
- When the relationship is required (NOT NULL foreign key)
- When data must exist in both tables
- Example: employees → users (all employees must have a user account)
Multiple Table JOIN
LEFT JOIN
Includes all data from the left table, and includes if available from the right table.Basic LEFT JOIN
When to use LEFT JOIN:
- When the relationship is optional (NULLABLE foreign key)
- When all data from the left table is needed
- Example: employees → departments (some employees may not have a department)
INNER JOIN vs LEFT JOIN Comparison
Complex LEFT JOIN
Mixed INNER + LEFT JOIN
Use INNER JOIN for required relationships, LEFT JOIN for optional ones.Self JOIN - Self Reference
Use aliases when joining the same table.Department Hierarchy Example
| childId | childName | parentId | parentName |
|---|---|---|---|
| 1 | Headquarters | null | null |
| 2 | Dev Team | 1 | Headquarters |
| 3 | Design Team | 1 | Headquarters |
| 4 | Backend Team | 2 | Dev Team |
User Referrer Example
Many-to-Many Join
Many-to-many (M:N) relationships are joined through junction tables.Projects ↔ Employees Example
- 1 project with N employees → N rows
- Projects without employees → 1 row (employee fields are null)
M:N Grouped by Employee
Subquery Join
Join subqueries as if they were tables.Department Statistics Join
Latest Login Join
Join + WHERE Conditions
Pre-join Filtering
Post-join Aggregation
Practical Examples
User Profile Query
Project Details + Member List
Department Hierarchy Query
Type Safety
Puri’s joins are type-safe.LEFT JOIN and types:
- Columns from LEFT JOIN tables are
T | nulltype - Columns from INNER JOIN tables are
Ttype - TypeScript automatically enforces null checks