SQL UPDATE Get Certified
Learn how to modify existing records in database tables using the SQL UPDATE statement.
What is the UPDATE Statement?
The UPDATE statement is used to change existing data in one or more rows of a database table. You can update a single column, multiple columns, or several records at the same time.
The WHERE clause is especially important because it determines
which rows will be modified. Without a WHERE clause, all rows
in the table can be updated.
Basic UPDATE Syntax
The basic syntax uses SET to specify the new value and WHERE to identify the record that should be changed.
What this code does: Creates a planets table with three rows, then runs an UPDATE that changes Mars’s distance from 228 to 227. SET gives the new value, and WHERE name = 'Mars' makes sure only the Mars row is touched. The final SELECT shows Mercury and Venus unchanged and Mars with its corrected distance.
CREATE TABLE planets (
id INTEGER,
name TEXT,
distance INTEGER
);
INSERT INTO planets (id, name, distance)
VALUES
(1, 'Mercury', 58),
(2, 'Venus', 108),
(3, 'Mars', 228);
UPDATE planets
SET distance = 227
WHERE name = 'Mars';
SELECT * FROM planets;
Updating a Single Column
You can change the value of one column for a specific record.
What this code does: Lowers the price of one camera. WHERE id = 3 targets the Focus X, and SET price = 42500 replaces its old price of 38000. Alpha One (45000) and Vision Pro (52000) keep their original prices, and the other columns of the Focus X row are not changed.
CREATE TABLE cameras (
id INTEGER,
model TEXT,
price REAL
);
INSERT INTO cameras (id, model, price)
VALUES
(1, 'Alpha One', 45000),
(2, 'Vision Pro', 52000),
(3, 'Focus X', 38000);
UPDATE cameras
SET price = 42500
WHERE id = 3;
SELECT * FROM cameras;
Updating Multiple Columns
Multiple columns can be changed in the same UPDATE statement by separating assignments with commas.
What this code does: Changes two columns of the Data Science course in a single statement. The comma-separated assignments set duration to 12 and level to ‘Advanced’ (it was 10 and ‘Intermediate’). Because of WHERE id = 2, the Web Design and Robotics courses stay exactly as they were.
CREATE TABLE courses (
id INTEGER,
title TEXT,
duration INTEGER,
level TEXT
);
INSERT INTO courses (id, title, duration, level)
VALUES
(1, 'Web Design', 6, 'Beginner'),
(2, 'Data Science', 10, 'Intermediate'),
(3, 'Robotics', 8, 'Beginner');
UPDATE courses
SET duration = 12,
level = 'Advanced'
WHERE id = 2;
SELECT * FROM courses;
UPDATE with WHERE
The WHERE clause limits the update to rows that match a specified condition.
What this code does: Marks one book as unavailable. The available column uses 1 for ‘in stock’ and 0 for ‘not available’, and the WHERE id = 2 condition means only Hidden Valley is changed from 1 to 0. Ocean Secrets stays available, and Winter Road was already 0.
CREATE TABLE books (
id INTEGER,
title TEXT,
available INTEGER
);
INSERT INTO books (id, title, available)
VALUES
(1, 'Ocean Secrets', 1),
(2, 'Hidden Valley', 1),
(3, 'Winter Road', 0);
UPDATE books
SET available = 0
WHERE id = 2;
SELECT * FROM books;
UPDATE Using an Arithmetic Expression
You can use an existing column value in an expression to calculate the new value.
What this code does: Raises the price of the Tech Summit ticket by 100. SET price = price + 100 reads the current price (1200), adds 100 and stores the result, so the ticket now costs 1300. The Music Night and Science Expo tickets are not matched by the WHERE clause and stay at 800 and 500.
CREATE TABLE tickets (
id INTEGER,
event_name TEXT,
price REAL
);
INSERT INTO tickets (id, event_name, price)
VALUES
(1, 'Music Night', 800),
(2, 'Science Expo', 500),
(3, 'Tech Summit', 1200);
UPDATE tickets
SET price = price + 100
WHERE event_name = 'Tech Summit';
SELECT * FROM tickets;
UPDATE with a Comparison
Comparison operators such as >, <, and = can be used in the WHERE clause.
What this code does: Gives an ‘A’ grade to every student who scored 90 or more. Noah (95) and James (91) meet the condition, so their grades change from ‘B’ to ‘A’. Emma (88) and Olivia (72) are below 90, so their grades are not touched.
CREATE TABLE students (
id INTEGER,
name TEXT,
score INTEGER,
grade TEXT
);
INSERT INTO students (id, name, score, grade)
VALUES
(1, 'Emma', 88, 'B'),
(2, 'Noah', 95, 'B'),
(3, 'Olivia', 72, 'C'),
(4, 'James', 91, 'B');
UPDATE students
SET grade = 'A'
WHERE score >= 90;
SELECT * FROM students;
UPDATE with OR
The OR operator allows an update when at least one of several conditions is true.
What this code does: Adds 0.2 to the rating of every Comedy or Drama movie. Blue Horizon (Drama) goes from 7.2 to 7.4 and Funny Days (Comedy) from 6.8 to 7.0. The two Science Fiction movies match neither condition, so Star Mission and Deep Space keep their ratings of 8.1 and 7.9.
CREATE TABLE movies (
id INTEGER,
title TEXT,
genre TEXT,
rating REAL
);
INSERT INTO movies (id, title, genre, rating)
VALUES
(1, 'Blue Horizon', 'Drama', 7.2),
(2, 'Star Mission', 'Science Fiction', 8.1),
(3, 'Funny Days', 'Comedy', 6.8),
(4, 'Deep Space', 'Science Fiction', 7.9);
UPDATE movies
SET rating = rating + 0.2
WHERE genre = 'Comedy'
OR genre = 'Drama';
SELECT * FROM movies;
UPDATE NULL Values
You can use IS NULL to locate missing values and then update them with a new value.
What this code does: Fills in missing tracking codes. The WHERE tracking_code IS NULL condition finds Michael’s delivery, which had no code, and SET replaces the NULL with the text ‘PENDING’. Ava (ZX100) and Sophia (ZX300) already have codes, so their rows are left alone.
CREATE TABLE deliveries (
id INTEGER,
customer TEXT,
tracking_code TEXT
);
INSERT INTO deliveries (id, customer, tracking_code)
VALUES
(1, 'Ava', 'ZX100'),
(2, 'Michael', NULL),
(3, 'Sophia', 'ZX300');
UPDATE deliveries
SET tracking_code = 'PENDING'
WHERE tracking_code IS NULL;
SELECT * FROM deliveries;
UPDATE Text Values
Text columns can be changed by assigning a new string value in the SET clause.
What this code does: Corrects a data-entry mistake. Lisbon was recorded with the wrong country (‘Spain’), and the UPDATE assigns the text ‘Portugal’ to the row where the name is ‘Lisbon’. Text values must be written in single quotes, and the Bengaluru and Pune rows are not affected.
CREATE TABLE cities (
id INTEGER,
name TEXT,
country TEXT
);
INSERT INTO cities (id, name, country)
VALUES
(1, 'Bengaluru', 'India'),
(2, 'Pune', 'India'),
(3, 'Lisbon', 'Spain');
UPDATE cities
SET country = 'Portugal'
WHERE name = 'Lisbon';
SELECT * FROM cities;
UPDATE with IN
The IN operator makes it easy to update rows whose value matches one of several specified values.
What this code does: Marks the languages Python and SQL as ‘Popular’ by checking each name against the list in IN ('Python', 'SQL'). This is shorter than writing name = 'Python' OR name = 'SQL'. Java and C++ are not in the list, so their status remains ‘Basic’.
CREATE TABLE languages (
id INTEGER,
name TEXT,
status TEXT
);
INSERT INTO languages (id, name, status)
VALUES
(1, 'Python', 'Basic'),
(2, 'Java', 'Basic'),
(3, 'SQL', 'Basic'),
(4, 'C++', 'Basic');
UPDATE languages
SET status = 'Popular'
WHERE name IN ('Python', 'SQL');
SELECT * FROM languages;
UPDATE Without WHERE
If you omit the WHERE clause, SQLite updates every row in the table. This can be useful when intentionally changing a column for all records, but it should be used carefully.
What this code does: Runs UPDATE weather SET unit = 'F' with no WHERE clause, so all three rows (Delhi, Mumbai and Chennai) have their unit changed from ‘C’ to ‘F’. Notice that only the label changes; this table stores no temperatures to convert. Forgetting WHERE on a real table is a classic mistake, so always double-check before running an update like this.
CREATE TABLE weather (
city TEXT,
unit TEXT
);
INSERT INTO weather (city, unit)
VALUES
('Delhi', 'C'),
('Mumbai', 'C'),
('Chennai', 'C');
UPDATE weather
SET unit = 'F';
SELECT * FROM weather;
Common UPDATE Clauses
UPDATE productsSET price = 500WHERE id = 3CASE WHEN ... THEN ...WHERE phone IS NULLAdvantages of UPDATE
- Modifies existing records without creating new rows.
- Can update one or multiple columns.
- Can update one row or many matching rows.
- Supports expressions and calculations.
- Can use conditions such as
AND,OR,IN, andLIKE. - Can handle
NULLvalues and conditional updates.
Best Practices
- Always check your
WHEREcondition before running anUPDATE. - Use a
SELECTquery first to verify which rows will be affected. - Do not omit
WHEREunless you intentionally want to update every row. - Use precise conditions when modifying important data.
- Use transactions when performing large or critical updates.
- Back up important data before making large-scale changes.
The UPDATE statement allows you to modify existing data efficiently. Click Test My Code on any example above to run it live, or Code Explanation to walk through the query block by block.
🧪 Test Your SQL Code
Edit the SQL code on the left and click “Run Code” to see the result on the right.
Click “Run Code” to see the result here.