Open History to see the last commit
1---
2id: BaseQueryBuilder
3title: BaseQueryBuilder
4---
5
6<!-- DO NOT EDIT: this page is autogenerated from the type comments -->
7
8# Class: BaseQueryBuilder\<TContext\>
9
10Defined in: [packages/db/src/query/builder/index.ts:37](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L37)
11
12## Type Parameters
13
14• **TContext** *extends* [`Context`](../../interfaces/context.md) = [`Context`](../../interfaces/context.md)
15
16## Constructors
17
18### new BaseQueryBuilder()
19
20```ts
21new BaseQueryBuilder<TContext>(query): BaseQueryBuilder<TContext>
22```
23
24Defined in: [packages/db/src/query/builder/index.ts:40](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L40)
25
26#### Parameters
27
28##### query
29
30`Partial`\<`QueryIR`\> = `{}`
31
32#### Returns
33
34[`BaseQueryBuilder`](../basequerybuilder.md)\<`TContext`\>
35
36## Accessors
37
38### fn
39
40#### Get Signature
41
42```ts
43get fn(): object
44```
45
46Defined in: [packages/db/src/query/builder/index.ts:645](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L645)
47
48Functional variants of the query builder
49These are imperative function that are called for ery row.
50Warning: that these cannot be optimized by the query compiler, and may prevent
51some type of optimizations being possible.
52
53##### Example
54
55```ts
56q.fn.select((row) => ({
57 name: row.user.name.toUpperCase(),
58 age: row.user.age + 1,
59}))
60```
61
62##### Returns
63
64`object`
65
66###### having()
67
68Filter grouped rows using a function that operates on each aggregated row
69Warning: This cannot be optimized by the query compiler
70
71###### Parameters
72
73###### callback
74
75(`row`) => `any`
76
77A function that receives an aggregated row and returns a boolean
78
79###### Returns
80
81[`QueryBuilder`](../../type-aliases/querybuilder.md)\<`TContext`\>
82
83A QueryBuilder with functional having filter applied
84
85###### Example
86
87```ts
88// Functional having (not optimized)
89query
90 .from({ posts: postsCollection })
91 .groupBy(({posts}) => posts.userId)
92 .fn.having(row => row.count > 5)
93```
94
95###### select()
96
97Select fields using a function that operates on each row
98Warning: This cannot be optimized by the query compiler
99
100###### Type Parameters
101
102• **TFuncSelectResult**
103
104###### Parameters
105
106###### callback
107
108(`row`) => `TFuncSelectResult`
109
110A function that receives a row and returns the selected value
111
112###### Returns
113
114[`QueryBuilder`](../../type-aliases/querybuilder.md)\<\{ \[K in string \| number \| symbol\]: (Omit\<TContext, "result"\> & \{ result: \{ \[K in string \| number \| symbol\]: TFuncSelectResult\[K\] \} \})\[K\] \}\>
115
116A QueryBuilder with functional selection applied
117
118###### Example
119
120```ts
121// Functional select (not optimized)
122query
123 .from({ users: usersCollection })
124 .fn.select(row => ({
125 name: row.users.name.toUpperCase(),
126 age: row.users.age + 1,
127 }))
128```
129
130###### where()
131
132Filter rows using a function that operates on each row
133Warning: This cannot be optimized by the query compiler
134
135###### Parameters
136
137###### callback
138
139(`row`) => `any`
140
141A function that receives a row and returns a boolean
142
143###### Returns
144
145[`QueryBuilder`](../../type-aliases/querybuilder.md)\<`TContext`\>
146
147A QueryBuilder with functional filtering applied
148
149###### Example
150
151```ts
152// Functional where (not optimized)
153query
154 .from({ users: usersCollection })
155 .fn.where(row => row.users.name.startsWith('A'))
156```
157
158## Methods
159
160### \_getQuery()
161
162```ts
163_getQuery(): QueryIR
164```
165
166Defined in: [packages/db/src/query/builder/index.ts:731](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L731)
167
168#### Returns
169
170`QueryIR`
171
172***
173
174### distinct()
175
176```ts
177distinct(): QueryBuilder<TContext>
178```
179
180Defined in: [packages/db/src/query/builder/index.ts:606](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L606)
181
182Specify that the query should return distinct rows.
183Deduplicates rows based on the selected columns.
184
185#### Returns
186
187[`QueryBuilder`](../../type-aliases/querybuilder.md)\<`TContext`\>
188
189A QueryBuilder with distinct enabled
190
191#### Example
192
193```ts
194// Get countries our users are from
195query
196 .from({ users: usersCollection })
197 .select(({users}) => users.country)
198 .distinct()
199```
200
201***
202
203### from()
204
205```ts
206from<TSource>(source): QueryBuilder<{
207 baseSchema: { [K in string | number | symbol]: { [K in string | number | symbol]: TSource[K] extends CollectionImpl<U, string | number, {}, StandardSchemaV1<unknown, unknown>, U> ? U : TSource[K] extends QueryBuilder<TContext> ? { [K in string | number | symbol]: ((...)[(...)] extends object ? any[any] : (...) extends (...) ? (...) : (...))[K] } : never }[K] };
208 fromSourceName: keyof TSource & string;
209 hasJoins: false;
210 schema: { [K in string | number | symbol]: { [K in string | number | symbol]: TSource[K] extends CollectionImpl<U, string | number, {}, StandardSchemaV1<unknown, unknown>, U> ? U : TSource[K] extends QueryBuilder<TContext> ? { [K in string | number | symbol]: ((...)[(...)] extends object ? any[any] : (...) extends (...) ? (...) : (...))[K] } : never }[K] };
211}>
212```
213
214Defined in: [packages/db/src/query/builder/index.ts:94](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L94)
215
216Specify the source table or subquery for the query
217
218#### Type Parameters
219
220• **TSource** *extends* [`Source`](../../type-aliases/source.md)
221
222#### Parameters
223
224##### source
225
226`TSource`
227
228An object with a single key-value pair where the key is the table alias and the value is a Collection or subquery
229
230#### Returns
231
232[`QueryBuilder`](../../type-aliases/querybuilder.md)\<\{
233 `baseSchema`: \{ \[K in string \| number \| symbol\]: \{ \[K in string \| number \| symbol\]: TSource\[K\] extends CollectionImpl\<U, string \| number, \{\}, StandardSchemaV1\<unknown, unknown\>, U\> ? U : TSource\[K\] extends QueryBuilder\<TContext\> ? \{ \[K in string \| number \| symbol\]: ((...)\[(...)\] extends object ? any\[any\] : (...) extends (...) ? (...) : (...))\[K\] \} : never \}\[K\] \};
234 `fromSourceName`: keyof `TSource` & `string`;
235 `hasJoins`: `false`;
236 `schema`: \{ \[K in string \| number \| symbol\]: \{ \[K in string \| number \| symbol\]: TSource\[K\] extends CollectionImpl\<U, string \| number, \{\}, StandardSchemaV1\<unknown, unknown\>, U\> ? U : TSource\[K\] extends QueryBuilder\<TContext\> ? \{ \[K in string \| number \| symbol\]: ((...)\[(...)\] extends object ? any\[any\] : (...) extends (...) ? (...) : (...))\[K\] \} : never \}\[K\] \};
237 \}\>
238
239A QueryBuilder with the specified source
240
241#### Example
242
243```ts
244// Query from a collection
245query.from({ users: usersCollection })
246
247// Query from a subquery
248const activeUsers = query.from({ u: usersCollection }).where(({u}) => u.active)
249query.from({ activeUsers })
250```
251
252***
253
254### fullJoin()
255
256```ts
257fullJoin<TSource>(source, onCallback): QueryBuilder<MergeContextWithJoinType<TContext, { [K in string | number | symbol]: { [K in string | number | symbol]: TSource[K] extends CollectionImpl<U, string | number, {}, StandardSchemaV1<unknown, unknown>, U> ? U : TSource[K] extends QueryBuilder<TContext> ? { [K in string | number | symbol]: ((...)[(...)] extends object ? any[any] : (...) extends (...) ? (...) : (...))[K] } : never }[K] }, "full">>
258```
259
260Defined in: [packages/db/src/query/builder/index.ts:285](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L285)
261
262Perform a FULL JOIN with another table or subquery
263
264#### Type Parameters
265
266• **TSource** *extends* [`Source`](../../type-aliases/source.md)
267
268#### Parameters
269
270##### source
271
272`TSource`
273
274An object with a single key-value pair where the key is the table alias and the value is a Collection or subquery
275
276##### onCallback
277
278`JoinOnCallback`\<`MergeContext`\<`TContext`, \{ \[K in string \| number \| symbol\]: \{ \[K in string \| number \| symbol\]: TSource\[K\] extends CollectionImpl\<U, string \| number, \{\}, StandardSchemaV1\<unknown, unknown\>, U\> ? U : TSource\[K\] extends QueryBuilder\<TContext\> ? \{ \[K in string \| number \| symbol\]: ((...)\[(...)\] extends object ? any\[any\] : (...) extends (...) ? (...) : (...))\[K\] \} : never \}\[K\] \}\>\>
279
280A function that receives table references and returns the join condition
281
282#### Returns
283
284[`QueryBuilder`](../../type-aliases/querybuilder.md)\<`MergeContextWithJoinType`\<`TContext`, \{ \[K in string \| number \| symbol\]: \{ \[K in string \| number \| symbol\]: TSource\[K\] extends CollectionImpl\<U, string \| number, \{\}, StandardSchemaV1\<unknown, unknown\>, U\> ? U : TSource\[K\] extends QueryBuilder\<TContext\> ? \{ \[K in string \| number \| symbol\]: ((...)\[(...)\] extends object ? any\[any\] : (...) extends (...) ? (...) : (...))\[K\] \} : never \}\[K\] \}, `"full"`\>\>
285
286A QueryBuilder with the full joined table available
287
288#### Example
289
290```ts
291// Full join users with posts
292query
293 .from({ users: usersCollection })
294 .fullJoin({ posts: postsCollection }, ({users, posts}) => eq(users.id, posts.userId))
295```
296
297***
298
299### groupBy()
300
301```ts
302groupBy(callback): QueryBuilder<TContext>
303```
304
305Defined in: [packages/db/src/query/builder/index.ts:529](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L529)
306
307Group rows by one or more columns for aggregation
308
309#### Parameters
310
311##### callback
312
313`GroupByCallback`\<`TContext`\>
314
315A function that receives table references and returns the field(s) to group by
316
317#### Returns
318
319[`QueryBuilder`](../../type-aliases/querybuilder.md)\<`TContext`\>
320
321A QueryBuilder with grouping applied (enables aggregate functions in SELECT and HAVING)
322
323#### Example
324
325```ts
326// Group by a single column
327query
328 .from({ posts: postsCollection })
329 .groupBy(({posts}) => posts.userId)
330 .select(({posts, count}) => ({
331 userId: posts.userId,
332 postCount: count()
333 }))
334
335// Group by multiple columns
336query
337 .from({ sales: salesCollection })
338 .groupBy(({sales}) => [sales.region, sales.category])
339 .select(({sales, sum}) => ({
340 region: sales.region,
341 category: sales.category,
342 totalSales: sum(sales.amount)
343 }))
344```
345
346***
347
348### having()
349
350```ts
351having(callback): QueryBuilder<TContext>
352```
353
354Defined in: [packages/db/src/query/builder/index.ts:365](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L365)
355
356Filter grouped rows based on aggregate conditions
357
358#### Parameters
359
360##### callback
361
362`WhereCallback`\<`TContext`\>
363
364A function that receives table references and returns an expression
365
366#### Returns
367
368[`QueryBuilder`](../../type-aliases/querybuilder.md)\<`TContext`\>
369
370A QueryBuilder with the having condition applied
371
372#### Example
373
374```ts
375// Filter groups by count
376query
377 .from({ posts: postsCollection })
378 .groupBy(({posts}) => posts.userId)
379 .having(({posts}) => gt(count(posts.id), 5))
380
381// Filter by average
382query
383 .from({ orders: ordersCollection })
384 .groupBy(({orders}) => orders.customerId)
385 .having(({orders}) => gt(avg(orders.total), 100))
386
387// Multiple having calls are ANDed together
388query
389 .from({ orders: ordersCollection })
390 .groupBy(({orders}) => orders.customerId)
391 .having(({orders}) => gt(count(orders.id), 5))
392 .having(({orders}) => gt(avg(orders.total), 100))
393```
394
395***
396
397### innerJoin()
398
399```ts
400innerJoin<TSource>(source, onCallback): QueryBuilder<MergeContextWithJoinType<TContext, { [K in string | number | symbol]: { [K in string | number | symbol]: TSource[K] extends CollectionImpl<U, string | number, {}, StandardSchemaV1<unknown, unknown>, U> ? U : TSource[K] extends QueryBuilder<TContext> ? { [K in string | number | symbol]: ((...)[(...)] extends object ? any[any] : (...) extends (...) ? (...) : (...))[K] } : never }[K] }, "inner">>
401```
402
403Defined in: [packages/db/src/query/builder/index.ts:259](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L259)
404
405Perform an INNER JOIN with another table or subquery
406
407#### Type Parameters
408
409• **TSource** *extends* [`Source`](../../type-aliases/source.md)
410
411#### Parameters
412
413##### source
414
415`TSource`
416
417An object with a single key-value pair where the key is the table alias and the value is a Collection or subquery
418
419##### onCallback
420
421`JoinOnCallback`\<`MergeContext`\<`TContext`, \{ \[K in string \| number \| symbol\]: \{ \[K in string \| number \| symbol\]: TSource\[K\] extends CollectionImpl\<U, string \| number, \{\}, StandardSchemaV1\<unknown, unknown\>, U\> ? U : TSource\[K\] extends QueryBuilder\<TContext\> ? \{ \[K in string \| number \| symbol\]: ((...)\[(...)\] extends object ? any\[any\] : (...) extends (...) ? (...) : (...))\[K\] \} : never \}\[K\] \}\>\>
422
423A function that receives table references and returns the join condition
424
425#### Returns
426
427[`QueryBuilder`](../../type-aliases/querybuilder.md)\<`MergeContextWithJoinType`\<`TContext`, \{ \[K in string \| number \| symbol\]: \{ \[K in string \| number \| symbol\]: TSource\[K\] extends CollectionImpl\<U, string \| number, \{\}, StandardSchemaV1\<unknown, unknown\>, U\> ? U : TSource\[K\] extends QueryBuilder\<TContext\> ? \{ \[K in string \| number \| symbol\]: ((...)\[(...)\] extends object ? any\[any\] : (...) extends (...) ? (...) : (...))\[K\] \} : never \}\[K\] \}, `"inner"`\>\>
428
429A QueryBuilder with the inner joined table available
430
431#### Example
432
433```ts
434// Inner join users with posts
435query
436 .from({ users: usersCollection })
437 .innerJoin({ posts: postsCollection }, ({users, posts}) => eq(users.id, posts.userId))
438```
439
440***
441
442### join()
443
444```ts
445join<TSource, TJoinType>(
446 source,
447 onCallback,
448type): QueryBuilder<MergeContextWithJoinType<TContext, { [K in string | number | symbol]: { [K in string | number | symbol]: TSource[K] extends CollectionImpl<U, string | number, {}, StandardSchemaV1<unknown, unknown>, U> ? U : TSource[K] extends QueryBuilder<TContext> ? { [K in string | number | symbol]: ((...)[(...)] extends object ? any[any] : (...) extends (...) ? (...) : (...))[K] } : never }[K] }, TJoinType>>
449```
450
451Defined in: [packages/db/src/query/builder/index.ts:137](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L137)
452
453Join another table or subquery to the current query
454
455#### Type Parameters
456
457• **TSource** *extends* [`Source`](../../type-aliases/source.md)
458
459• **TJoinType** *extends* `"full"` \| `"left"` \| `"right"` \| `"inner"` = `"left"`
460
461#### Parameters
462
463##### source
464
465`TSource`
466
467An object with a single key-value pair where the key is the table alias and the value is a Collection or subquery
468
469##### onCallback
470
471`JoinOnCallback`\<`MergeContext`\<`TContext`, \{ \[K in string \| number \| symbol\]: \{ \[K in string \| number \| symbol\]: TSource\[K\] extends CollectionImpl\<U, string \| number, \{\}, StandardSchemaV1\<unknown, unknown\>, U\> ? U : TSource\[K\] extends QueryBuilder\<TContext\> ? \{ \[K in string \| number \| symbol\]: ((...)\[(...)\] extends object ? any\[any\] : (...) extends (...) ? (...) : (...))\[K\] \} : never \}\[K\] \}\>\>
472
473A function that receives table references and returns the join condition
474
475##### type
476
477`TJoinType` = `...`
478
479The type of join: 'inner', 'left', 'right', or 'full' (defaults to 'left')
480
481#### Returns
482
483[`QueryBuilder`](../../type-aliases/querybuilder.md)\<`MergeContextWithJoinType`\<`TContext`, \{ \[K in string \| number \| symbol\]: \{ \[K in string \| number \| symbol\]: TSource\[K\] extends CollectionImpl\<U, string \| number, \{\}, StandardSchemaV1\<unknown, unknown\>, U\> ? U : TSource\[K\] extends QueryBuilder\<TContext\> ? \{ \[K in string \| number \| symbol\]: ((...)\[(...)\] extends object ? any\[any\] : (...) extends (...) ? (...) : (...))\[K\] \} : never \}\[K\] \}, `TJoinType`\>\>
484
485A QueryBuilder with the joined table available
486
487#### Example
488
489```ts
490// Left join users with posts
491query
492 .from({ users: usersCollection })
493 .join({ posts: postsCollection }, ({users, posts}) => eq(users.id, posts.userId))
494
495// Inner join with explicit type
496query
497 .from({ u: usersCollection })
498 .join({ p: postsCollection }, ({u, p}) => eq(u.id, p.userId), 'inner')
499```
500
501// Join with a subquery
502const activeUsers = query.from({ u: usersCollection }).where(({u}) => u.active)
503query
504 .from({ activeUsers })
505 .join({ p: postsCollection }, ({u, p}) => eq(u.id, p.userId))
506
507***
508
509### leftJoin()
510
511```ts
512leftJoin<TSource>(source, onCallback): QueryBuilder<MergeContextWithJoinType<TContext, { [K in string | number | symbol]: { [K in string | number | symbol]: TSource[K] extends CollectionImpl<U, string | number, {}, StandardSchemaV1<unknown, unknown>, U> ? U : TSource[K] extends QueryBuilder<TContext> ? { [K in string | number | symbol]: ((...)[(...)] extends object ? any[any] : (...) extends (...) ? (...) : (...))[K] } : never }[K] }, "left">>
513```
514
515Defined in: [packages/db/src/query/builder/index.ts:207](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L207)
516
517Perform a LEFT JOIN with another table or subquery
518
519#### Type Parameters
520
521• **TSource** *extends* [`Source`](../../type-aliases/source.md)
522
523#### Parameters
524
525##### source
526
527`TSource`
528
529An object with a single key-value pair where the key is the table alias and the value is a Collection or subquery
530
531##### onCallback
532
533`JoinOnCallback`\<`MergeContext`\<`TContext`, \{ \[K in string \| number \| symbol\]: \{ \[K in string \| number \| symbol\]: TSource\[K\] extends CollectionImpl\<U, string \| number, \{\}, StandardSchemaV1\<unknown, unknown\>, U\> ? U : TSource\[K\] extends QueryBuilder\<TContext\> ? \{ \[K in string \| number \| symbol\]: ((...)\[(...)\] extends object ? any\[any\] : (...) extends (...) ? (...) : (...))\[K\] \} : never \}\[K\] \}\>\>
534
535A function that receives table references and returns the join condition
536
537#### Returns
538
539[`QueryBuilder`](../../type-aliases/querybuilder.md)\<`MergeContextWithJoinType`\<`TContext`, \{ \[K in string \| number \| symbol\]: \{ \[K in string \| number \| symbol\]: TSource\[K\] extends CollectionImpl\<U, string \| number, \{\}, StandardSchemaV1\<unknown, unknown\>, U\> ? U : TSource\[K\] extends QueryBuilder\<TContext\> ? \{ \[K in string \| number \| symbol\]: ((...)\[(...)\] extends object ? any\[any\] : (...) extends (...) ? (...) : (...))\[K\] \} : never \}\[K\] \}, `"left"`\>\>
540
541A QueryBuilder with the left joined table available
542
543#### Example
544
545```ts
546// Left join users with posts
547query
548 .from({ users: usersCollection })
549 .leftJoin({ posts: postsCollection }, ({users, posts}) => eq(users.id, posts.userId))
550```
551
552***
553
554### limit()
555
556```ts
557limit(count): QueryBuilder<TContext>
558```
559
560Defined in: [packages/db/src/query/builder/index.ts:561](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L561)
561
562Limit the number of rows returned by the query
563`orderBy` is required for `limit`
564
565#### Parameters
566
567##### count
568
569`number`
570
571Maximum number of rows to return
572
573#### Returns
574
575[`QueryBuilder`](../../type-aliases/querybuilder.md)\<`TContext`\>
576
577A QueryBuilder with the limit applied
578
579#### Example
580
581```ts
582// Get top 5 posts by likes
583query
584 .from({ posts: postsCollection })
585 .orderBy(({posts}) => posts.likes, 'desc')
586 .limit(5)
587```
588
589***
590
591### offset()
592
593```ts
594offset(count): QueryBuilder<TContext>
595```
596
597Defined in: [packages/db/src/query/builder/index.ts:585](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L585)
598
599Skip a number of rows before returning results
600`orderBy` is required for `offset`
601
602#### Parameters
603
604##### count
605
606`number`
607
608Number of rows to skip
609
610#### Returns
611
612[`QueryBuilder`](../../type-aliases/querybuilder.md)\<`TContext`\>
613
614A QueryBuilder with the offset applied
615
616#### Example
617
618```ts
619// Get second page of results
620query
621 .from({ posts: postsCollection })
622 .orderBy(({posts}) => posts.createdAt, 'desc')
623 .offset(page * pageSize)
624 .limit(pageSize)
625```
626
627***
628
629### orderBy()
630
631```ts
632orderBy(callback, direction): QueryBuilder<TContext>
633```
634
635Defined in: [packages/db/src/query/builder/index.ts:479](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L479)
636
637Sort the query results by one or more columns
638
639#### Parameters
640
641##### callback
642
643`OrderByCallback`\<`TContext`\>
644
645A function that receives table references and returns the field to sort by
646
647##### direction
648
649`OrderByDirection` = `...`
650
651Sort direction: 'asc' for ascending, 'desc' for descending (defaults to 'asc')
652
653#### Returns
654
655[`QueryBuilder`](../../type-aliases/querybuilder.md)\<`TContext`\>
656
657A QueryBuilder with the ordering applied
658
659#### Example
660
661```ts
662// Sort by a single column
663query
664 .from({ users: usersCollection })
665 .orderBy(({users}) => users.name)
666
667// Sort descending
668query
669 .from({ users: usersCollection })
670 .orderBy(({users}) => users.createdAt, 'desc')
671
672// Multiple sorts (chain orderBy calls)
673query
674 .from({ users: usersCollection })
675 .orderBy(({users}) => users.lastName)
676 .orderBy(({users}) => users.firstName)
677```
678
679***
680
681### rightJoin()
682
683```ts
684rightJoin<TSource>(source, onCallback): QueryBuilder<MergeContextWithJoinType<TContext, { [K in string | number | symbol]: { [K in string | number | symbol]: TSource[K] extends CollectionImpl<U, string | number, {}, StandardSchemaV1<unknown, unknown>, U> ? U : TSource[K] extends QueryBuilder<TContext> ? { [K in string | number | symbol]: ((...)[(...)] extends object ? any[any] : (...) extends (...) ? (...) : (...))[K] } : never }[K] }, "right">>
685```
686
687Defined in: [packages/db/src/query/builder/index.ts:233](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L233)
688
689Perform a RIGHT JOIN with another table or subquery
690
691#### Type Parameters
692
693• **TSource** *extends* [`Source`](../../type-aliases/source.md)
694
695#### Parameters
696
697##### source
698
699`TSource`
700
701An object with a single key-value pair where the key is the table alias and the value is a Collection or subquery
702
703##### onCallback
704
705`JoinOnCallback`\<`MergeContext`\<`TContext`, \{ \[K in string \| number \| symbol\]: \{ \[K in string \| number \| symbol\]: TSource\[K\] extends CollectionImpl\<U, string \| number, \{\}, StandardSchemaV1\<unknown, unknown\>, U\> ? U : TSource\[K\] extends QueryBuilder\<TContext\> ? \{ \[K in string \| number \| symbol\]: ((...)\[(...)\] extends object ? any\[any\] : (...) extends (...) ? (...) : (...))\[K\] \} : never \}\[K\] \}\>\>
706
707A function that receives table references and returns the join condition
708
709#### Returns
710
711[`QueryBuilder`](../../type-aliases/querybuilder.md)\<`MergeContextWithJoinType`\<`TContext`, \{ \[K in string \| number \| symbol\]: \{ \[K in string \| number \| symbol\]: TSource\[K\] extends CollectionImpl\<U, string \| number, \{\}, StandardSchemaV1\<unknown, unknown\>, U\> ? U : TSource\[K\] extends QueryBuilder\<TContext\> ? \{ \[K in string \| number \| symbol\]: ((...)\[(...)\] extends object ? any\[any\] : (...) extends (...) ? (...) : (...))\[K\] \} : never \}\[K\] \}, `"right"`\>\>
712
713A QueryBuilder with the right joined table available
714
715#### Example
716
717```ts
718// Right join users with posts
719query
720 .from({ users: usersCollection })
721 .rightJoin({ posts: postsCollection }, ({users, posts}) => eq(users.id, posts.userId))
722```
723
724***
725
726### select()
727
728```ts
729select<TSelectObject>(callback): QueryBuilder<{ [K in string | number | symbol]: (Omit<TContext, "result"> & { result: { [K in string | number | symbol]: ResultTypeFromSelect<TSelectObject>[K] } })[K] }>
730```
731
732Defined in: [packages/db/src/query/builder/index.ts:412](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L412)
733
734Select specific columns or computed values from the query
735
736#### Type Parameters
737
738• **TSelectObject** *extends* `Record`\<`string`,
739 \| `BasicExpression`
740 \| `Aggregate`\<`any`\>
741 \| `RefProxy`\<`any`\>
742 \| `RefProxyFor`\<`any`\>\>
743
744#### Parameters
745
746##### callback
747
748(`refs`) => `TSelectObject`
749
750A function that receives table references and returns an object with selected fields or expressions
751
752#### Returns
753
754[`QueryBuilder`](../../type-aliases/querybuilder.md)\<\{ \[K in string \| number \| symbol\]: (Omit\<TContext, "result"\> & \{ result: \{ \[K in string \| number \| symbol\]: ResultTypeFromSelect\<TSelectObject\>\[K\] \} \})\[K\] \}\>
755
756A QueryBuilder that returns only the selected fields
757
758#### Example
759
760```ts
761// Select specific columns
762query
763 .from({ users: usersCollection })
764 .select(({users}) => ({
765 name: users.name,
766 email: users.email
767 }))
768
769// Select with computed values
770query
771 .from({ users: usersCollection })
772 .select(({users}) => ({
773 fullName: concat(users.firstName, ' ', users.lastName),
774 ageInMonths: mul(users.age, 12)
775 }))
776
777// Select with aggregates (requires GROUP BY)
778query
779 .from({ posts: postsCollection })
780 .groupBy(({posts}) => posts.userId)
781 .select(({posts, count}) => ({
782 userId: posts.userId,
783 postCount: count(posts.id)
784 }))
785```
786
787***
788
789### where()
790
791```ts
792where(callback): QueryBuilder<TContext>
793```
794
795Defined in: [packages/db/src/query/builder/index.ts:324](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L324)
796
797Filter rows based on a condition
798
799#### Parameters
800
801##### callback
802
803`WhereCallback`\<`TContext`\>
804
805A function that receives table references and returns an expression
806
807#### Returns
808
809[`QueryBuilder`](../../type-aliases/querybuilder.md)\<`TContext`\>
810
811A QueryBuilder with the where condition applied
812
813#### Example
814
815```ts
816// Simple condition
817query
818 .from({ users: usersCollection })
819 .where(({users}) => gt(users.age, 18))
820
821// Multiple conditions
822query
823 .from({ users: usersCollection })
824 .where(({users}) => and(
825 gt(users.age, 18),
826 eq(users.active, true)
827 ))
828
829// Multiple where calls are ANDed together
830query
831 .from({ users: usersCollection })
832 .where(({users}) => gt(users.age, 18))
833 .where(({users}) => eq(users.active, true))
834```