CRUD Statements  «Prev  Next»
Lesson 9Updating Data from Values in Another Table
ObjectiveConstruct UPDATE Transact-SQL statements that retrieve data from another table.

Updating Data from Values in Another Table in SQL Server

In this lesson, you will learn how to update data in one table using values or conditions from another table. Similar to the INSERT statement, you will use the FROM clause in your statement.

Widget Sales Service

In the previous lesson, we helped Widget Sales Service (WSS) track employee raises by inserting data from the Employee table into the SalaryHistory table. After monitoring the SalaryHistory table for several months, WSS management has identified that employees are generally underpaid, risking staff turnover. To address this, WSS plans to issue a 10% raise to all employees who have not received a raise in the past 12 months. Below are the SalaryHistory and Employee tables.

SalaryHistory

LastNameFirstNamePriorSalaryRaiseDate
DuckDonald50002024-01-01
JonesJohn300002023-06-15
FredericksFred450002024-06-09

Employee

LastName

FirstName

CurrentSalary

DuckDonald10000
MouseMickey20000
MouseMinnie30000
JonesJohn40000
FredericksFred50000

To accomplish this, we must update the CurrentSalary column in the Employee table to 1.1 times its current value for all employees who:
  1. are not in the SalaryHistory table, or
  2. are in the SalaryHistory table but have a RaiseDate more than 365 days before the current date.

Using a Join

Since the Employee table contains more employees than the SalaryHistory table, an inner join would exclude employees without salary history records. Instead, we use a LEFT JOIN to include all employees. The following Transact-SQL statement is explained in detail below.

Update in Transact-SQL


UPDATE Employee
SET CurrentSalary = CurrentSalary * 1.1
FROM Employee E LEFT JOIN SalaryHistory S
ON E.LastName = S.LastName
AND E.FirstName = S.FirstName
WHERE DATEDIFF(day, S.RaiseDate, GETDATE()) > 365
OR DATEDIFF(day, S.RaiseDate, GETDATE()) IS NULL;

Note: This syntax is specific to SQL Server (Transact-SQL). Other databases like MySQL or Oracle may use different syntax for updating with data from another table, such as a correlated subquery or the MERGE statement. Always consult the documentation for your specific database system.


Explanation of Each Part:

  1. UPDATE Employee: Specifies the Employee table as the target for updates.
  2. SET CurrentSalary = CurrentSalary * 1.1: Increases the CurrentSalary column by 10% for qualifying rows.
  3. FROM Employee E LEFT JOIN SalaryHistory S: Uses a LEFT JOIN to include all employees, even those without salary history records. Aliases E and S represent the Employee and SalaryHistory tables, respectively.
  4. ON E.LastName = S.LastName: Matches rows between tables based on the LastName column.
  5. AND E.FirstName = S.FirstName: Further refines the match using the FirstName column for precision.
  6. WHERE DATEDIFF(day, S.RaiseDate, GETDATE()) > 365: Filters employees whose last raise was more than 365 days ago.
  7. OR DATEDIFF(day, S.RaiseDate, GETDATE()) IS NULL: Includes employees with no salary history (where RaiseDate is NULL).
  8. LEFT JOIN rationale: Ensures all employees are included, even those without salary history records, which is critical for the update logic.
  9. Aliasing explained: Aliases E and S improve readability and allow clear column references from both tables.
  10. DATEDIFF explained: The DATEDIFF function calculates the number of days between RaiseDate and the current date, identifying outdated salary records.
  11. GETDATE(): Returns the current system date, used with DATEDIFF to evaluate the time since the last raise.
  12. NULL logic in outer joins: The LEFT JOIN results in NULL for RaiseDate for employees without salary history, which are included via the IS NULL condition.

Updating Data Values - Exercise

Click the exercise link below to practice updating data with values from another table. In the next lesson, you will learn how to delete data using values from another table. Updating Data Values - Exercise