Meritshot Tutorials

  1. Home
  2. »
  3. SQL Update Statement

SQL Tutorial

SQL Update Statement

The UPDATE statement is used to modify existing records in a table. You can update one or more rows at a time based on specific conditions.

Basic Syntax

Syntax:

UPDATE table_name

SET column1 = value1, column2 = value2, …

WHERE condition;

Note: Always use the WHERE clause to specify which records to update. Without it, all records in the table will be updated.

Example 1: Update a Single Record

Update the salary of a specific employee based on their employee_id.

Example:

Increase the salary of the employee with employee_id 5 by 10%:

UPDATE employees

SET salary = salary * 1.10

WHERE employee_id = 5;

Output:

If the previous salary of employee with employee_id 5 was 45000, it will now be updated to 49500.

employee_id

first_name

last_name

salary

5

Anil

Kapoor

49500

 

Example 2: Update Multiple Records

Update the department of employees who are currently in ‘Marketing’ to ‘Sales’.

Example:

Change the department of all employees in ‘Marketing’ to ‘Sales’:

UPDATE employees

SET department = ‘Sales’

WHERE department = ‘Marketing’;

Output:

If there were multiple employees in ‘Marketing’, their department will now be ‘Sales’.

employee_id

first_name

last_name

department

5

Anil

Kapoor

Sales

9

Ananya

Kumar

Sales

 

Example 3: Update with NULL Values

Update the age of employees who do not have a recorded age.

Example:

Set a default age of 30 for employees where age is NULL:

UPDATE employees

SET age = 30

WHERE age IS NULL;

Output:

If the employees with NULL age were updated, they will now have an age of 30.

employee_id

first_name

last_name

age

12

Meera

Patel

30

 

Tips to Remember

  1. Use WHERE Clause Carefully: Always use the WHERE clause to target specific records. Omitting it will update all rows.
  2. Backup Data: Consider backing up your data before performing an update, especially if updating multiple rows.
  3. Test with SELECT: Before executing an UPDATE, run a SELECT statement with the same WHERE condition to see which rows will be affected.
  4. Transactions: Use transactions (BEGIN, COMMIT, ROLLBACK) to ensure data integrity and to undo changes if necessary.

 

Power of UPDATE Statement

The UPDATE statement:

  • Modifies Existing Data: Allows you to correct or change existing records in the table.
  • Supports Bulk Updates: Can update multiple rows based on specified conditions.

Preserves Data Integrity: Allows for targeted changes to maintain consistency in the database.

Frequently Asked Questions

Q1: What if I accidentally update the wrong records?
A1: Use ROLLBACK if you are using transactions, or restore from a backup if available.

Q2: Can I update data using values from another table?
A2: Yes, you can use a subquery to update records based on values from another table.

Example:

UPDATE employees

SET department = (SELECT new_department FROM departments WHERE departments.id = employees.department_id)

WHERE EXISTS (SELECT 1 FROM departments WHERE departments.id = employees.department_id);

Q3: How do I update data with complex conditions?
A3: Use AND, OR, and parentheses to build complex conditions in the WHERE clause.

Example:

UPDATE employees

SET salary = 55000

WHERE department = ‘IT’ AND age > 30;

Q4: Can I use UPDATE to set a column to NULL?
A4: Yes, you can set a column to NULL using the UPDATE statement.

Example:

UPDATE employees

SET salary = NULL

WHERE employee_id = 10;

Q5: How do I ensure that the update operation does not affect unintended rows?
A5: Carefully construct your WHERE clause to accurately target only the intended rows. Test with a SELECT query first to verify the affected rows.