Advertisement
❮ Previous: SQL Insert Into Next: SQL Delete ❯

SQL Update

The UPDATE statement is a Data Manipulation Language (DML) command used to modify existing records within a database table.

Unlike the INSERT command (which creates brand-new rows), UPDATE alters values inside columns that already exist.

In simple words the SQL UPDATE statement is used to modify existing records in a database table.


Basic Syntax

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

⚠️ CRITICAL WARNING: Always double-check your WHERE clause before executing an update query. If you omit the WHERE clause, every single row in your table will be modified with the new values.


Advertisement

Standard Examples

Updating a Single Column

To change the salary of a specific employee whose employee_id is 5:

UPDATE employees
SET salary = 65000
WHERE employee_id = 5;

Updating Multiple Columns

To modify both the email and job title for a specific employee at the same time, separate the assignments with a comma:

UPDATE employees
SET email = 'jane.doe@company.com', job_title = 'Senior Developer'
WHERE employee_id = 12;

Updating Based on Current Values

You can also calculate new values dynamically based on data already present in the table: [5]

-- Give a 10% raise to all employees in Department 3UPDATE employeesSET salary = salary * 1.10WHERE department_id = 3;


Advertisement

Advanced Operations

Update with a JOIN (Using Data From Another Table)

If you need to update a table using values stored in a secondary lookup or staging table, the syntax varies by database engine:

UPDATE eSET e.department_name = d.name
FROM employees e
INNER JOIN departments d ON e.department_id = d.id;
UPDATE employees e
SET department_name = d.name
FROM departments d
WHERE e.department_id = d.id;
UPDATE employees e
INNER JOIN departments d ON e.department_id = d.id
SET e.department_name = d.name;

Advertisement

Tips for Safe Production Updates

❮ Previous: SQL Insert Into Next: SQL Delete ❯
Advertisement