SQL UPDATE Statement

SQL UPDATE Statement: A Complete Guide

SQL UPDATE Statement

SQL provides powerful commands to manipulate data within a database. Two important SQL commands, UPDATE and DELETE, allow users to modify existing records. The SQL UPDATE statement specifically helps in changing data within a table, targeting specific rows using the WHERE clause.

In this guide, we will explore how to use the SQL UPDATE statement effectively, along with syntax and examples. Let’s dive in!

Complete Python Course with Advance topics:-Click Here
SQL Tutorial :-Click Here

Understanding the SQL UPDATE Statement

The SQL UPDATE statement modifies existing records in a database table. You must use the WHERE clause to specify which rows need updating; otherwise, all rows will be modified, which may lead to unintended changes.

SQL UPDATE Syntax

UPDATE table_name  
SET column_name = new_value  
WHERE condition;

This syntax ensures that only specific rows meeting the given condition will be updated.

Example: Updating a Single Column

Assume that the following information is in a table called employees:

Employee_ID First_Name Last_Name Email
101 Rohan Khanna rohan.k@xyz.com
102 Simran Mehta simran.m@xyz.com
103 Aryan Sharma aryan.s@xyz.com

Now, we update Email for the employee with Employee_ID = 103.

UPDATE employees  
SET Email = 'aryan.sh@xyz.com'  
WHERE Employee_ID = 103;

After executing this query, the updated table will be:

Employee_ID First_Name Last_Name Email
101 Rohan Khanna rohan.k@xyz.com
102 Simran Mehta simran.m@xyz.com
103 Aryan Sharma aryan.sh@xyz.com

Updating Multiple Columns

If you want to modify multiple fields simultaneously, separate each assignment with a comma.

UPDATE employees  
SET Email = 'john.d@xyz.com', First_Name = 'John'  
WHERE Employee_ID = 103;

Updated Table:

Employee_ID First_Name Last_Name Email
101 Rohan Khanna rohan.k@xyz.com
102 Simran Mehta simran.m@xyz.com
103 John Sharma john.d@xyz.com

MySQL Syntax for Updating a Table

The MySQL UPDATE statement follows a similar syntax:

UPDATE table_name  
SET column1 = new_value1, column2 = new_value2  
[WHERE condition];

SQL UPDATE with SELECT Query

We can use the SELECT statement inside an UPDATE query to dynamically update records based on another table.

SQL UPDATE with SELECT Syntax:

UPDATE target_table  
SET target_table.column_name = source_table.column_value  
WHERE EXISTS (  
    SELECT source_table.column_value  
    FROM source_table  
    WHERE source_table.join_column = target_table.join_column  
);

Alternatively, we can use INNER JOIN for updating records:

UPDATE target_table  
SET target_table.column1 = source_table.column1,  
    target_table.column2 = source_table.column2  
FROM target_table  
INNER JOIN source_table  
ON target_table.id = source_table.id;

Updating a Single Column in SQL

To update a specific column in a table:

UPDATE employees  
SET Employee_ID = 201  
WHERE First_Name = 'Amit';

This SQL UPDATE statement changes Employee_ID to 201 where the First_Name is Amit.

Updating Multiple Columns in SQL

If multiple column updates are required, use the following syntax:

UPDATE employees  
SET First_Name = 'Raj', Department = 'Finance'  
WHERE First_Name = 'Arun';

This will update First_Name to ‘Raj’ and Department to ‘Finance’ where First_Name was ‘Arun’.

Download New Real Time Projects :-Click here
Complete Advance AI topics:- CLICK HERE

Conclusion

The SQL UPDATE statement is a crucial command in database management, allowing for efficient data modification. By using the WHERE clause, SELECT statements, and JOINs, you can dynamically update data as needed while ensuring accuracy and consistency.

Mastering the UPDATE statement enhances your ability to manage databases effectively, making it an essential skill for database administrators and developers alike.

Stay tuned for more insightful SQL tutorials on UpdateGadh!


sql update statement with join
update query in sql server
sql update from select
update column value in sql
sql update from another table
delete command in sql
sql update multiple columns
update query in mysql
sql insert statement
sql delete statement

Post Comment