SQLite Update

What is update?

Update is a SQLite command that allows you to change data in your database. You can use the UPDATE command to change data in one or more columns of a single row, multiple rows, or all rows in a table.

Update syntax

UPDATE table_name 
SET column1 = value1, column2 = value2, ... 
WHERE condition; 

Update example

UPDATE customers SET name='Liam' WHERE id=1;

Update multiple columns

UPDATE customers 
SET name='Liam', city='New York' 
WHERE id=1;

Update from select

In the example below, the has_order column is updated if the select returns the value 1.

UPDATE customers 
SET has_orders = (SELECT 1 FROM orders WHERE customers.id = orders.customer_id) 
WHERE id is not null; 

Update from subquery

In the example below, the update is done if the condition after the WHERE clause is met based on a subquery.

UPDATE customers 
SET has_orders = 1
WHERE id in (SELECT distinct customer_id FROM orders);