Skip to main content
The SelectQueryBuilder is used to build and execute SELECT queries in Kysely. It provides a fluent API for selecting columns, filtering data, joining tables, and more.

Basic Usage

Selecting Columns

Select specific columns from a table:
The generated SQL (PostgreSQL):

Multiple Columns

Select multiple columns:
The generated SQL (PostgreSQL):

Column Aliases

You can give aliases to selections by appending as the_alias to the name:
The generated SQL (PostgreSQL):

Complex Selections

You can select arbitrary expressions including subqueries and raw SQL snippets:

Select All Columns

The selectAll method generates SELECT *:
The generated SQL (PostgreSQL):
Select all columns of a specific table:
The generated SQL (PostgreSQL):

Filtering Results

Where Clause

Filter results using the where method:
See the SelectQueryBuilder documentation for more filtering options.

Ordering Results

Order By

Order results using the orderBy method:

Limiting Results

Limit and Offset

Limit the number of results:

Grouping Results

Group By

Group results:

Having Clause

Filter grouped results:

Distinct Selection

Make the selection distinct:
The generated SQL (PostgreSQL):

Distinct On (PostgreSQL)

The generated SQL (PostgreSQL):

Advanced Features

For Update/Share

Add locking modifiers to select queries on supported databases:

Skip Locked / No Wait

Type Narrowing

Not Null Types

Kysely has helpers for dealing with nullable types:

Dynamic Queries

Use the dynamic module for runtime column selection:

API Reference

Main Methods

  • select() - Add columns or expressions to select
  • selectAll() - Select all columns
  • selectFrom() - Specify the table(s) to select from
  • where() - Add WHERE conditions
  • whereRef() - Add WHERE conditions comparing two columns
  • innerJoin(), leftJoin(), rightJoin(), fullJoin() - Join tables
  • orderBy() - Order results
  • groupBy() - Group results
  • having() - Filter grouped results
  • limit() - Limit number of results
  • offset() - Skip a number of results
  • distinct() - Make selection distinct
  • distinctOn() - PostgreSQL distinct on specific columns
  • forUpdate(), forShare() - Add locking modifiers
  • skipLocked(), noWait() - Add row lock modifiers
  • modifyFront(), modifyEnd() - Add custom SQL to query
  • execute() - Execute the query and return all results
  • executeTakeFirst() - Execute and return first result or undefined
  • executeTakeFirstOrThrow() - Execute and return first result or throw