@@ -7,7 +7,7 @@ Join clause in SQL is used to combine 2 or more tables, based on related columns
7
7
Drizzle ORM joins syntax is a balance between the SQL-likeness and type safety.
8
8
9
9
## Join types
10
- Drizzle ORM has APIs for ` INNER JOIN ` , ` FULL JOIN ` , ` LEFT JOIN ` and ` RIGHT JOIN ` .
10
+ Drizzle ORM has APIs for ` INNER JOIN [LATERAL] ` , ` FULL JOIN ` , ` LEFT JOIN [LATERAL] ` , ` RIGHT JOIN ` , ` CROSS JOIN [LATERAL] ` .
11
11
Lets have a quick look at examples based on below table schemas:
12
12
``` typescript copy
13
13
export const users = pgTable (' users' , {
@@ -46,6 +46,31 @@ const result: {
46
46
```
47
47
</Section >
48
48
49
+ ### Left Join Lateral
50
+ <Section >
51
+ ``` typescript copy
52
+ const subquery = db .select ().from (pets ).where (gte (users .age , 16 )).as (' userPets' )
53
+ const result = await db .select ().from (users ).leftJoinLateral (subquery , sql ` true ` )
54
+ ```
55
+ ``` sql
56
+ select ... from " users" left join lateral (select ... from " pets" where " users" ." age" >= 16 ) " userPets" on true
57
+ ```
58
+ ``` typescript
59
+ // result type
60
+ const result: {
61
+ user: {
62
+ id: number ;
63
+ name: string ;
64
+ };
65
+ userPets: {
66
+ id: number ;
67
+ name: string ;
68
+ ownerId: number ;
69
+ } | null ;
70
+ }[];
71
+ ```
72
+ </Section >
73
+
49
74
### Right Join
50
75
<Section >
51
76
``` typescript copy
@@ -69,6 +94,7 @@ const result: {
69
94
}[];
70
95
```
71
96
</Section >
97
+
72
98
### Inner Join
73
99
<Section >
74
100
``` typescript copy
@@ -92,6 +118,32 @@ const result: {
92
118
}[];
93
119
```
94
120
</Section >
121
+
122
+ ### Inner Join Lateral
123
+ <Section >
124
+ ``` typescript copy
125
+ const subquery = db .select ().from (pets ).where (gte (users .age , 16 )).as (' userPets' )
126
+ const result = await db .select ().from (users ).innerJoinLateral (subquery , sql ` true ` )
127
+ ```
128
+ ``` sql
129
+ select ... from " users" inner join lateral (select ... from " pets" where " users" ." age" >= 16 ) " userPets" on true
130
+ ```
131
+ ``` typescript
132
+ // result type
133
+ const result: {
134
+ user: {
135
+ id: number ;
136
+ name: string ;
137
+ };
138
+ userPets: {
139
+ id: number ;
140
+ name: string ;
141
+ ownerId: number ;
142
+ };
143
+ }[];
144
+ ```
145
+ </Section >
146
+
95
147
### Full Join
96
148
<Section >
97
149
``` typescript copy
@@ -116,6 +168,55 @@ const result: {
116
168
```
117
169
</Section >
118
170
171
+ ### Cross Join
172
+ <Section >
173
+ ``` typescript copy
174
+ const result = await db .select ().from (users ).crossJoin (pets )
175
+ ```
176
+ ``` sql
177
+ select ... from " users" cross join " pets"
178
+ ```
179
+ ``` typescript
180
+ // result type
181
+ const result: {
182
+ user: {
183
+ id: number ;
184
+ name: string ;
185
+ };
186
+ pets: {
187
+ id: number ;
188
+ name: string ;
189
+ ownerId: number ;
190
+ };
191
+ }[];
192
+ ```
193
+ </Section >
194
+
195
+ ### Cross Join Lateral
196
+ <Section >
197
+ ``` typescript copy
198
+ const subquery = db .select ().from (pets ).where (gte (users .age , 16 )).as (' userPets' )
199
+ const result = await db .select ().from (users ).crossJoinLateral (subquery )
200
+ ```
201
+ ``` sql
202
+ select ... from " users" cross join lateral (select ... from " pets" where " users" ." age" >= 16 ) " userPets"
203
+ ```
204
+ ``` typescript
205
+ // result type
206
+ const result: {
207
+ user: {
208
+ id: number ;
209
+ name: string ;
210
+ };
211
+ userPets: {
212
+ id: number ;
213
+ name: string ;
214
+ ownerId: number ;
215
+ };
216
+ }[];
217
+ ```
218
+ </Section >
219
+
119
220
## Partial select
120
221
If you need to select a particular subset of fields or to have a flat response type, Drizzle ORM
121
222
supports joins with partial select and will automatically infer return type based on ` .select({ ... }) ` structure.
0 commit comments