SQL

SQL - Update

The UPDATE statement in SQL is used to modify existing records in a table. Here’s a detailed tutorial on how to use the UPDATE statement effectively.

Basic Syntax

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

Steps to Update Data
  1. Specify the table name: Indicate the table that contains the data you want to update.
  2. Set the new values: Use the SET clause to specify the columns and their new values.
  3. Provide the condition: Use the WHERE clause to specify which rows should be updated. If you omit the WHERE clause, all rows in the table will be updated.

Examples

1. Updating Specific Columns

Assume you have a table named employees with columns first_name, last_name, age, and department.

Update the age of an employee named John Doe:


UPDATE employees
SET age = 31
WHERE first_name = 'John' AND last_name = 'Doe';

2. Updating Multiple Columns

Update the age and department of an employee named Jane Smith:


UPDATE employees
SET age = 29, department = 'Sales'
WHERE first_name = 'Jane' AND last_name = 'Smith';

3. Updating All Rows

Increase the age of all employees by 1 year:


UPDATE employees
SET age = age + 1;