Skip to main content
The UpdateQueryBuilder is used to build and execute UPDATE queries in Kysely. It provides a fluent API for updating rows with type safety.

Basic Usage

Update a Single Row

Update a row in a table:
The generated SQL (PostgreSQL):

Update with Complex Values

You can provide a callback to the set method to get access to an expression builder:
The generated SQL (PostgreSQL):

Update Column by Column

You can also provide two arguments where the first is the column and the second is the value:

Returning Data

On PostgreSQL you can chain returning to get the updated rows’ columns:
The generated SQL (PostgreSQL):
Return all columns:

Advanced Expressions

Values can be arbitrary expressions including raw SQL snippets and subqueries:
The generated SQL (PostgreSQL):

Update with Joins

PostgreSQL: Update from Join

On PostgreSQL, you can use the from method:
The generated SQL (PostgreSQL):

MySQL: Direct Table Joins

MySQL allows you to join tables directly and update rows of all joined tables:
The generated SQL (MySQL):

Using Join Methods (PostgreSQL)

Limit and Order By

Limit (MySQL)

On MySQL and some other databases, you can limit the number of updated rows:
The generated SQL (MySQL):

Order By (MySQL)

Top Clause (MS SQL Server)

Update the first N rows:
The generated SQL (MS SQL Server):
Update a percentage:
The generated SQL (MS SQL Server):

Where Conditions

All the standard where methods are available:
See the UpdateQueryBuilder documentation for more filtering options.

API Reference

Main Methods

  • updateTable(table) - Specify the table to update
  • set(updates) - Set the values to update
  • set(column, value) - Set a single column value
  • where(...) - Add WHERE conditions
  • whereRef(...) - Add WHERE conditions comparing columns
  • from(table) - Add FROM clause (PostgreSQL)
  • innerJoin(), leftJoin(), rightJoin(), fullJoin() - Join tables
  • orderBy(...) - Order rows (MySQL)
  • limit(n) - Limit updated rows (MySQL)
  • top(n) - Update top N rows (MS SQL Server)
  • returning(...) - Return columns from updated rows
  • returningAll() - Return all columns
  • clearWhere() - Clear WHERE conditions
  • clearOrderBy() - Clear ORDER BY clause
  • modifyEnd(modifier) - Add custom SQL
  • execute() - Execute the query
  • executeTakeFirst() - Execute and return first result
  • executeTakeFirstOrThrow() - Execute and return first result or throw

Result Object

The return value is an instance of UpdateResult: