The Wayback Machine - https://web.archive.org/web/20241104094732/https://www.geeksforgeeks.org/how-to-update-multiple-columns-in-single-update-statement-in-sql/
Open In App

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

Last Updated : 06 May, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.


Similar Reads

How to Update Two Tables in One Statement in SQL Server?
To update two tables in one statement in SQL Server, use the BEGIN TRANSACTION clause and the COMMIT clause. The individual UPDATE clauses are written in between the former ones to execute both updates simultaneously. Here, we will learn how to update two tables in a single statement in SQL Server. SyntaxUpdating two tables in one statement in SQL
3 min read
SQL UPDATE Statement
SQL UPDATE Statement modifies the existing data from the table. UPDATE Statement in SQL The UPDATE statement in SQL is used to update the data of an existing table in the database. We can update single columns as well as multiple columns using the UPDATE statement as per our requirement. In a very simple way, we can say that SQL commands(UPDATE and
3 min read
SQL Query to Update All Columns in a Table
In SQL, sometimes we need to write a single query to update the values of all columns in a table. We will use the UPDATE keyword to achieve this. For this, we use a specific kind of query shown in the below demonstration. For this article, we will be using the Microsoft SQL Server as our database and Select keyword. Step 1: Create a Database. For t
2 min read
SQL Query to Update Columns Values With Column of Another Table
To update column values with the column values of another table, use the nested statement feature of SQL. Using the feature, we can select data from two tables, and update data in one table based on data from another table. Using JOIN also allows to connection of two tables, and updates the column values of one table with the column of another tabl
2 min read
Removing Duplicate Rows (Based on Values from Multiple Columns) From SQL Table
In SQL, some rows contain duplicate entries in multiple columns(>1). For deleting such rows, we need to use the DELETE keyword along with self-joining the table with itself. The same is illustrated below. For this article, we will be using the Microsoft SQL Server as our database. Step 1: Create a Database. For this use the below command to crea
3 min read
How to Alter Multiple Columns at Once in SQL Server?
In SQL, sometimes we need to write a single query to update the values of all columns in a table. We will use the UPDATE keyword to achieve this. For this, we use a specific kind of query shown in the below demonstration. For this article, we will be using the Microsoft SQL Server as our database and Select keyword. Step 1: Create a Database. For t
3 min read
How to Find Duplicates Values Across Multiple Columns in SQL?
In SQL, sometimes we need to find duplicate entries across multiple columns in a table in a single query. We will use the GROUP BY and COUNT keywords to achieve this. For this, we use a specific kind of query shown in the below demonstration. For this article, we will be using the Microsoft SQL Server as our database and Select keyword. Step 1: Cre
2 min read
Selecting Multiple Columns Based On Condition in SQL
In the real-world scenario where we have to select only columns from a given table, the columns selected can be single or multiple as per requirement. For Example: Write a query that gives the names of EMPLOYEE in an organization, so here we have to pick out only the name column from that particular EMPLOYEE table. Similarly, another example of mul
4 min read
How to SELECT DISTINCT on Multiple Columns in SQL?
In the world of databases, data duplication can lead to confusion and inefficiency. SQL provides a powerful tool, SELECT DISTINCT, to retrieve unique values from columns. However, when dealing with multiple columns, the approach becomes more detailed. In this article, We will explore How to SELECT DISTINCT on multiple columns in SQL by understandin
4 min read
How to Find the Maximum of Multiple Columns in SQL
Finding the maximum value of multiple columns is one of the most common analytical tasks essential for making decisions and analyzing data. Using the MAX() function of SQL, users can find the maximum value in a single column. But to find the maximum value in multiple columns, users need to use other SQL statements like CASE or GREATEST. This articl
2 min read
How to Perform SQL Join on Multiple Columns in Same Table?
To perform SQL JOIN on multiple columns in the same table use the Self Join. Using Self Join on a table, a connection between columns of the same table can be created. We can implement Self Join on a table using “inner”, “left”, “right”, “full”, or “cartesian” join. Let us see how to perform a Self Join to connect multiple columns within the same t
3 min read
SQL Query to Convert Rows to Columns in SQL Server
In this article we will see, how to convert Rows to Column in SQL Server. In a table where many columns have the have same data for many entries in the table, it is advisable to convert the rows to column. This will help to reduce the table and make the table more readable. For example, Suppose we have a table given below: NAMECOLLEGEROLL NUMBERSUB
2 min read
How to Update Multiple Records Using One Query in SQL Server?
To update multiple records in the SQL server, use the UPDATE Statement. Using the SQL UPDATE statement with the WHERE clause users can update multiple records in the table based on some condition. There can be two cases in this situation: Table of Content Update Multiple Records Based on One Condition in SQL ServerUpdate Multiple Records Based on M
4 min read
Insert Multiple Values Into Multiple Tables in SQL Server
To insert multiple values into multiple tables in SQL Server, use the OUTPUT clause. The SQL OUTPUT clause allows to insert multiple values into multiple tables in a single query. Output clause clause in SQL is used to capture data affected by Data Manipulation Language (DML) statements like INSERT, UPDATE, or DELETE. SyntaxSyntax to insert multipl
2 min read
SQL - SELECT from Multiple Tables with MS SQL Server
In SQL we can retrieve data from multiple tables also by using SELECT with multiple tables which actually results in CROSS JOIN of all the tables. The resulting table occurring from CROSS JOIN of two contains all the row combinations of the 2nd table which is a Cartesian product of tables. If we consider table1 contains m rows and table2 contains n
3 min read
MySQL INSERT ON DUPLICATE KEY UPDATE Statement
MySQL INSERT ON DUPLICATE KEY UPDATE statement is an extension to the INSERT statement, that if the row being inserted already exists in the table, it will perform a UPDATE operation instead. INSERT ON DUPLICATE KEY UPDATE in MySQLINSERT ON DUPLICATE KEY UPDATE statement in MySQL is used to handle duplicate entries on a primary key or unique column
3 min read
Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
Structured Query Language (SQL): Structured Query Language (SQL) has a specific design motive for defining, accessing and changement of data. It is considered as non-procedural, In that case the important elements and its results are first specified without taking care of the how they are computed. It is implemented over the database which is drive
2 min read
Configure SQL Jobs in SQL Server using T-SQL
In this article, we will learn how to configure SQL jobs in SQL Server using T-SQL. Also, we will discuss the parameters of SQL jobs in SQL Server using T-SQL in detail. Let's discuss it one by one. Introduction :SQL Server Agent is a component used for database task automation. For Example, If we need to perform index maintenance on Production ser
7 min read
Reverse Statement Word by Word in SQL server
To reverse any statement Word by Word in SQL server we could use the SUBSTRING function which allows us to extract and display the part of a string. Pre-requisite :SUBSTRING function Approach : Declared three variables (@Input, @Output, @Length) using the DECLARE statement. Use the WHILE Loop to iterate every character present in the @Input. For th
2 min read
Multi-Statement Table Valued Function in SQL Server
In SQL Server, a multi-statement table-valued function (TVF) is a user-defined function that returns a table of rows and columns. Unlike a scalar function, which returns a single value, a TVF can return multiple rows and columns. Multi-statement function is very much similar to inline functions only difference is that in multi-statement function we
4 min read
SQL | INSERT IGNORE Statement
We know that a primary key of a table cannot be duplicated. For instance, the roll number of a student in the student table must always be distinct. Similarly, the EmployeeID is expected to be unique in an employee table. When we try to insert a tuple into a table where the primary key is repeated, it results in an error. However, with the INSERT I
2 min read
SQL | DESCRIBE Statement
Prerequisite: SQL Create Clause As the name suggests, DESCRIBE is used to describe something. Since in a database, we have tables, that's why do we use DESCRIBE or DESC(both are the same) commands to describe the structure of a table. Syntax: DESCRIBE one; OR DESC one; Note: We can use either DESCRIBE or DESC(both are Case Insensitive). Suppose our
2 min read
SQL INSERT INTO SELECT Statement
In SQL, the INSERT INTO statement is used to add or insert records to the specified table. We can use this statement to add data directly to the table. We use the VALUES keyword along with the INSERT INTO statement. VALUES keyword is accompanied by column names in a specific order in which we want to insert values in them. SELECT statement is used
5 min read
SQL CREATE VIEW Statement
The SQL CREATE VIEW statement is a very powerful feature in RDBMSs that allows users to create virtual tables based on the result set of a SQL query. Unlike regular tables, these views do not store data themselves rather they provide a way of dynamically retrieving and presenting data from one or many underlying tables. Views help simplify complex
4 min read
How to Combine LIKE and IN in SQL Statement
Combining 'LIKE' and 'IN' operators in SQL can greatly enhance users' ability to retrieve specific data from a database. By Learning how to use these operators together users can perform more complex queries and efficiently filter data based on multiple criteria, making queries more precise and targeted. This article explores how the LIKE and IN op
4 min read
SQL MERGE Statement
SQL MERGE Statement combines INSERT, DELETE, and UPDATE statements into one single query. MERGE Statement in SQLMERGE statement in SQL is used to perform insert, update, and delete operations on a target table based on the results of JOIN with a source table. This allows users to synchronize two tables by performing operations on one table based on
4 min read
SQL SELECT INTO Statement
The SQL SELECT INTO statement is used to copy data from one table into a new table. Note: The queries are executed in SQL Server, and they may not work in many online SQL editors so better use an offline editor. SyntaxSQL INSERT INTO Syntax is: SELECT column1, column2... INTO NEW_TABLE from SOURCE_TABLEWHERE Condition; To copy the entire data of th
3 min read
SQL DROP INDEX Statement
The SQL DROP INDEX statement removes an existing Index from a database table. SQL DROP INDEXThe SQL DROP INDEX Command is used to remove an index from the table. Indexes occupy space, which can cause extra time consumption on table modification operations. Benefits of Using DROP INDEX:Removing unused indexes can improve INSERT, UPDATE, and DELETE o
3 min read
DELETE Statement in MS SQL Server
The DELETE statement in MS SQL Server deletes specified records from the table. SyntaxMS SQL Server DELETE statement syntax is: DELETE FROM table_name WHERE condition; Note: Always use the DELETE statement with WHERE clause. The WHERE clause specifies which record(s) need to be deleted. If you exclude the WHERE clause, all records in the table will
1 min read
SQL Server MERGE Statement
SQL Server MERGE statement combines INSERT, UPDATE, and DELETE operations into a single transaction. MERGE in SQL ServerThe MERGE statement in SQL provides a convenient way to perform INSERT, UPDATE, and DELETE operations together, which helps handle the large running databases. But unlike INSERT, UPDATE, and DELETE statements MERGE statement requi
2 min read
Article Tags :
three90RightbarBannerImg