SQL Update: How To Update Multiple Rows or Columns in SQL

SQL Tutorial

SQL Update: How To Update Multiple Rows or Columns in SQL

We use sql update operation while we want to update a table record(s). Let’s say you have a Table which named as Employee. This Employee table contains following data;

EmployeeID FirstName LastName EmailAddress Salary
1 John Doe john@mail.com 50000
2 Will Smith smith@mail.com 56000
3 Goe Robert robert@mail.com 45000

Now if you want to update the Email Address column for an Employee which EmployeeID is 1 to something like doe@mail.com, then you have to execute the following sql command:

UPDATE Employee SET EmailAddress = ‘doe@mail.com’ WHERE EmployeeID = 1

This update sql command will update a single row for this Employee Table.

SQL Update Multiple Columns:

Let’s say you wants to update multiple column value like you wants to change the Last Name,Salary and Email Address for Goe Robert. Then you have to execute the below sql update multiple columns query:

UPDATE Employee SET LastName = ‘Peter’, EmailAddress = 'peter@mail.com', Salary=54000 WHERE EmployeeID = 3

You will notice here that, i have used single quotation for string type value like Last Name or Email Address. But when we have used number type or numeric value, we don’t need to use the single quotation.

SQL Update Multiple Rows:

Now if you want to update all the employee salary to 60000 at once by running a single sql update commands, then you need to execute this below sql update multiple rows commands:

UPDATE Employee SET Salary = 60000

You can see here that, we don’t used any where clause because we want sql command to apply for each row of your employee table. Which means, if you wants to apply any update which need to apply for each row of that table, then you don’t need to use the WHERE part.

Note: Be careful when you use any update sql commands in production or live database because, one mistake can change the data of that table.

I hope, now you have a clear understanding of sql update commands. Let me know your opinion in the comment section.

Nahidur Rahman Rifath

Passionate Software Engineer, Content Writer, Reader

Leave a Reply

Your email address will not be published. Required fields are marked *