SQL Update
Example Table
We will use the following table named employees for our examples:
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10, 2)
);
INSERT INTO employees (employee_id, name, department, salary) VALUES
(1, 'Alice', 'HR', 50000.00),
(2, 'Bob', 'IT', 60000.00),
(3, 'Charlie', 'Finance', 55000.00);
Employees Table
| employee_id | name | department | salary |
|---|---|---|---|
| 1 | Alice | HR | 50000.00 |
| 2 | Bob | IT | 60000.00 |
| 3 | Charlie | Finance | 55000.00 |
Using the SQL UPDATE Statement
The SQL UPDATE statement is used to modify the existing records in a table.
Example: Updating a Single Record
To update the salary of the employee with employee_id 1:
UPDATE employees
SET salary = 52000.00
WHERE employee_id = 1;
Result:
| Query | UPDATE employees SET salary = 52000.00 WHERE employee_id = 1; |
|---|---|
| Result |
|
Example: Updating Multiple Records
To update the department of all employees in the IT department to 'Tech':
UPDATE employees
SET department = 'Tech'
WHERE department = 'IT';
Result:
| Query | UPDATE employees SET department = 'Tech' WHERE department = 'IT'; |
|---|---|
| Result |
|