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### findOne()
204
205```ts
206findOne(): QueryBuilder<TContext>
207```
208
209Specify that the query should return a single row as `data` and not an array.
210
211#### Returns
212
213[`QueryBuilder`](../../type-aliases/querybuilder.md)\<`TContext`\>
214
215A QueryBuilder with single return enabled
216
217#### Example
218
219```ts
220// Get an user by ID
221query
222 .from({ users: usersCollection })
223 .where(({users}) => eq(users.id, 1))
224 .findOne()
225```
226
227***
228
229### from()
230
231```ts
232from<TSource>(source): QueryBuilder<{
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```
239
240Defined in: [packages/db/src/query/builder/index.ts:94](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L94)
241
242Specify the source table or subquery for the query
243
244#### Type Parameters
245
246• **TSource** *extends* [`Source`](../../type-aliases/source.md)
247
248#### Parameters
249
250##### source
251
252`TSource`
253
254An object with a single key-value pair where the key is the table alias and the value is a Collection or subquery
255
256#### Returns
257
258[`QueryBuilder`](../../type-aliases/querybuilder.md)\<\{
259 `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\] \};
260 `fromSourceName`: keyof `TSource` & `string`;
261 `hasJoins`: `false`;
262 `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\] \};
263 \}\>
264
265A QueryBuilder with the specified source
266
267#### Example
268
269```ts
270// Query from a collection
271query.from({ users: usersCollection })
272
273// Query from a subquery
274const activeUsers = query.from({ u: usersCollection }).where(({u}) => u.active)
275query.from({ activeUsers })
276```
277
278***
279
280### fullJoin()
281
282```ts
283fullJoin<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">>
284```
285
286Defined in: [packages/db/src/query/builder/index.ts:285](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L285)
287
288Perform a FULL JOIN with another table or subquery
289
290#### Type Parameters
291
292• **TSource** *extends* [`Source`](../../type-aliases/source.md)
293
294#### Parameters
295
296##### source
297
298`TSource`
299
300An object with a single key-value pair where the key is the table alias and the value is a Collection or subquery
301
302##### onCallback
303
304`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\] \}\>\>
305
306A function that receives table references and returns the join condition
307
308#### Returns
309
310[`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"`\>\>
311
312A QueryBuilder with the full joined table available
313
314#### Example
315
316```ts
317// Full join users with posts
318query
319 .from({ users: usersCollection })
320 .fullJoin({ posts: postsCollection }, ({users, posts}) => eq(users.id, posts.userId))
321```
322
323***
324
325### groupBy()
326
327```ts
328groupBy(callback): QueryBuilder<TContext>
329```
330
331Defined in: [packages/db/src/query/builder/index.ts:529](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L529)
332
333Group rows by one or more columns for aggregation
334
335#### Parameters
336
337##### callback
338
339`GroupByCallback`\<`TContext`\>
340
341A function that receives table references and returns the field(s) to group by
342
343#### Returns
344
345[`QueryBuilder`](../../type-aliases/querybuilder.md)\<`TContext`\>
346
347A QueryBuilder with grouping applied (enables aggregate functions in SELECT and HAVING)
348
349#### Example
350
351```ts
352// Group by a single column
353query
354 .from({ posts: postsCollection })
355 .groupBy(({posts}) => posts.userId)
356 .select(({posts, count}) => ({
357 userId: posts.userId,
358 postCount: count()
359 }))
360
361// Group by multiple columns
362query
363 .from({ sales: salesCollection })
364 .groupBy(({sales}) => [sales.region, sales.category])
365 .select(({sales, sum}) => ({
366 region: sales.region,
367 category: sales.category,
368 totalSales: sum(sales.amount)
369 }))
370```
371
372***
373
374### having()
375
376```ts
377having(callback): QueryBuilder<TContext>
378```
379
380Defined in: [packages/db/src/query/builder/index.ts:365](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L365)
381
382Filter grouped rows based on aggregate conditions
383
384#### Parameters
385
386##### callback
387
388`WhereCallback`\<`TContext`\>
389
390A function that receives table references and returns an expression
391
392#### Returns
393
394[`QueryBuilder`](../../type-aliases/querybuilder.md)\<`TContext`\>
395
396A QueryBuilder with the having condition applied
397
398#### Example
399
400```ts
401// Filter groups by count
402query
403 .from({ posts: postsCollection })
404 .groupBy(({posts}) => posts.userId)
405 .having(({posts}) => gt(count(posts.id), 5))
406
407// Filter by average
408query
409 .from({ orders: ordersCollection })
410 .groupBy(({orders}) => orders.customerId)
411 .having(({orders}) => gt(avg(orders.total), 100))
412
413// Multiple having calls are ANDed together
414query
415 .from({ orders: ordersCollection })
416 .groupBy(({orders}) => orders.customerId)
417 .having(({orders}) => gt(count(orders.id), 5))
418 .having(({orders}) => gt(avg(orders.total), 100))
419```
420
421***
422
423### innerJoin()
424
425```ts
426innerJoin<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">>
427```
428
429Defined in: [packages/db/src/query/builder/index.ts:259](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L259)
430
431Perform an INNER JOIN with another table or subquery
432
433#### Type Parameters
434
435• **TSource** *extends* [`Source`](../../type-aliases/source.md)
436
437#### Parameters
438
439##### source
440
441`TSource`
442
443An object with a single key-value pair where the key is the table alias and the value is a Collection or subquery
444
445##### onCallback
446
447`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\] \}\>\>
448
449A function that receives table references and returns the join condition
450
451#### Returns
452
453[`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"`\>\>
454
455A QueryBuilder with the inner joined table available
456
457#### Example
458
459```ts
460// Inner join users with posts
461query
462 .from({ users: usersCollection })
463 .innerJoin({ posts: postsCollection }, ({users, posts}) => eq(users.id, posts.userId))
464```
465
466***
467
468### join()
469
470```ts
471join<TSource, TJoinType>(
472 source,
473 onCallback,
474type): 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>>
475```
476
477Defined in: [packages/db/src/query/builder/index.ts:137](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L137)
478
479Join another table or subquery to the current query
480
481#### Type Parameters
482
483• **TSource** *extends* [`Source`](../../type-aliases/source.md)
484
485• **TJoinType** *extends* `"full"` \| `"left"` \| `"right"` \| `"inner"` = `"left"`
486
487#### Parameters
488
489##### source
490
491`TSource`
492
493An object with a single key-value pair where the key is the table alias and the value is a Collection or subquery
494
495##### onCallback
496
497`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\] \}\>\>
498
499A function that receives table references and returns the join condition
500
501##### type
502
503`TJoinType` = `...`
504
505The type of join: 'inner', 'left', 'right', or 'full' (defaults to 'left')
506
507#### Returns
508
509[`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`\>\>
510
511A QueryBuilder with the joined table available
512
513#### Example
514
515```ts
516// Left join users with posts
517query
518 .from({ users: usersCollection })
519 .join({ posts: postsCollection }, ({users, posts}) => eq(users.id, posts.userId))
520
521// Inner join with explicit type
522query
523 .from({ u: usersCollection })
524 .join({ p: postsCollection }, ({u, p}) => eq(u.id, p.userId), 'inner')
525```
526
527// Join with a subquery
528const activeUsers = query.from({ u: usersCollection }).where(({u}) => u.active)
529query
530 .from({ activeUsers })
531 .join({ p: postsCollection }, ({u, p}) => eq(u.id, p.userId))
532
533***
534
535### leftJoin()
536
537```ts
538leftJoin<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">>
539```
540
541Defined in: [packages/db/src/query/builder/index.ts:207](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L207)
542
543Perform a LEFT JOIN with another table or subquery
544
545#### Type Parameters
546
547• **TSource** *extends* [`Source`](../../type-aliases/source.md)
548
549#### Parameters
550
551##### source
552
553`TSource`
554
555An object with a single key-value pair where the key is the table alias and the value is a Collection or subquery
556
557##### onCallback
558
559`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\] \}\>\>
560
561A function that receives table references and returns the join condition
562
563#### Returns
564
565[`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"`\>\>
566
567A QueryBuilder with the left joined table available
568
569#### Example
570
571```ts
572// Left join users with posts
573query
574 .from({ users: usersCollection })
575 .leftJoin({ posts: postsCollection }, ({users, posts}) => eq(users.id, posts.userId))
576```
577
578***
579
580### limit()
581
582```ts
583limit(count): QueryBuilder<TContext>
584```
585
586Defined in: [packages/db/src/query/builder/index.ts:561](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L561)
587
588Limit the number of rows returned by the query
589`orderBy` is required for `limit`
590
591#### Parameters
592
593##### count
594
595`number`
596
597Maximum number of rows to return
598
599#### Returns
600
601[`QueryBuilder`](../../type-aliases/querybuilder.md)\<`TContext`\>
602
603A QueryBuilder with the limit applied
604
605#### Example
606
607```ts
608// Get top 5 posts by likes
609query
610 .from({ posts: postsCollection })
611 .orderBy(({posts}) => posts.likes, 'desc')
612 .limit(5)
613```
614
615***
616
617### offset()
618
619```ts
620offset(count): QueryBuilder<TContext>
621```
622
623Defined in: [packages/db/src/query/builder/index.ts:585](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L585)
624
625Skip a number of rows before returning results
626`orderBy` is required for `offset`
627
628#### Parameters
629
630##### count
631
632`number`
633
634Number of rows to skip
635
636#### Returns
637
638[`QueryBuilder`](../../type-aliases/querybuilder.md)\<`TContext`\>
639
640A QueryBuilder with the offset applied
641
642#### Example
643
644```ts
645// Get second page of results
646query
647 .from({ posts: postsCollection })
648 .orderBy(({posts}) => posts.createdAt, 'desc')
649 .offset(page * pageSize)
650 .limit(pageSize)
651```
652
653***
654
655### orderBy()
656
657```ts
658orderBy(callback, direction): QueryBuilder<TContext>
659```
660
661Defined in: [packages/db/src/query/builder/index.ts:479](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L479)
662
663Sort the query results by one or more columns
664
665#### Parameters
666
667##### callback
668
669`OrderByCallback`\<`TContext`\>
670
671A function that receives table references and returns the field to sort by
672
673##### direction
674
675`OrderByDirection` = `...`
676
677Sort direction: 'asc' for ascending, 'desc' for descending (defaults to 'asc')
678
679#### Returns
680
681[`QueryBuilder`](../../type-aliases/querybuilder.md)\<`TContext`\>
682
683A QueryBuilder with the ordering applied
684
685#### Example
686
687```ts
688// Sort by a single column
689query
690 .from({ users: usersCollection })
691 .orderBy(({users}) => users.name)
692
693// Sort descending
694query
695 .from({ users: usersCollection })
696 .orderBy(({users}) => users.createdAt, 'desc')
697
698// Multiple sorts (chain orderBy calls)
699query
700 .from({ users: usersCollection })
701 .orderBy(({users}) => users.lastName)
702 .orderBy(({users}) => users.firstName)
703```
704
705***
706
707### rightJoin()
708
709```ts
710rightJoin<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">>
711```
712
713Defined in: [packages/db/src/query/builder/index.ts:233](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L233)
714
715Perform a RIGHT JOIN with another table or subquery
716
717#### Type Parameters
718
719• **TSource** *extends* [`Source`](../../type-aliases/source.md)
720
721#### Parameters
722
723##### source
724
725`TSource`
726
727An object with a single key-value pair where the key is the table alias and the value is a Collection or subquery
728
729##### onCallback
730
731`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\] \}\>\>
732
733A function that receives table references and returns the join condition
734
735#### Returns
736
737[`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"`\>\>
738
739A QueryBuilder with the right joined table available
740
741#### Example
742
743```ts
744// Right join users with posts
745query
746 .from({ users: usersCollection })
747 .rightJoin({ posts: postsCollection }, ({users, posts}) => eq(users.id, posts.userId))
748```
749
750***
751
752### select()
753
754```ts
755select<TSelectObject>(callback): QueryBuilder<{ [K in string | number | symbol]: (Omit<TContext, "result"> & { result: { [K in string | number | symbol]: ResultTypeFromSelect<TSelectObject>[K] } })[K] }>
756```
757
758Defined in: [packages/db/src/query/builder/index.ts:412](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L412)
759
760Select specific columns or computed values from the query
761
762#### Type Parameters
763
764• **TSelectObject** *extends* `Record`\<`string`,
765 \| `BasicExpression`
766 \| `Aggregate`\<`any`\>
767 \| `RefProxy`\<`any`\>
768 \| `RefProxyFor`\<`any`\>\>
769
770#### Parameters
771
772##### callback
773
774(`refs`) => `TSelectObject`
775
776A function that receives table references and returns an object with selected fields or expressions
777
778#### Returns
779
780[`QueryBuilder`](../../type-aliases/querybuilder.md)\<\{ \[K in string \| number \| symbol\]: (Omit\<TContext, "result"\> & \{ result: \{ \[K in string \| number \| symbol\]: ResultTypeFromSelect\<TSelectObject\>\[K\] \} \})\[K\] \}\>
781
782A QueryBuilder that returns only the selected fields
783
784#### Example
785
786```ts
787// Select specific columns
788query
789 .from({ users: usersCollection })
790 .select(({users}) => ({
791 name: users.name,
792 email: users.email
793 }))
794
795// Select with computed values
796query
797 .from({ users: usersCollection })
798 .select(({users}) => ({
799 fullName: concat(users.firstName, ' ', users.lastName),
800 ageInMonths: mul(users.age, 12)
801 }))
802
803// Select with aggregates (requires GROUP BY)
804query
805 .from({ posts: postsCollection })
806 .groupBy(({posts}) => posts.userId)
807 .select(({posts, count}) => ({
808 userId: posts.userId,
809 postCount: count(posts.id)
810 }))
811```
812
813***
814
815### where()
816
817```ts
818where(callback): QueryBuilder<TContext>
819```
820
821Defined in: [packages/db/src/query/builder/index.ts:324](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L324)
822
823Filter rows based on a condition
824
825#### Parameters
826
827##### callback
828
829`WhereCallback`\<`TContext`\>
830
831A function that receives table references and returns an expression
832
833#### Returns
834
835[`QueryBuilder`](../../type-aliases/querybuilder.md)\<`TContext`\>
836
837A QueryBuilder with the where condition applied
838
839#### Example
840
841```ts
842// Simple condition
843query
844 .from({ users: usersCollection })
845 .where(({users}) => gt(users.age, 18))
846
847// Multiple conditions
848query
849 .from({ users: usersCollection })
850 .where(({users}) => and(
851 gt(users.age, 18),
852 eq(users.active, true)
853 ))
854
855// Multiple where calls are ANDed together
856query
857 .from({ users: usersCollection })
858 .where(({users}) => gt(users.age, 18))
859 .where(({users}) => eq(users.active, true))
860```