kysely
Version:
Type safe SQL query builder
1,612 lines • 65.2 kB
TypeScript
import { CompiledQuery } from '../query-compiler/compiled-query.js';
import { JoinCallbackExpression, JoinReferenceExpression } from '../parser/join-parser.js';
import { TableExpression } from '../parser/table-parser.js';
import { SelectExpression, Selection, AllSelection, SelectCallback, CallbackSelection } from '../parser/select-parser.js';
import { ReferenceExpression } from '../parser/reference-parser.js';
import { SelectQueryNode } from '../operation-node/select-query-node.js';
import { QueryNode } from '../operation-node/query-node.js';
import { DrainOuterGeneric, NarrowPartial, Nullable, ShallowRecord, Simplify, SimplifySingleResult, SqlBool } from '../util/type-utils.js';
import { OrderByDirectionExpression, OrderByExpression, DirectedOrderByStringReference, UndirectedOrderByExpression } from '../parser/order-by-parser.js';
import { Compilable } from '../util/compilable.js';
import { QueryExecutor } from '../query-executor/query-executor.js';
import { QueryId } from '../util/query-id.js';
import { GroupByArg } from '../parser/group-by-parser.js';
import { KyselyPlugin } from '../plugin/kysely-plugin.js';
import { WhereInterface } from './where-interface.js';
import { NoResultErrorConstructor } from './no-result-error.js';
import { HavingInterface } from './having-interface.js';
import { Explainable, ExplainFormat } from '../util/explainable.js';
import { SetOperandExpression } from '../parser/set-operation-parser.js';
import { AliasedExpression, Expression } from '../expression/expression.js';
import { ComparisonOperatorExpression, OperandValueExpressionOrList } from '../parser/binary-operation-parser.js';
import { KyselyTypeError } from '../util/type-error.js';
import { Selectable } from '../util/column-type.js';
import { Streamable } from '../util/streamable.js';
import { ExpressionOrFactory } from '../parser/expression-parser.js';
import { ExpressionWrapper } from '../expression/expression-wrapper.js';
import { SelectQueryBuilderExpression } from './select-query-builder-expression.js';
import { ValueExpression } from '../parser/value-parser.js';
import { FetchModifier } from '../operation-node/fetch-node.js';
import { TopModifier } from '../operation-node/top-node.js';
export interface SelectQueryBuilder<DB, TB extends keyof DB, O> extends WhereInterface<DB, TB>, HavingInterface<DB, TB>, SelectQueryBuilderExpression<O>, Compilable<O>, Explainable, Streamable<O> {
/**
* Adds a `where` expression to the query.
*
* Calling this method multiple times will combine the expressions using `and`.
*
* Also see {@link whereRef}
*
* ### Examples
*
* <!-- siteExample("where", "Simple where clause", 10) -->
*
* `where` method calls are combined with `AND`:
*
* ```ts
* const person = await db
* .selectFrom('person')
* .selectAll()
* .where('first_name', '=', 'Jennifer')
* .where('age', '>', 40)
* .executeTakeFirst()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select * from "person" where "first_name" = $1 and "age" > $2
* ```
*
* Operator can be any supported operator or if the typings don't support it
* you can always use:
*
* ```ts
* sql`your operator`
* ```
*
* <!-- siteExample("where", "Where in", 20) -->
*
* Find multiple items using a list of identifiers:
*
* ```ts
* const persons = await db
* .selectFrom('person')
* .selectAll()
* .where('id', 'in', ['1', '2', '3'])
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select * from "person" where "id" in ($1, $2, $3)
* ```
*
* <!-- siteExample("where", "Object filter", 30) -->
*
* You can use the `and` function to create a simple equality
* filter using an object
*
* ```ts
* const persons = await db
* .selectFrom('person')
* .selectAll()
* .where((eb) => eb.and({
* first_name: 'Jennifer',
* last_name: eb.ref('first_name')
* }))
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select *
* from "person"
* where (
* "first_name" = $1
* and "last_name" = "first_name"
* )
* ```
*
* <!-- siteExample("where", "OR where", 40) -->
*
* To combine conditions using `OR`, you can use the expression builder.
* There are two ways to create `OR` expressions. Both are shown in this
* example:
*
* ```ts
* const persons = await db
* .selectFrom('person')
* .selectAll()
* // 1. Using the `or` method on the expression builder:
* .where((eb) => eb.or([
* eb('first_name', '=', 'Jennifer'),
* eb('first_name', '=', 'Sylvester')
* ]))
* // 2. Chaining expressions using the `or` method on the
* // created expressions:
* .where((eb) =>
* eb('last_name', '=', 'Aniston').or('last_name', '=', 'Stallone')
* )
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select *
* from "person"
* where (
* ("first_name" = $1 or "first_name" = $2)
* and
* ("last_name" = $3 or "last_name" = $4)
* )
* ```
*
* <!-- siteExample("where", "Conditional where calls", 50) -->
*
* You can add expressions conditionally like this:
*
* ```ts
* import { Expression, SqlBool } from 'kysely'
*
* const firstName: string | undefined = 'Jennifer'
* const lastName: string | undefined = 'Aniston'
* const under18 = true
* const over60 = true
*
* let query = db
* .selectFrom('person')
* .selectAll()
*
* if (firstName) {
* // The query builder is immutable. Remember to reassign
* // the result back to the query variable.
* query = query.where('first_name', '=', firstName)
* }
*
* if (lastName) {
* query = query.where('last_name', '=', lastName)
* }
*
* if (under18 || over60) {
* // Conditional OR expressions can be added like this.
* query = query.where((eb) => {
* const ors: Expression<SqlBool>[] = []
*
* if (under18) {
* ors.push(eb('age', '<', 18))
* }
*
* if (over60) {
* ors.push(eb('age', '>', 60))
* }
*
* return eb.or(ors)
* })
* }
*
* const persons = await query.execute()
* ```
*
* Both the first and third argument can also be arbitrary expressions like
* subqueries. An expression can defined by passing a function and calling
* the methods of the {@link ExpressionBuilder} passed to the callback:
*
* ```ts
* const persons = await db
* .selectFrom('person')
* .selectAll()
* .where(
* (qb) => qb.selectFrom('pet')
* .select('pet.name')
* .whereRef('pet.owner_id', '=', 'person.id')
* .limit(1),
* '=',
* 'Fluffy'
* )
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select *
* from "person"
* where (
* select "pet"."name"
* from "pet"
* where "pet"."owner_id" = "person"."id"
* limit $1
* ) = $2
* ```
*
* A `where in` query can be built by using the `in` operator and an array
* of values. The values in the array can also be expressions:
*
* ```ts
* const persons = await db
* .selectFrom('person')
* .selectAll()
* .where('person.id', 'in', [100, 200, 300])
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select * from "person" where "id" in ($1, $2, $3)
* ```
*
* <!-- siteExample("where", "Complex where clause", 60) -->
*
* For complex `where` expressions you can pass in a single callback and
* use the `ExpressionBuilder` to build your expression:
*
* ```ts
* const firstName = 'Jennifer'
* const maxAge = 60
*
* const persons = await db
* .selectFrom('person')
* .selectAll('person')
* .where(({ eb, or, and, not, exists, selectFrom }) => and([
* or([
* eb('first_name', '=', firstName),
* eb('age', '<', maxAge)
* ]),
* not(exists(
* selectFrom('pet')
* .select('pet.id')
* .whereRef('pet.owner_id', '=', 'person.id')
* ))
* ]))
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select "person".*
* from "person"
* where (
* (
* "first_name" = $1
* or "age" < $2
* )
* and not exists (
* select "pet"."id" from "pet" where "pet"."owner_id" = "person"."id"
* )
* )
* ```
*
* If everything else fails, you can always use the {@link sql} tag
* as any of the arguments, including the operator:
*
* ```ts
* import { sql } from 'kysely'
*
* const persons = await db
* .selectFrom('person')
* .selectAll()
* .where(
* sql`coalesce(first_name, last_name)`,
* 'like',
* '%' + name + '%',
* )
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select * from "person"
* where coalesce(first_name, last_name) like $1
* ```
*
* In all examples above the columns were known at compile time
* (except for the raw {@link sql} expressions). By default kysely only
* allows you to refer to columns that exist in the database **and**
* can be referred to in the current query and context.
*
* Sometimes you may want to refer to columns that come from the user
* input and thus are not available at compile time.
*
* You have two options, the {@link sql} tag or `db.dynamic`. The example below
* uses both:
*
* ```ts
* import { sql } from 'kysely'
* const { ref } = db.dynamic
*
* const persons = await db
* .selectFrom('person')
* .selectAll()
* .where(ref(columnFromUserInput), '=', 1)
* .where(sql.id(columnFromUserInput), '=', 2)
* .execute()
* ```
*/
where<RE extends ReferenceExpression<DB, TB>, VE extends OperandValueExpressionOrList<DB, TB, RE>>(lhs: RE, op: ComparisonOperatorExpression, rhs: VE): SelectQueryBuilder<DB, TB, O>;
where<E extends ExpressionOrFactory<DB, TB, SqlBool>>(expression: E): SelectQueryBuilder<DB, TB, O>;
/**
* Adds a `where` clause where both sides of the operator are references
* to columns.
*
* The normal `where` method treats the right hand side argument as a
* value by default. `whereRef` treats it as a column reference. This method is
* expecially useful with joins and correlated subqueries.
*
* ### Examples
*
* Usage with a join:
*
* ```ts
* db.selectFrom(['person', 'pet'])
* .selectAll()
* .whereRef('person.first_name', '=', 'pet.name')
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select * from "person", "pet" where "person"."first_name" = "pet"."name"
* ```
*
* Usage in a subquery:
*
* ```ts
* const persons = await db
* .selectFrom('person')
* .selectAll('person')
* .select((eb) => eb
* .selectFrom('pet')
* .select('name')
* .whereRef('pet.owner_id', '=', 'person.id')
* .limit(1)
* .as('pet_name')
* )
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select "person".*, (
* select "name"
* from "pet"
* where "pet"."owner_id" = "person"."id"
* limit $1
* ) as "pet_name"
* from "person"
*/
whereRef<LRE extends ReferenceExpression<DB, TB>, RRE extends ReferenceExpression<DB, TB>>(lhs: LRE, op: ComparisonOperatorExpression, rhs: RRE): SelectQueryBuilder<DB, TB, O>;
/**
* Just like {@link WhereInterface.where | where} but adds a `having` statement
* instead of a `where` statement.
*/
having<RE extends ReferenceExpression<DB, TB>, VE extends OperandValueExpressionOrList<DB, TB, RE>>(lhs: RE, op: ComparisonOperatorExpression, rhs: VE): SelectQueryBuilder<DB, TB, O>;
having<E extends ExpressionOrFactory<DB, TB, SqlBool>>(expression: E): SelectQueryBuilder<DB, TB, O>;
/**
* Just like {@link WhereInterface.whereRef | whereRef} but adds a `having` statement
* instead of a `where` statement.
*/
havingRef<LRE extends ReferenceExpression<DB, TB>, RRE extends ReferenceExpression<DB, TB>>(lhs: LRE, op: ComparisonOperatorExpression, rhs: RRE): SelectQueryBuilder<DB, TB, O>;
/**
* Adds a select statement to the query.
*
* When a column (or any expression) is selected, Kysely adds its type to the return
* type of the query. Kysely is smart enough to parse the selection names and types
* from aliased columns, subqueries, raw expressions etc.
*
* Kysely only allows you to select columns and expressions that exist and would
* produce valid SQL. However, Kysely is not perfect and there may be cases where
* the type inference doesn't work and you need to override it. You can always
* use the {@link Kysely.dynamic | dynamic} module and the {@link sql} tag
* to override the types.
*
* Select calls are additive. Calling `select('id').select('first_name')` is the
* same as calling `select(['id', 'first_name'])`.
*
* To select all columns of the query or specific tables see the
* {@link selectAll} method.
*
* See the {@link $if} method if you are looking for a way to add selections
* based on a runtime condition.
*
* ### Examples
*
* <!-- siteExample("select", "A single column", 10) -->
*
* Select a single column:
*
* ```ts
* const persons = await db
* .selectFrom('person')
* .select('id')
* .where('first_name', '=', 'Arnold')
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select "id" from "person" where "first_name" = $1
* ```
*
* <!-- siteExample("select", "Column with a table", 20) -->
*
* Select a single column and specify a table:
*
* ```ts
* const persons = await db
* .selectFrom(['person', 'pet'])
* .select('person.id')
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select "person"."id" from "person", "pet"
* ```
*
* <!-- siteExample("select", "Multiple columns", 30) -->
*
* Select multiple columns:
*
* ```ts
* const persons = await db
* .selectFrom('person')
* .select(['person.id', 'first_name'])
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select "person"."id", "first_name" from "person"
* ```
*
* <!-- siteExample("select", "Aliases", 40) -->
*
* You can give an alias for selections and tables by appending `as the_alias` to the name:
*
* ```ts
* const persons = await db
* .selectFrom('person as p')
* .select([
* 'first_name as fn',
* 'p.last_name as ln'
* ])
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select
* "first_name" as "fn",
* "p"."last_name" as "ln"
* from "person" as "p"
* ```
*
* <!-- siteExample("select", "Complex selections", 50) -->
*
* You can select arbitrary expression including subqueries and raw sql snippets.
* When you do that, you need to give a name for the selections using the `as` method:
*
* ```ts
* import { sql } from 'kysely'
*
* const persons = await db.selectFrom('person')
* .select(({ eb, selectFrom, or }) => [
* // Select a correlated subquery
* selectFrom('pet')
* .whereRef('person.id', '=', 'pet.owner_id')
* .select('pet.name')
* .orderBy('pet.name')
* .limit(1)
* .as('first_pet_name'),
*
* // Build and select an expression using
* // the expression builder
* or([
* eb('first_name', '=', 'Jennifer'),
* eb('first_name', '=', 'Arnold')
* ]).as('is_jennifer_or_arnold'),
*
* // Select a raw sql expression
* sql<string>`concat(first_name, ' ', last_name)`.as('full_name')
* ])
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select
* (
* select "pet"."name"
* from "pet"
* where "person"."id" = "pet"."owner_id"
* order by "pet"."name"
* limit $1
* ) as "pet_name",
* ("first_name" = $2 or "first_name" = $3) as "jennifer_or_arnold",
* concat(first_name, ' ', last_name) as "full_name"
* from "person"
* ```
*
* In case you use the {@link sql} tag you need to specify the type of the expression
* (in this example `string`).
*
* All the examples above assume you know the column names at compile time.
* While it's better to build your code like that (that way you also know
* the types) sometimes it's not possible or you just prefer to write more
* dynamic code.
* <br><br>
* In this example, we use the `dynamic` module's methods to add selections
* dynamically:
*
* ```ts
* const { ref } = db.dynamic
*
* // Some column name provided by the user. Value not known at compile time.
* const columnFromUserInput = req.query.select;
*
* // A type that lists all possible values `columnFromUserInput` can have.
* // You can use `keyof Person` if any column of an interface is allowed.
* type PossibleColumns = 'last_name' | 'first_name' | 'birth_date'
*
* const spersons = await db
* .selectFrom('person')
* .select([
* ref<PossibleColumns>(columnFromUserInput)
* 'id'
* ])
* .execute()
*
* // The resulting type contains all `PossibleColumns` as optional fields
* // because we cannot know which field was actually selected before
* // running the code.
* const lastName: string | undefined = persons[0].last_name
* const firstName: string | undefined = persons[0].first_name
* const birthDate: string | undefined = persons[0].birth_date
*
* // The result type also contains the compile time selection `id`.
* persons[0].id
* ```
*/
select<SE extends SelectExpression<DB, TB>>(selections: ReadonlyArray<SE>): SelectQueryBuilder<DB, TB, O & Selection<DB, TB, SE>>;
select<CB extends SelectCallback<DB, TB>>(callback: CB): SelectQueryBuilder<DB, TB, O & CallbackSelection<DB, TB, CB>>;
select<SE extends SelectExpression<DB, TB>>(selection: SE): SelectQueryBuilder<DB, TB, O & Selection<DB, TB, SE>>;
/**
* Adds `distinct on` expressions to the select clause.
*
* ### Examples
*
* <!-- siteExample("select", "Distinct on", 80) -->
*
* ```ts
* const persons = await db.selectFrom('person')
* .innerJoin('pet', 'pet.owner_id', 'person.id')
* .where('pet.name', '=', 'Doggo')
* .distinctOn('person.id')
* .selectAll('person')
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select distinct on ("person"."id") "person".*
* from "person"
* inner join "pet" on "pet"."owner_id" = "person"."id"
* where "pet"."name" = $1
* ```
*/
distinctOn<RE extends ReferenceExpression<DB, TB>>(selections: ReadonlyArray<RE>): SelectQueryBuilder<DB, TB, O>;
distinctOn<RE extends ReferenceExpression<DB, TB>>(selection: RE): SelectQueryBuilder<DB, TB, O>;
/**
* This can be used to add any additional SQL to the front of the query __after__ the `select` keyword.
*
* ### Examples
*
* ```ts
* db.selectFrom('person')
* .modifyFront(sql`sql_no_cache`)
* .select('first_name')
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* select sql_no_cache `first_name`
* from `person`
* ```
*/
modifyFront(modifier: Expression<any>): SelectQueryBuilder<DB, TB, O>;
/**
* This can be used to add any additional SQL to the end of the query.
*
* Also see {@link forUpdate}, {@link forShare}, {@link forKeyShare}, {@link forNoKeyUpdate}
* {@link skipLocked} and {@link noWait}.
*
* ### Examples
*
* ```ts
* db.selectFrom('person')
* .select('first_name')
* .modifyEnd(sql`for update`)
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select "first_name"
* from "person"
* for update
* ```
*/
modifyEnd(modifier: Expression<any>): SelectQueryBuilder<DB, TB, O>;
/**
* Makes the selection distinct.
*
* <!-- siteExample("select", "Distinct", 70) -->
*
* ### Examples
*
* ```ts
* const persons = await db.selectFrom('person')
* .select('first_name')
* .distinct()
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select distinct "first_name" from "person"
* ```
*/
distinct(): SelectQueryBuilder<DB, TB, O>;
/**
* Adds the `for update` modifier to a select query on supported databases.
*/
forUpdate(of?: TableOrList<TB>): SelectQueryBuilder<DB, TB, O>;
/**
* Adds the `for share` modifier to a select query on supported databases.
*/
forShare(of?: TableOrList<TB>): SelectQueryBuilder<DB, TB, O>;
/**
* Adds the `for key share` modifier to a select query on supported databases.
*/
forKeyShare(of?: TableOrList<TB>): SelectQueryBuilder<DB, TB, O>;
/**
* Adds the `for no key update` modifier to a select query on supported databases.
*/
forNoKeyUpdate(of?: TableOrList<TB>): SelectQueryBuilder<DB, TB, O>;
/**
* Adds the `skip locked` modifier to a select query on supported databases.
*/
skipLocked(): SelectQueryBuilder<DB, TB, O>;
/**
* Adds the `nowait` modifier to a select query on supported databases.
*/
noWait(): SelectQueryBuilder<DB, TB, O>;
/**
* Adds a `select *` or `select table.*` clause to the query.
*
* ### Examples
*
* <!-- siteExample("select", "All columns", 90) -->
*
* The `selectAll` method generates `SELECT *`:
*
* ```ts
* const persons = await db
* .selectFrom('person')
* .selectAll()
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select * from "person"
* ```
*
* <!-- siteExample("select", "All columns of a table", 100) -->
*
* Select all columns of a table:
*
* ```ts
* const persons = await db
* .selectFrom('person')
* .selectAll('person')
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select "person".* from "person"
* ```
*
* Select all columns of multiple tables:
*
* ```ts
* const personsPets = await db
* .selectFrom(['person', 'pet'])
* .selectAll(['person', 'pet'])
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select "person".*, "pet".* from "person", "pet"
* ```
*/
selectAll<T extends TB>(table: ReadonlyArray<T>): SelectQueryBuilder<DB, TB, O & AllSelection<DB, T>>;
selectAll<T extends TB>(table: T): SelectQueryBuilder<DB, TB, O & Selectable<DB[T]>>;
selectAll(): SelectQueryBuilder<DB, TB, O & AllSelection<DB, TB>>;
/**
* Joins another table to the query using an inner join.
*
* ### Examples
*
* <!-- siteExample("join", "Simple inner join", 10) -->
*
* Simple inner joins can be done by providing a table name and two columns to join:
*
* ```ts
* const result = await db
* .selectFrom('person')
* .innerJoin('pet', 'pet.owner_id', 'person.id')
* // `select` needs to come after the call to `innerJoin` so
* // that you can select from the joined table.
* .select(['person.id', 'pet.name as pet_name'])
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select "person"."id", "pet"."name" as "pet_name"
* from "person"
* inner join "pet"
* on "pet"."owner_id" = "person"."id"
* ```
*
* <!-- siteExample("join", "Aliased inner join", 20) -->
*
* You can give an alias for the joined table like this:
*
* ```ts
* await db.selectFrom('person')
* .innerJoin('pet as p', 'p.owner_id', 'person.id')
* .where('p.name', '=', 'Doggo')
* .selectAll()
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select *
* from "person"
* inner join "pet" as "p"
* on "p"."owner_id" = "person"."id"
* where "p".name" = $1
* ```
*
* <!-- siteExample("join", "Complex join", 30) -->
*
* You can provide a function as the second argument to get a join
* builder for creating more complex joins. The join builder has a
* bunch of `on*` methods for building the `on` clause of the join.
* There's basically an equivalent for every `where` method
* (`on`, `onRef` etc.).
*
* You can do all the same things with the
* `on` method that you can with the corresponding `where` method (like [OR expressions for example](https://kysely.dev/docs/examples/WHERE/or-where)).
* See the `where` method documentation for more examples.
*
* ```ts
* await db.selectFrom('person')
* .innerJoin(
* 'pet',
* (join) => join
* .onRef('pet.owner_id', '=', 'person.id')
* .on('pet.name', '=', 'Doggo')
* .on((eb) => eb.or([eb("person.age", ">", 18), eb("person.age", "<", 100)]))
* )
* .selectAll()
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select *
* from "person"
* inner join "pet"
* on "pet"."owner_id" = "person"."id"
* and "pet"."name" = $1
* ```
*
* <!-- siteExample("join", "Subquery join", 40) -->
*
* You can join a subquery by providing two callbacks:
*
* ```ts
* const result = await db.selectFrom('person')
* .innerJoin(
* (eb) => eb
* .selectFrom('pet')
* .select(['owner_id as owner', 'name'])
* .where('name', '=', 'Doggo')
* .as('doggos'),
* (join) => join
* .onRef('doggos.owner', '=', 'person.id'),
* )
* .selectAll('doggos')
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select "doggos".*
* from "person"
* inner join (
* select "owner_id" as "owner", "name"
* from "pet"
* where "name" = $1
* ) as "doggos"
* on "doggos"."owner" = "person"."id"
* ```
*/
innerJoin<TE extends TableExpression<DB, TB>, K1 extends JoinReferenceExpression<DB, TB, TE>, K2 extends JoinReferenceExpression<DB, TB, TE>>(table: TE, k1: K1, k2: K2): SelectQueryBuilderWithInnerJoin<DB, TB, O, TE>;
innerJoin<TE extends TableExpression<DB, TB>, FN extends JoinCallbackExpression<DB, TB, TE>>(table: TE, callback: FN): SelectQueryBuilderWithInnerJoin<DB, TB, O, TE>;
/**
* Just like {@link innerJoin} but adds a left join instead of an inner join.
*/
leftJoin<TE extends TableExpression<DB, TB>, K1 extends JoinReferenceExpression<DB, TB, TE>, K2 extends JoinReferenceExpression<DB, TB, TE>>(table: TE, k1: K1, k2: K2): SelectQueryBuilderWithLeftJoin<DB, TB, O, TE>;
leftJoin<TE extends TableExpression<DB, TB>, FN extends JoinCallbackExpression<DB, TB, TE>>(table: TE, callback: FN): SelectQueryBuilderWithLeftJoin<DB, TB, O, TE>;
/**
* Just like {@link innerJoin} but adds a right join instead of an inner join.
*/
rightJoin<TE extends TableExpression<DB, TB>, K1 extends JoinReferenceExpression<DB, TB, TE>, K2 extends JoinReferenceExpression<DB, TB, TE>>(table: TE, k1: K1, k2: K2): SelectQueryBuilderWithRightJoin<DB, TB, O, TE>;
rightJoin<TE extends TableExpression<DB, TB>, FN extends JoinCallbackExpression<DB, TB, TE>>(table: TE, callback: FN): SelectQueryBuilderWithRightJoin<DB, TB, O, TE>;
/**
* Just like {@link innerJoin} but adds a full join instead of an inner join.
*/
fullJoin<TE extends TableExpression<DB, TB>, K1 extends JoinReferenceExpression<DB, TB, TE>, K2 extends JoinReferenceExpression<DB, TB, TE>>(table: TE, k1: K1, k2: K2): SelectQueryBuilderWithFullJoin<DB, TB, O, TE>;
fullJoin<TE extends TableExpression<DB, TB>, FN extends JoinCallbackExpression<DB, TB, TE>>(table: TE, callback: FN): SelectQueryBuilderWithFullJoin<DB, TB, O, TE>;
/**
* Just like {@link innerJoin} but adds a lateral join instead of an inner join.
*
* ### Examples
*
* ```ts
* db.selectFrom('person')
* .innerJoinLateral(
* (eb) =>
* eb.selectFrom('pet')
* .select('name')
* .whereRef('pet.owner_id', '=', 'person.id')
* .as('p'),
* (join) => join.onTrue()
* )
* .select(['first_name', 'p.name'])
* .orderBy('first_name')
* ```
*/
innerJoinLateral<TE extends TableExpression<DB, TB>, K1 extends JoinReferenceExpression<DB, TB, TE>, K2 extends JoinReferenceExpression<DB, TB, TE>>(table: TE, k1: K1, k2: K2): SelectQueryBuilderWithInnerJoin<DB, TB, O, TE>;
innerJoinLateral<TE extends TableExpression<DB, TB>, FN extends JoinCallbackExpression<DB, TB, TE>>(table: TE, callback: FN): SelectQueryBuilderWithInnerJoin<DB, TB, O, TE>;
/**
* Just like {@link innerJoin} but adds a lateral left join instead of an inner join.
* ### Examples
*
* ```ts
* db.selectFrom('person')
* .leftJoinLateral(
* (eb) =>
* eb.selectFrom('pet')
* .select('name')
* .whereRef('pet.owner_id', '=', 'person.id')
* .as('p'),
* (join) => join.onTrue()
* )
* .select(['first_name', 'p.name'])
* .orderBy('first_name')
* ```
*/
leftJoinLateral<TE extends TableExpression<DB, TB>, K1 extends JoinReferenceExpression<DB, TB, TE>, K2 extends JoinReferenceExpression<DB, TB, TE>>(table: TE, k1: K1, k2: K2): SelectQueryBuilderWithLeftJoin<DB, TB, O, TE>;
leftJoinLateral<TE extends TableExpression<DB, TB>, FN extends JoinCallbackExpression<DB, TB, TE>>(table: TE, callback: FN): SelectQueryBuilderWithLeftJoin<DB, TB, O, TE>;
/**
* Adds an `order by` clause to the query.
*
* `orderBy` calls are additive. Meaning, additional `orderBy` calls append to
* the existing order by clause.
*
* In a single call you can add a single column/expression or multiple columns/expressions.
*
* Single column/expression calls can have 1-2 arguments. The first argument is
* the expression to order by (optionally including the direction) while the second
* optional argument is the direction (`asc` or `desc`).
*
* ### Examples
*
* Single column/expression per call:
*
* ```ts
* await db
* .selectFrom('person')
* .select('person.first_name as fn')
* .orderBy('id')
* .orderBy('fn desc')
* .execute()
* ```
*
* ```ts
* await db
* .selectFrom('person')
* .select('person.first_name as fn')
* .orderBy('id')
* .orderBy('fn', 'desc')
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select "person"."first_name" as "fn"
* from "person"
* order by "id" asc, "fn" desc
* ```
*
* Multiple columns/expressions per call:
*
* ```ts
* await db
* .selectFrom('person')
* .select('person.first_name as fn')
* .orderBy(['id', 'fn desc'])
* .execute()
* ```
*
* The order by expression can also be a raw sql expression or a subquery
* in addition to column references:
*
* ```ts
* import { sql } from 'kysely'
*
* await db
* .selectFrom('person')
* .selectAll()
* .orderBy((eb) => eb.selectFrom('pet')
* .select('pet.name')
* .whereRef('pet.owner_id', '=', 'person.id')
* .limit(1)
* )
* .orderBy(
* sql`concat(first_name, last_name)`
* )
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select *
* from "person"
* order by
* ( select "pet"."name"
* from "pet"
* where "pet"."owner_id" = "person"."id"
* limit 1
* ) asc,
* concat(first_name, last_name) asc
* ```
*
* `dynamic.ref` can be used to refer to columns not known at
* compile time:
*
* ```ts
* async function someQuery(orderBy: string) {
* const { ref } = db.dynamic
*
* return await db
* .selectFrom('person')
* .select('person.first_name as fn')
* .orderBy(ref(orderBy))
* .execute()
* }
*
* someQuery('fn')
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select "person"."first_name" as "fn"
* from "person"
* order by "fn" asc
* ```
*/
orderBy<OE extends UndirectedOrderByExpression<DB, TB, O>>(orderBy: OE, direction?: OrderByDirectionExpression): SelectQueryBuilder<DB, TB, O>;
orderBy<OE extends DirectedOrderByStringReference<DB, TB, O>>(ref: OE): SelectQueryBuilder<DB, TB, O>;
orderBy<OE extends OrderByExpression<DB, TB, O>>(refs: ReadonlyArray<OE>): SelectQueryBuilder<DB, TB, O>;
/**
* Adds a `group by` clause to the query.
*
* ### Examples
*
* ```ts
* import { sql } from 'kysely'
*
* await db
* .selectFrom('person')
* .select([
* 'first_name',
* sql`max(id)`.as('max_id')
* ])
* .groupBy('first_name')
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select "first_name", max(id)
* from "person"
* group by "first_name"
* ```
*
* `groupBy` also accepts an array:
*
* ```ts
* import { sql } from 'kysely'
*
* await db
* .selectFrom('person')
* .select([
* 'first_name',
* 'last_name',
* sql`max(id)`.as('max_id')
* ])
* .groupBy([
* 'first_name',
* 'last_name'
* ])
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select "first_name", "last_name", max(id)
* from "person"
* group by "first_name", "last_name"
* ```
*
* The group by expressions can also be subqueries or
* raw sql expressions:
*
* ```ts
* import { sql } from 'kysely'
*
* await db
* .selectFrom('person')
* .select([
* 'first_name',
* 'last_name',
* sql`max(id)`.as('max_id')
* ])
* .groupBy([
* sql`concat(first_name, last_name)`,
* (qb) => qb.selectFrom('pet').select('id').limit(1)
* ])
* .execute()
* ```
*
* `dynamic.ref` can be used to refer to columns not known at
* compile time:
*
* ```ts
* async function someQuery(groupBy: string) {
* const { ref } = db.dynamic
*
* return await db
* .selectFrom('person')
* .select('first_name')
* .groupBy(ref(groupBy))
* .execute()
* }
*
* someQuery('first_name')
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select "first_name"
* from "person"
* group by "first_name"
* ```
*/
groupBy<GE extends GroupByArg<DB, TB, O>>(groupBy: GE): SelectQueryBuilder<DB, TB, O>;
/**
* Adds a limit clause to the query.
*
* ### Examples
*
* Select the first 10 rows of the result:
*
* ```ts
* return await db
* .selectFrom('person')
* .select('first_name')
* .limit(10)
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select "first_name" from "person" limit $1
* ```
*
* Select rows from index 10 to index 19 of the result:
*
* ```ts
* return await db
* .selectFrom('person')
* .select('first_name')
* .limit(10)
* .offset(10)
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select "first_name" from "person" limit $1 offset $2
* ```
*/
limit(limit: ValueExpression<DB, TB, number | bigint>): SelectQueryBuilder<DB, TB, O>;
/**
* Adds an `offset` clause to the query.
*
* ### Examples
*
* Select rows from index 10 to index 19 of the result:
*
* ```ts
* return await db
* .selectFrom('person')
* .select('first_name')
* .limit(10)
* .offset(10)
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select "first_name" from "person" limit $1 offset $2
* ```
*/
offset(offset: ValueExpression<DB, TB, number | bigint>): SelectQueryBuilder<DB, TB, O>;
/**
* Adds a `fetch` clause to the query.
*
* This clause is only supported by some dialects like PostgreSQL or MS SQL Server.
*
* ### Examples
*
* ```ts
* return await db
* .selectFrom('person')
* .select('first_name')
* .orderBy('first_name')
* .offset(0)
* .fetch(10)
* .execute()
* ```
*
* The generated SQL (MS SQL Server):
*
* ```sql
* select "first_name"
* from "person"
* order by "first_name"
* offset 0 rows
* fetch next 10 rows only
* ```
*/
fetch(rowCount: number | bigint, modifier?: FetchModifier): SelectQueryBuilder<DB, TB, O>;
/**
* Adds a `top` clause to the query.
*
* This clause is only supported by some dialects like MS SQL Server.
*
* ### Examples
*
* Select 10 biggest ages:
*
* ```ts
* return await db
* .selectFrom('person')
* .select('age')
* .top(10)
* .orderBy('age desc')
* .execute()
* ```
*
* The generated SQL (MS SQL Server):
*
* ```sql
* select top(10) "age" from "person" order by "age" desc
* ```
*
* Select 10% first rows:
*
* ```ts
* return await db
* .selectFrom('person')
* .selectAll()
* .top(10, 'percent')
* .execute()
* ```
*
* The generated SQL (MS SQL Server):
*
* ```sql
* select top(10) percent * from "person"
* ```
*/
top(expression: number | bigint, modifiers?: TopModifier): SelectQueryBuilder<DB, TB, O>;
/**
* Combines another select query or raw expression to this query using `union`.
*
* The output row type of the combined query must match `this` query.
*
* ### Examples
*
* ```ts
* db.selectFrom('person')
* .select(['id', 'first_name as name'])
* .union(db.selectFrom('pet').select(['id', 'name']))
* .orderBy('name')
* ```
*
* You can provide a callback to get an expression builder.
* In the following example, this allows us to wrap the query in parentheses:
*
* ```ts
* db.selectFrom('person')
* .select(['id', 'first_name as name'])
* .union((eb) => eb.parens(
* eb.selectFrom('pet').select(['id', 'name'])
* ))
* .orderBy('name')
* ```
*/
union<E extends SetOperandExpression<DB, O>>(expression: E): SelectQueryBuilder<DB, TB, O>;
/**
* Combines another select query or raw expression to this query using `union all`.
*
* The output row type of the combined query must match `this` query.
*
* ### Examples
*
* ```ts
* db.selectFrom('person')
* .select(['id', 'first_name as name'])
* .unionAll(db.selectFrom('pet').select(['id', 'name']))
* .orderBy('name')
* ```
*
* You can provide a callback to get an expression builder.
* In the following example, this allows us to wrap the query in parentheses:
*
* ```ts
* db.selectFrom('person')
* .select(['id', 'first_name as name'])
* .unionAll((eb) => eb.parens(
* eb.selectFrom('pet').select(['id', 'name'])
* ))
* .orderBy('name')
* ```
*/
unionAll<E extends SetOperandExpression<DB, O>>(expression: E): SelectQueryBuilder<DB, TB, O>;
/**
* Combines another select query or raw expression to this query using `intersect`.
*
* The output row type of the combined query must match `this` query.
*
* ### Examples
*
* ```ts
* db.selectFrom('person')
* .select(['id', 'first_name as name'])
* .intersect(db.selectFrom('pet').select(['id', 'name']))
* .orderBy('name')
* ```
*
* You can provide a callback to get an expression builder.
* In the following example, this allows us to wrap the query in parentheses:
*
* ```ts
* db.selectFrom('person')
* .select(['id', 'first_name as name'])
* .intersect((eb) => eb.parens(
* eb.selectFrom('pet').select(['id', 'name'])
* ))
* .orderBy('name')
* ```
*/
intersect<E extends SetOperandExpression<DB, O>>(expression: E): SelectQueryBuilder<DB, TB, O>;
/**
* Combines another select query or raw expression to this query using `intersect all`.
*
* The output row type of the combined query must match `this` query.
*
* ### Examples
*
* ```ts
* db.selectFrom('person')
* .select(['id', 'first_name as name'])
* .intersectAll(db.selectFrom('pet').select(['id', 'name']))
* .orderBy('name')
* ```
*
* You can provide a callback to get an expression builder.
* In the following example, this allows us to wrap the query in parentheses:
*
* ```ts
* db.selectFrom('person')
* .select(['id', 'first_name as name'])
* .intersectAll((eb) => eb.parens(
* eb.selectFrom('pet').select(['id', 'name'])
* ))
* .orderBy('name')
* ```
*/
intersectAll<E extends SetOperandExpression<DB, O>>(expression: E): SelectQueryBuilder<DB, TB, O>;
/**
* Combines another select query or raw expression to this query using `except`.
*
* The output row type of the combined query must match `this` query.
*
* ### Examples
*
* ```ts
* db.selectFrom('person')
* .select(['id', 'first_name as name'])
* .except(db.selectFrom('pet').select(['id', 'name']))
* .orderBy('name')
* ```
*
* You can provide a callback to get an expression builder.
* In the following example, this allows us to wrap the query in parentheses:
*
* ```ts
* db.selectFrom('person')
* .select(['id', 'first_name as name'])
* .except((eb) => eb.parens(
* eb.selectFrom('pet').select(['id', 'name'])
* ))
* .orderBy('name')
* ```
*/
except<E extends SetOperandExpression<DB, O>>(expression: E): SelectQueryBuilder<DB, TB, O>;
/**
* Combines another select query or raw expression to this query using `except all`.
*
* The output row type of the combined query must match `this` query.
*
* ### Examples
*
* ```ts
* db.selectFrom('person')
* .select(['id', 'first_name as name'])
* .exceptAll(db.selectFrom('pet').select(['id', 'name']))
* .orderBy('name')
* ```
*
* You can provide a callback to get an expression builder.
* In the following example, this allows us to wrap the query in parentheses:
*
* ```ts
* db.selectFrom('person')
* .select(['id', 'first_name as name'])
* .exceptAll((eb) => eb.parens(
* eb.selectFrom('pet').select(['id', 'name'])
* ))
* .orderBy('name')
* ```
*/
exceptAll<E extends SetOperandExpression<DB, O>>(expression: E): SelectQueryBuilder<DB, TB, O>;
/**
* Gives an alias for the query. This method is only useful for sub queries.
*
* ### Examples
*
* ```ts
* const pets = await db.selectFrom('pet')
* .selectAll('pet')
* .select(
* (qb) => qb.selectFrom('person')
* .select('first_name')
* .whereRef('pet.owner_id', '=', 'person.id')
* .as('owner_first_name')
* )
* .execute()
*
* pets[0].owner_first_name
* ```
*/
as<A extends string>(alias: A): AliasedSelectQueryBuilder<O, A>;
/**
* Clears all select clauses from the query.
*
* ### Examples
*
* ```ts
* db.selectFrom('person')
* .select(['id', 'first_name'])
* .clearSelect()
* .select(['id', 'gender'])
* ```
*
* The generated SQL(PostgreSQL):
*
* ```sql
* select "id", "gender" from "person"
* ```
*/
clearSelect(): SelectQueryBuilder<DB, TB, {}>;
/**
* Clears all where expressions from the query.
*
* ### Examples
*
* ```ts
* db.selectFrom('person')
* .selectAll()
* .where('id','=',42)
* .clearWhere()
* ```
*
* The generated SQL(PostgreSQL):
*
* ```sql
* select * from "person"
* ```
*/
clearWhere(): SelectQueryBuilder<DB, TB, O>;
/**
* Clears limit clause from the query.
*
* ### Examples
*
* ```ts
* db.selectFrom('person')
* .selectAll()
* .limit(10)
* .clearLimit()
* ```
*
* The generated SQL(PostgreSQL):
*
* ```sql
* select * from "person"
* ```
*/
clearLimit(): SelectQueryBuilder<DB, TB, O>;
/**
* Clears offset clause from the query.
*
* ### Examples
*
* ```ts
* db.selectFrom('person')
* .selectAll()
* .limit(10)
* .offset(20)
* .clearOffset()
* ```
*
* The generated SQL(PostgreSQL):
*
* ```sql
* select * from "person" limit 10
* ```
*/
clearOffset(): SelectQueryBuilder<DB, TB, O>;
/**
* Clears all `order by` clauses from the query.
*
* ### Examples
*
* ```ts
* db.selectFrom('person')
* .selectAll()
* .orderBy('id')
* .clearOrderBy()
* ```
*
* The generated SQL(PostgreSQL):
*
* ```sql
* select * from "person"
* ```
*/
clearOrderBy(): SelectQueryBuilder<DB, TB, O>;
/**
* Simply calls the provided function passing `this` as the only argument. `$call` returns
* what the provided function returns.
*
* If you want to conditionally call a method on `this`, see
* the {@link $if} method.
*
* ### Examples
*
* The next example uses a helper function `log` to log a query:
*
* ```ts
* function log<T extends Compilable>(qb: T): T {
* console.log(qb.compile())
* return qb
* }
*
* db.selectFrom('person')
* .selectAll()
* .$call(log)
* .execute()
* ```
*/
$call<T>(func: (qb: this) => T): T;
/**
* Call `func(this)` if `condition` is true.
*
* NOTE: This method has an impact on typescript performance and it should only be used
* when necessary. Remember that you can call most methods like `where` conditionally
* like this:
*
* ```ts
* let query = db.selectFrom('person').selectAll()
*
* if (firstName) {
* query = query.where('first_name', '=', firstName)
* }
*
* if (lastName) {
* query = query.where('last_name', '=', lastName)
* }
*
* const result = await query.execute()
* ```
*
* This method is mainly useful with optional selects. Any `select` or `selectAll`
* method called inside the callback add optional fields to the result type. This is
* because we can't know if those selections were actually made before running the code.
*
* Also see [this recipe](https://github.com/koskimas/kysely/tree/master/site/docs/recipes/conditional-selects.md)
*
* ### Examples
*
* ```ts
* async function getPerson(id: number, withLastName: boolean) {
* return await db
* .selectFrom('person')
* .sele