Explain ‘SQL UPDATE Statement’ with an Example

The SQL UPDATE statement is used to modify existing data in a table. It allows you to update specific columns of one or more rows based on specified conditions. Here’s an explanation of the SQL UPDATE statement with an example:

Syntax:

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

Example:
Consider a table called “Employees” with columns like “EmployeeID,” “FirstName,” “LastName,” and “Salary.” Let’s say we want to update the salary of an employee with the EmployeeID of 101 to $6000.

UPDATE Employees
SET Salary = 6000
WHERE EmployeeID = 101;

In this example, the UPDATE statement specifies the “Employees” table as the target. The SET clause is used to specify the column(s) that need to be updated and the corresponding new values. In this case, we are updating the “Salary” column and setting it to $6000. The WHERE clause is used to identify the row(s) that should be updated based on the specified condition. In this case, we are updating the salary for the employee with the EmployeeID of 101.

Upon executing this query, the salary of the specified employee will be updated to $6000 in the “Employees” table.

It’s important to note that the UPDATE statement can update multiple columns simultaneously by including multiple column-value pairs in the SET clause. You can also use various operators and functions within the SET clause to perform calculations or modify values based on existing data.

If you omit the WHERE clause from the UPDATE statement, all rows in the specified table will be updated with the new values provided in the SET clause. Therefore, it’s crucial to use the WHERE clause appropriately to target specific rows that need to be updated.

The SQL UPDATE statement allows you to modify existing data in a table, providing flexibility to change the values in specific columns based on certain conditions. It is a powerful tool for data manipulation and maintaining the accuracy and relevance of the database.

Run Code In Live & Test

Leave a Reply

Your email address will not be published. Required fields are marked *