SQL UPDATE Statement

What is SQL UPDATE?

The UPDATE statement in SQL is used to modify existing records in a database table. It allows you to update one or more records in a table based on a specified condition using the WHERE clause.

Syntax of SQL UPDATE

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

Explanation:

  • table_name: The name of the table where you want to update data.
  • column1, column2,...: The columns you want to update with new values.
  • condition: The condition (usually with the WHERE clause) to specify which record(s) should be updated. Without the WHERE clause, all records in the table will be updated!

SQL UPDATE Example

Let’s assume we have a table named Employees. Here’s an example of how to update a record:

UPDATE Employees
SET Department = 'Marketing'
WHERE ID = 1;

This query updates the department of the employee with ID 1 to 'Finance'.

Updating Multiple Columns

To update multiple columns in a table, you can simply separate each column and its new value with a comma:

UPDATE Employees
SET Name = 'Khan Shaista', Department = 'IT'
WHERE ID = 1;

This updates both the name and department of the employee with ID 1.

Updating Multiple Rows

You can update multiple records at once by using a condition that matches more than one row:

UPDATE Employees
SET Department = 'HR'
WHERE Department = 'Admin';

This query updates the department of all employees in the 'Admin' department to 'HR'.

SQL UPDATE in Popular Databases

SQL UPDATE is used across various relational database systems, such as:

  • MySQL
  • PostgreSQL
  • Microsoft SQL Server
  • Oracle Database
  • SQLite