Skip to main content

SQL UPDATE Statement

SQL UPDATE Statement

The SQL UPDATE statement is used to modify existing records or rows in a table in a database.


1. Basic Syntax

The basic syntax of the UPDATE statement is:

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

Example:

// Example of updating the "age" column for a specific employee in the "employees" table
UPDATE employees
SET age = 32
WHERE first_name = 'John' AND last_name = 'Doe';

This example updates the "age" column for the employee named "John Doe" in the "employees" table.


2. Updating Multiple Rows

You can update multiple rows in a table using a single UPDATE statement:

// Example of updating the "status" column for multiple records in the "orders" table
UPDATE orders
SET status = 'Completed'
WHERE customer_id = 123;

This example updates the "status" column for all orders associated with the customer ID 123 in the "orders" table.


3. Conclusion

The UPDATE statement is a powerful tool in SQL for modifying existing records in a table. It allows developers to update single or multiple rows at once, providing flexibility when making changes to data stored in a database.

Comments