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:Multiple Columns
Select multiple columns:Column Aliases
You can give aliases to selections by appendingas the_alias to the name:
Complex Selections
You can select arbitrary expressions including subqueries and raw SQL snippets:Select All Columns
TheselectAll method generates SELECT *:
Filtering Results
Where Clause
Filter results using thewhere method:
Ordering Results
Order By
Order results using theorderBy 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:Distinct On (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 thedynamic module for runtime column selection:
API Reference
Main Methods
select()- Add columns or expressions to selectselectAll()- Select all columnsselectFrom()- Specify the table(s) to select fromwhere()- Add WHERE conditionswhereRef()- Add WHERE conditions comparing two columnsinnerJoin(),leftJoin(),rightJoin(),fullJoin()- Join tablesorderBy()- Order resultsgroupBy()- Group resultshaving()- Filter grouped resultslimit()- Limit number of resultsoffset()- Skip a number of resultsdistinct()- Make selection distinctdistinctOn()- PostgreSQL distinct on specific columnsforUpdate(),forShare()- Add locking modifiersskipLocked(),noWait()- Add row lock modifiersmodifyFront(),modifyEnd()- Add custom SQL to queryexecute()- Execute the query and return all resultsexecuteTakeFirst()- Execute and return first result or undefinedexecuteTakeFirstOrThrow()- Execute and return first result or throw