Skip to main content

SelectQueryBuilder

Builder for constructing SELECT queries with type-safe column selection, joins, filtering, and more.

Type Parameters

  • DB - The database schema type
  • TB - Union of table names available in the FROM clause
  • O - The output row type (columns selected)

Methods

select

Adds a select statement to the query. When a column is selected, Kysely adds its type to the return type. Select calls are additive. Calling select('id').select('first_name') is the same as select(['id', 'first_name']). Examples: Select a single column:
Select multiple columns:
Select with aliases:
Select complex expressions:

selectAll

Adds a select * or select table.* clause to the query. Examples: Select all columns:
Select all columns from a specific table:
Select all columns from multiple tables:

where

Adds a WHERE clause to the query. Multiple where calls are combined with AND. See WhereInterface documentation for more examples.

whereRef

Adds a WHERE clause that compares two column references.

innerJoin

Joins another table using an inner join. Examples: Simple join:
Join with callback:
Subquery join:

leftJoin

Just like innerJoin but adds a left join instead of an inner join.

rightJoin

Just like innerJoin but adds a right join instead of an inner join.

fullJoin

Just like innerJoin but adds a full join instead of an inner join. Only supported by some dialects like PostgreSQL, MS SQL Server and SQLite.

orderBy

Adds an ORDER BY clause to the query. See OrderByInterface documentation for more examples.

groupBy

Adds a GROUP BY clause to the query. Example:

having

Adds a HAVING clause to the query. See HavingInterface documentation for more examples.

limit

Adds a LIMIT clause to the query. Example:

offset

Adds an OFFSET clause to the query. Example:

distinct

Makes the selection distinct. Example:

distinctOn

Adds DISTINCT ON expressions to the select clause. Example:

union

Combines another select query to this query using UNION. Example:

unionAll

Combines another select query using UNION ALL.

intersect

Combines another select query using INTERSECT.

intersectAll

Combines another select query using INTERSECT ALL.

except

Combines another select query using EXCEPT.

exceptAll

Combines another select query using EXCEPT ALL.

as

Gives an alias for the query. Only useful for sub queries. Example:

$call

Simply calls the provided function passing this as the only argument. Example:

$if

Call func(this) if condition is true. This method is especially handy with optional selects. Selections made inside the callback add optional fields to the result type. Example:

$castTo

Change the output type of the query. This method doesn’t change the SQL. It simply returns a copy with a new output type.

$narrowType

Narrows (parts of) the output type of the query.

execute

Executes the query and returns an array of rows.

executeTakeFirst

Executes the query and returns the first result or undefined if the query returned no result.

executeTakeFirstOrThrow

Executes the query and returns the first result or throws if the query returned no result. By default an instance of NoResultError is thrown.

stream

Streams the query results.

compile

Compiles the query to SQL without executing it.

explain

Executes an EXPLAIN query for this query.