Update Multiple Columns in SQL

Learn via video course
FREE
View all courses
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
Topics Covered

How to Update Multiple Columns in SQL?

We can update multiple columns in SQL using the UPDATE command. The UPDATE statement is followed by a SET statement, which specifies the column(s) where the update is required.

Multiple columns in SQL

Syntax:

At first, we use the UPDATE command with the name of the table whose columns have to be updated. Then we use the SET command which specifies the columns to be updated and assigns them a new value. Lastly, the WHERE command is used to filter the records and update them at places only where required.

Examples of Updating Multiple Columns in SQL

We can update specified multiple columns or all the columns of a table using the UPDATE, SET, and WHERE commands.

Demo Database

Student Table

roll_nonameaddressage
1Reed RichardsEarth-6165
2Jason GurjarGujarat29
3Jim PamJammu48
4Robot SaxenaNULL1
5DomsDelhi35

This table has 4 columns: roll_noroll\_no, namename, addressaddress, and ageage.
The PRIMARY KEY is roll_noroll\_no of every student.
There are 5 students in this table.

Every example will make an update to this table and not an already updated table.

Updating Specified Columns

Reed Richards has gone to a different planet (Earth-9907) and has aged a lot (990), as a result, our Student table is now outdated. We will now update the address and age columns of the 1st row (roll_no = 1)

Code:

Output:

roll_nonameaddressage
1Reed RichardsEarth-9907990
2Jason GurjarGujarat29
3Jim PamJammu48
4Robot SaxenaNULL1
5DomsDelhi35

In this example, we have updated the address from 'Earth-616' to 'Earth-9907' and the age from 55 to 990990 in the first row.

Updating Multiple Columns of Multiple Rows

Doms and Robot Saxena have failed the school and two new students have taken admission in their place.

The details of the new students are unknown, so we will address them as "New Kid" and their address and age will be set as NULL. Let's make the required updates in the Student table.

Code:

Output:

roll_nonameaddressage
1Reed RichardsEarth-6165
2Jason GurjarGujarat29
3Jim PamJammu48
4New KidNULLNULL
5New KidNULLNULL

In the above example, we have updated the last two columns by changing their name, address, and age.

Updating a whole row, i.e. All Columns of a Single Row

In this example, we will update all the columns of the last row, as the previous details were wrong for that student.

Code:

Output:

roll_nonameaddressage
1Reed RichardsEarth-6165
2Jason GurjarGujarat29
3Jim PamJammu48
4Robot SaxenaNULL1
6MarthaGotham39

In the above example, we have updated the roll_no, name, address, and age of the last row.

Learn about GROUP BY multiple columns in SQL

The SQL GROUP BY clause is used with the SELECT statement to arrange identical data into groups.

Syntax:

The GROUP BY clause is used in the SELECT statement after the WHERE command and before the ORDER BY command.

To learn more about using GROUP BY with multiple columns, click here.

Conclusion

  • We can update the columns of a table using the UPDATE statement.
  • The SET command is used inside the UPDATE statement to specify the columns to update.
  • The WHERE command is used after the SET command to specify the conditions. The cells that satisfy the conditions are updated.
  • The GROUP BY clause is used to group data into groups.

See Also: