SQL UPDATE Statement

The SQL UPDATE statement is used to update data stored in database tables.

SQL UPDATE Statement Syntax

UPDATE TableName
SET Column1=Value1, Column2=Value2, ...
WHERE conditions

The WHERE clause is very important for SQL UPDATE statement. If it’s omitted, all records will be modified.

SQL UPDATE Example

Table: Employees

EmployeeId FirstName LastName Department Salary
203 Mary Fox Finance 78000
204 Joe Lanyon Finance 45800
303 Dona Earl Development 55000
304 Tony Oakes Development 49000

Example 1
We need to change Dona Earl’s department to Finance:

UPDATE Employees
SET Department='Finance'
WHERE FirstName='Dona' AND LastName='Earl'

The Employees table will be updated to:

EmployeeId FirstName LastName Department Salary
203 Mary Fox Finance 78000
204 Joe Lanyon Finance 45800
303 Dona Earl Finance 55000
304 Tony Oakes Development 49000

Example 2
We want to raise Tony Oakes’s salary by $5000:

UPDATE Employees
SET Salary = Salary + 5000
WHERE FirstName='Tony' AND LastName='Oakes'

Now, the table will look like:

EmployeeId FirstName LastName Department Salary
203 Mary Fox Finance 78000
204 Joe Lanyon Finance 45800
303 Dona Earl Finance 55000
304 Tony Oakes Development 54000

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>