SQL UPDATE Statement

The UPDATE statement in SQL is used to modify existing records in a table. This is useful when you need to correct data or make changes based on new information. The SET clause specifies the column(s) to be updated and the new values to be assigned. The WHERE clause is used to target specific rows; if omitted, all rows will be updated.

Proper use of the WHERE clause is essential to avoid unintentional changes to all rows in the table.

Example

In this example, the price of a product in the Orders table is updated.

Code Example


-- Update the price of a product in the Orders table
UPDATE Orders
SET Price = 1300.00
WHERE Product = 'Laptop';

-- View the updated table
SELECT * FROM Orders;
            

Output

Product Category Price
Laptop Electronics 1300.00
Phone Electronics 800.00
Tablet Gadgets 600.00
Smartwatch Gadgets 250.00
Headphones Gadgets 150.00
Camera Electronics 500.00

Explanation

- The UPDATE Orders statement specifies the table to modify.
- The SET clause assigns a new price (1300.00) to the product "Laptop".
- The WHERE clause ensures that only the row with "Laptop" is updated.
- The updated table shows the new price for "Laptop" while other rows remain unchanged.