Update Multiple Columns in SQL
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.
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_no | name | address | age |
---|---|---|---|
1 | Reed Richards | Earth-616 | 5 |
2 | Jason Gurjar | Gujarat | 29 |
3 | Jim Pam | Jammu | 48 |
4 | Robot Saxena | NULL | 1 |
5 | Doms | Delhi | 35 |
This table has 4 columns: , , , and .
The PRIMARY KEY is 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_no | name | address | age |
---|---|---|---|
1 | Reed Richards | Earth-9907 | 990 |
2 | Jason Gurjar | Gujarat | 29 |
3 | Jim Pam | Jammu | 48 |
4 | Robot Saxena | NULL | 1 |
5 | Doms | Delhi | 35 |
In this example, we have updated the address from 'Earth-616' to 'Earth-9907' and the age from to 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_no | name | address | age |
---|---|---|---|
1 | Reed Richards | Earth-616 | 5 |
2 | Jason Gurjar | Gujarat | 29 |
3 | Jim Pam | Jammu | 48 |
4 | New Kid | NULL | NULL |
5 | New Kid | NULL | NULL |
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_no | name | address | age |
---|---|---|---|
1 | Reed Richards | Earth-616 | 5 |
2 | Jason Gurjar | Gujarat | 29 |
3 | Jim Pam | Jammu | 48 |
4 | Robot Saxena | NULL | 1 |
6 | Martha | Gotham | 39 |
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.