Updating data in a SQL database is a fundamental operation that allows you to modify existing records. There are several ways to update SQL data, each with its own use cases and best practices. In this article, we will explore five common methods for updating SQL data, including their syntax, examples, and considerations for when to use each method.
Key Points
- UPDATE statement: used to modify existing records in a table
- UPDATE with WHERE clause: used to update specific records based on conditions
- UPDATE with JOIN: used to update records based on relationships with other tables
- UPDATE with subquery: used to update records based on the results of a subquery
- UPDATE with LIMIT: used to update a limited number of records
UPDATE Statement

The UPDATE statement is the most basic way to update SQL data. It allows you to modify one or more columns of a table by specifying the new values. The general syntax of the UPDATE statement is:
UPDATE table_name SET column1 = value1, column2 = value2,… WHERE condition;
For example, to update the salary of an employee in the “employees” table, you would use the following query:
UPDATE employees SET salary = 50000 WHERE employee_id = 1;
This query updates the salary of the employee with ID 1 to 50000.
UPDATE with WHERE Clause
The WHERE clause is used to specify conditions for which records to update. This allows you to update only specific records that meet certain criteria. The general syntax of the UPDATE statement with a WHERE clause is:
UPDATE table_name SET column1 = value1, column2 = value2,… WHERE condition;
For example, to update the salaries of all employees in the “employees” table who work in the sales department, you would use the following query:
UPDATE employees SET salary = salary * 1.1 WHERE department = ‘Sales’;
This query updates the salaries of all employees in the sales department by 10%.
UPDATE with JOIN

The JOIN clause is used to combine rows from two or more tables based on a related column. You can use the JOIN clause to update records based on relationships with other tables. The general syntax of the UPDATE statement with a JOIN clause is:
UPDATE table1 SET column1 = value1, column2 = value2,… FROM table1 JOIN table2 ON table1.column = table2.column WHERE condition;
For example, to update the order status of all orders in the “orders” table that have been shipped, you would use the following query:
UPDATE orders SET status = ‘Shipped’ FROM orders JOIN shipments ON orders.order_id = shipments.order_id WHERE shipments.ship_date IS NOT NULL;
This query updates the status of all orders that have been shipped to “Shipped”.
UPDATE with Subquery
A subquery is a query nested inside another query. You can use subqueries to update records based on the results of a subquery. The general syntax of the UPDATE statement with a subquery is:
UPDATE table_name SET column1 = value1, column2 = value2,… WHERE condition IN (SELECT column FROM table_name WHERE condition);
For example, to update the salaries of all employees in the “employees” table who work in the top 10% of departments by average salary, you would use the following query:
UPDATE employees SET salary = salary * 1.1 WHERE department IN (SELECT department FROM ( SELECT department, AVG(salary) AS avg_salary FROM employees GROUP BY department ORDER BY avg_salary DESC LIMIT 10 ) AS top_departments);
This query updates the salaries of all employees in the top 10% of departments by average salary by 10%.
UPDATE with LIMIT
The LIMIT clause is used to limit the number of records updated. This can be useful when you want to update a limited number of records, such as when testing a query. The general syntax of the UPDATE statement with a LIMIT clause is:
UPDATE table_name SET column1 = value1, column2 = value2,… WHERE condition LIMIT number;
For example, to update the salaries of the top 5 employees in the “employees” table by average salary, you would use the following query:
UPDATE employees SET salary = salary * 1.1 ORDER BY salary DESC LIMIT 5;
This query updates the salaries of the top 5 employees by average salary by 10%.
Method | Syntax | Example |
---|---|---|
UPDATE statement | UPDATE table_name SET column1 = value1, column2 = value2,... WHERE condition; | UPDATE employees SET salary = 50000 WHERE employee_id = 1; |
UPDATE with WHERE clause | UPDATE table_name SET column1 = value1, column2 = value2,... WHERE condition; | UPDATE employees SET salary = salary * 1.1 WHERE department = 'Sales'; |
UPDATE with JOIN | UPDATE table1 SET column1 = value1, column2 = value2,... FROM table1 JOIN table2 ON table1.column = table2.column WHERE condition; | UPDATE orders SET status = 'Shipped' FROM orders JOIN shipments ON orders.order_id = shipments.order_id WHERE shipments.ship_date IS NOT NULL; |
UPDATE with subquery | UPDATE table_name SET column1 = value1, column2 = value2,... WHERE condition IN (SELECT column FROM table_name WHERE condition); | UPDATE employees SET salary = salary * 1.1 WHERE department IN (SELECT department FROM (SELECT department, AVG(salary) AS avg_salary FROM employees GROUP BY department ORDER BY avg_salary DESC LIMIT 10) AS top_departments); |
UPDATE with LIMIT | UPDATE table_name SET column1 = value1, column2 = value2,... WHERE condition LIMIT number; | UPDATE employees SET salary = salary * 1.1 ORDER BY salary DESC LIMIT 5; |

What is the difference between UPDATE and REPLACE in SQL?
+The UPDATE statement is used to modify existing records in a table, while the REPLACE statement is used to replace existing records with new ones. The main difference between the two is that UPDATE modifies the existing records, while REPLACE deletes the existing records and inserts new ones.
How do I update multiple tables in a single query?
+You can update multiple tables in a single query using the JOIN clause. This allows you to combine rows from two or more tables based on a related column, and then update the records in one or more of the tables.
What is the purpose of the WHERE clause in an UPDATE statement?
+The WHERE clause is used to specify conditions for which records to update. This allows you to update only specific records that meet certain criteria, rather than updating all records in the table.
In conclusion, updating SQL data is a crucial operation that requires careful consideration and planning. By understanding the different methods for updating SQL data, including the UPDATE statement, UPDATE with WHERE clause, UPDATE with JOIN, UPDATE with subquery, and UPDATE with LIMIT, you can ensure that your database remains up-to-date and accurate. Remember to always back up your data before making any changes, and test your queries on a small dataset before applying them to your entire database.