SQL UPDATE statement: modify data

The UPDATE statement is used to modify existing records in a table.

Here is the basic syntax for the UPDATE statement:

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
  • table_name: Specify the name of the table you want to update.
  • SET: This keyword is used to set the values of the specified columns.
  • column1 = value1, column2 = value2, ...: The columns you wish to update and the corresponding values.
  • WHERE: This is an optional clause that can be used to specify the rows that will be updated. If the WHERE clause is not provided, all rows in the table will be updated.

Let's walk through an example:

Assume we have a table called Employees that has data as follows:

IDNAMEAGEADDRESSSALARY
1Rick35California65000
2Dan31New York75000
3Michelle24Los Angeles55000
4Ryan19Missouri45000

Now, if we want to update the address of the employee with the ID of 1, we could use an UPDATE statement like this:

UPDATE Employees
SET ADDRESS = 'San Francisco'
WHERE ID = 1;

After the above SQL is executed, the Employees table will look like this:

IDNAMEAGEADDRESSSALARY
1Rick35San Francisco65000
2Dan31New York75000
3Michelle24Los Angeles55000
4Ryan19Missouri45000

Notice that only the address of the employee with ID 1 has been updated.

Also, be very careful when using the UPDATE statement without a WHERE clause. This will update all rows in the table.

For instance, running the following command will increase everyone's salary by 5000:

UPDATE Employees
SET SALARY = SALARY + 5000;
  1. Modifying Data in SQL with UPDATE:

    • Description: UPDATE is used to modify existing records in a table.
    • Code Example:
      UPDATE your_table
      SET column1 = new_value
      WHERE condition;
      
  2. Updating Specific Columns in SQL:

    • Code Example:
      UPDATE your_table
      SET column1 = new_value1,
          column2 = new_value2
      WHERE condition;
      
  3. Using WHERE Clause in SQL UPDATE:

    • Description: The WHERE clause specifies which rows to update.
    • Code Example:
      UPDATE your_table
      SET column1 = new_value
      WHERE column2 = 'specific_condition';
      
  4. Updating Multiple Rows in SQL:

    • Code Example:
      UPDATE your_table
      SET column1 = new_value
      WHERE column2 IN ('value1', 'value2');
      
  5. SQL UPDATE Examples:

    • Code Example 1:
      UPDATE employees
      SET salary = salary * 1.1
      WHERE department = 'Engineering';
      
    • Code Example 2:
      UPDATE products
      SET stock_quantity = stock_quantity - 10
      WHERE product_id = 123;
      
  6. Updating Data in [Your Database System]:

    • Code Example (MySQL):
      UPDATE your_table
      SET column1 = new_value
      WHERE condition;
      
    • Code Example (SQL Server):
      UPDATE your_table
      SET column1 = new_value
      WHERE condition;
      
    • Code Example (PostgreSQL):
      UPDATE your_table
      SET column1 = new_value
      WHERE condition;
      
    • Code Example (Oracle):
      UPDATE your_table
      SET column1 = new_value
      WHERE condition;
      
  7. Conditional Updates in SQL:

    • Code Example:
      UPDATE your_table
      SET column1 = CASE
                       WHEN condition1 THEN new_value1
                       WHEN condition2 THEN new_value2
                       ELSE column1
                    END;
      
  8. Bulk Updates in SQL:

    • Description: Use UPDATE with a JOIN or a subquery for bulk updates.
    • Code Example (Using JOIN):
      UPDATE orders
      SET status = 'Shipped'
      FROM customers
      WHERE orders.customer_id = customers.customer_id
        AND customers.country = 'USA';
      
  9. Updating Data Based on Subqueries in SQL:

    • Code Example:
      UPDATE products
      SET price = price * 1.1
      WHERE category_id IN (SELECT category_id FROM categories WHERE category_name = 'Electronics');
      
  10. Handling NULL Values in SQL UPDATE:

    • Code Example:
      UPDATE your_table
      SET column1 = COALESCE(new_value, default_value)
      WHERE condition;
      
  11. Rolling Back Updates in SQL Transactions:

    • Description: Use transactions to roll back updates in case of errors.
    • Code Example:
      BEGIN TRANSACTION;
      
      UPDATE your_table
      SET column1 = new_value
      WHERE condition;
      
      -- Check for errors or conditions
      
      COMMIT; -- If everything is fine
      ROLLBACK; -- If an error occurs
      
  12. Updating Data in Related Tables Using SQL JOIN:

    • Code Example:
      UPDATE orders
      SET status = 'Shipped'
      FROM orders
      INNER JOIN customers ON orders.customer_id = customers.customer_id
      WHERE customers.country = 'USA';