Open In App

How to Update Multiple Columns in Single Update Statement in SQL?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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 the 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 (NAME, AGE, CITY) VALUES
('Aman Chopra', 25, 'New York'),
('Shubham Thakur', 30, 'Los Angeles'),
('Naveen Tulasi', 45, 'Chicago'),
('Aditya Arpan', 28, 'Miami'),
('Nishant Jain', 50, 'Houston');

Output:

IMG3

 

Step 5: View the data from the table

Query:

SELECT * FROM demo_table;

Output:

IMG3

Step 6:Update multiple columns

For the purpose of demonstration let’s update the AGE value to 30 and the CITY value to PUNJAB where the CITY value is ‘Chicago’.

Query:

UPDATE demo_table
SET AGE=30 ,CITY='PUNJAB'
WHERE CITY='NEW DELHI';

Output:

View the content of table demo_table

 

We can see that the table is updated now with the desired value.

Using  GROUP BY Multiple Columns in SQL

In the SQL GROUP BY clause we use the SELECT statement to arrange similar data into groups.

Syntax:

SELECT column1, column2, …
FROM table_name
WHERE condition
GROUP BY column1, column2, …
ORDER BY column1, column2, …

Let’s have a look into the GROUP BY clause by seeing some queries.

Query:

CREATE TABLE demo_table(
NAME VARCHAR (20),
AGE INT ,
CITY VARCHAR(20)
);
INSERT INTO demo_table (NAME, AGE, CITY) VALUES
('Aman Chopra', 25, 'New York'),
('Shubham Thakur', 30, 'Los Angeles'),
('Naveen Tulasi', 45, 'Chicago'),
('Aditya Arpan', 28, 'Miami'),
('Nishant Jain', 50, 'Houston');

Output:

 

Query:

SELECT AGE, CITY
FROM demo_table 
WHERE AGE<30
GROUP BY AGE
ORDER BY AGE;

Output:

IMG3

 

Conclusion

  1. The UPDATE statement can be used to update a table’s columns.
  2. The UPDATE statement uses the SET command to specify the columns to update.
  3. Following the SET command, the conditions are specified using the WHERE command.
  4. Updates are made to the cells that meet the criteria.
  5. To divide up data into groups, use the GROUP BY clause.

Last Updated : 06 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads