The Wayback Machine - https://web.archive.org/web/20250821074421/https://www.geeksforgeeks.org/sql/sql-update-statement/
Open In App

SQL UPDATE Statement

Last Updated : 11 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The UPDATE statement in SQL is used to modify existing records in a table without deleting them. It allows updating one or multiple columns, with or without conditions, to keep data accurate and consistent.

  • Change specific column values in selected rows
  • Apply targeted updates using WHERE
  • Update single or multiple columns at once
  • More efficient than deleting and re-inserting rows
  • Maintains data integrity by modifying in place

Syntax:

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

Parameters

  • table_name: Name of the table you want to update.
  • SET: The column(s) you want to update and their new values.
  • WHERE: Filters the specific rows you want to update.

Note: The SET keyword assigns new values to columns, while the WHERE clause selects which rows to update. Without WHERE, all rows will be updated.

Examples of SQL UPDATE Statement

Let’s begin by creating a Customer table with some sample data. This table contains each customer's unique ID, name, last name, phone number and country. We will use it to demonstrate how the UPDATE statement works in SQL.

Query:

CREATE TABLE Customer(
    CustomerID INT PRIMARY KEY,
    CustomerName VARCHAR(50),
    LastName VARCHAR(50),
    Country VARCHAR(50),
    Age int(2),
  Phone int(10)
);

-- Insert some sample data into the Customers table
INSERT INTO Customer (CustomerID, CustomerName, LastName, Country, Age, Phone)
VALUES (1, 'Shubham', 'Thakur', 'India','23','xxxxxxxxxx'),
       (2, 'Aman ', 'Chopra', 'Australia','21','xxxxxxxxxx'),
       (3, 'Naveen', 'Tulasi', 'Sri lanka','24','xxxxxxxxxx'),
       (4, 'Aditya', 'Arpan', 'Austria','21','xxxxxxxxxx'),
       (5, 'Nishant. Salchichas S.A.', 'Jain', 'Spain','22','xxxxxxxxxx'); 

Output

Customer_table

Example 1: Update Single Column Using UPDATE Statement

We have a Customer table and we want to Update the CustomerName where the Age is 22.

Query:

UPDATE Customer 
SET CustomerName  = 'Nitin' 
WHERE Age = 22;

Output:

Example1

Explanation: Only the rows where Age is 22 will be updated, and the CustomerName will be set to 'Nitin'.

Example 2: Updating Multiple Columns using UPDATE Statement

We need to update both the CustomerName and Country for a specific CustomerID.

Query:

UPDATE Customer 
SET CustomerName = 'Satyam', 
Country = 'USA' 
WHERE CustomerID = 1;

Output:

Example2

Explanation: For the row where CustomerID is 1, both CustomerName and Country will be updated simultaneously.

Note: For updating multiple columns we have used comma(,) to separate the names and values of two columns.

Example 3: Omitting WHERE Clause in UPDATE Statement

If we accidentally omit the WHERE clause, all the rows in the table will be updated, which is a common mistake. Let’s update the CustomerName for every record in the table:

Query:

UPDATE Customer 
SET CustomerName = 'Shubham';

Output

Example3

Explanation: This will set the CustomerName for every row in the Customer table to 'Shubham'. Be careful while omitting the WHERE clause, as this action is irreversible unless you have a backup.

Optimizing SQL UPDATE Queries

  • Avoid frequent updates: Constantly updating rows can slow down performance. Batch updates or consider using a database trigger to handle automatic updates.
  • Index relevant columns: Ensure that columns in the WHERE clause (such as CustomerID) are indexed. This will improve the speed of the update operation.

Best Practices for Using SQL UPDATE Statement

1. Always use the WHERE clause:

The most important point when using the UPDATE statement is to always include a WHERE clause unless you genuinely intend to update all rows.

2. Check your data before updating:

Run a SELECT query to view the data you want to update before executing the UPDATE statement. This helps avoid accidental data modifications.

SELECT * FROM Customer WHERE Age = 22;

3. Use transactions for critical updates:

When performing updates in a production environment, consider using transactions. Transactions allow you to commit or roll back changes as needed.

BEGIN TRANSACTION;
UPDATE Customer SET CustomerName = 'John' WHERE CustomerID = 3;
COMMIT;  -- Use ROLLBACK to undo if necessary

4. Test on a small dataset first: If you are uncertain about the impact of your UPDATE, test it on a small subset of data to ensure the changes are as expected.


UPDATE Statement in SQL
Article Tags :

Similar Reads