Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • SQL - Getting Started
  • What is SQL
  • Create Table
  • ALTER TABLE Statements
  • Rename Columns
  • Modify Column Type
  • Drop Columns
  • Rename Tables
  • Drop Tables
  • Insert Statement
  • Update Statement
  • Delete Statement
  • Truncate Statement
  • Merge Statement
  • Null Value
  • Select Query
  • WHERE Clause
  • GROUP BY Clause
  • HAVING Clause
  • ORDER BY Clause
  • SQL - Inner Join
  • SQL - Left Join
  • SQL - Right Join
  • SQL - Full Join
  • SQL - BETWEEN
  • SQL - IN
  • SQL - LIKE
  • SQL - INTERSECT
  • SQL - MINUS
  • SQL - UNION
  • SQL - UNION ALL
  • SQL - DISTINCT
  • SQL - ANY, SOME
  • SQL - ALL
  • SQL - AVG()
  • SQL - COUNT()
  • SQL - MAX()
  • SQL - MIN()
  • SQL - SUM()
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

SQL - UPDATE Statement

The UPDATE TABLE statement is used to update records of the table in the database.

Syntax:

UPDATE table_name SET column_name1 = new_value, column_name2 = new_value, ... [WHERE Condition];

Note that the WHERE clause is optional, but you should use it to update the specific record. An UPDATE statement without the WHERE clause will update values in all the rows of the table.

For the demo purpose, the following Employee table will be used in all examples here.

EmpIdFirstNameLastNameEmailPhoneNoSalary
1'John''King''john.king@abc.com''123.123.1834'33000
2'James''Bond'
3'Neena''Kochhar''neena@test.com''123.456.4568'17000
4'Lex''De Haan''lex@test.com''123.456.4569'15000

The following UPDATE statement will change the value of the Email column of the Employee table where the value of the EmpId is 1 in SQL Server, Oracle, MySQL, PostgreSQL, SQLite database.

SQL Script: Update Column
UPDATE Employee
SET email = 'jking@test.com'
WHERE EmpId = 1;

Now, the Select * from Employee query will display the following result.

EmpIdFirstNameLastNameEmailPhoneNoSalary
1'John''King''jking@test.com''123.123.1834'33000
2'James''Bond'
3'Neena''Kochhar''neena@test.com''123.456.4568'17000
4'Lex''De Haan''lex@test.com''123.456.4569'15000

Update Multiple Columns

Using the UPDATE statement, you can update more than one column of a table. The following query will change the Email and the PhoneNo in the Employee table whose EmpIdis 2.

SQL Script: Update Multiple Columns
UPDATE Employee
SET Email = 'jb007@test.com', PhoneNo = '111.111.0007'
WHERE EmpId = 2;

Now, the Select * from Employee query will display the following result.

EmpIdFirstNameLastNameEmailPhoneNoSalary
1'John''King''jking@test.com''123.123.1834'33000
2'James''Bond'jb007@test.com111.111.0007
3'Neena''Kochhar''neena@test.com''123.456.4568'17000
4'Lex''De Haan''lex@test.com''123.456.4569'15000

You can also update a column with the calculated value. The following will increase the salaries of all the employees to 10% in the Employee table using a single UPDATE statement.

SQL Script: Update Data
UPDATE Employee 
SET Salary = Salary + (Salary * 10/100);

Now, the Select * from Employee query will display the following result.

EmpIdFirstNameLastNameEmailPhoneNoSalary
1'John''King''jking@test.com''123.123.1834'36300
2'James''Bond'jb007@test.com111.111.0007
3'Neena''Kochhar''neena@test.com''123.456.4568'18700
4'Lex''De Haan''lex@test.com''123.456.4569'16500

You can use the subquery to update the data from another table. The following UPDATE statement will update the Salary in the Consultant table by selecting Salary from the Employee table for the matching EmpId values.

SQL Script: Update Values from Another Table
UPDATE Consultant
SET salary = (SELECT salary
FROM Employee WHERE Employee.EmpId = Consultant.EmpId);
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

contact@tutorialsteacher.com

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.