How to Update Multiple Columns in Single Update Statement in SQL?
In this article, we will see, how to update multiple columns in a single statement in SQL. We can update multiple columns by specifying multiple columns after the SET command in the UPDATE statement. The UPDATE statement is always followed by the SET command, it specifies the column where the update is required.
UPDATE for multiple columns
Syntax:
UPDATE table_name SET column_name1= value1, column_name2= value2 WHERE condition;
Now, for the demonstration follow the below steps:
Step 1: Create a database
we can use the following command to create a database called geeks.
Query:
CREATE DATABASE geeks;
Step 2: Use database
Use the below SQL statement to switch the database context to geeks:
Query:
USE geeks;
Step 3: Table definition
We have the following demo_table in our geek’s database.
Query:
CREATE TABLE demo_table( NAME VARCHAR(20), AGE INT, CITY VARCHAR(20) );
Step 4: Insert data into a table
Query:
INSERT INTO demo_table VALUES ('ROMY KUMARI', 22, 'NEW DELHI'), ('PUSHKAR JHA',23, 'NEW DELHI'), ('AKANKSHA GUPTA',22, 'PUNJAB'), ('SUJATA JHA', 30,'PATNA'), ('PREETI GIRI', 26,'BANGLORE'), ('PREM GUPTA',31,'PUNE'), ('RAM KAPOOR', 34,'PUNE'), ('SHEETAL AWASTHI',32, 'RAJASTHAN');
Step 5: View data of the table
Query:
SELECT * FROM demo_table;
Output:
Step 6: Update multiple columns
For the purpose of demonstration let’s update AGE value to be 30 and CITY value to be PUNJAB where CITY value is ‘NEW DELHI’.
Query:
UPDATE demo_table SET AGE=30, CITY='PUNJAB' WHERE CITY='NEW DELHI';
Output:
view content of table demo_table
We can see that the table is updated now with the desired value.
Please Login to comment...