Correlated UPDATE with Subquery
Correlated UPDATE with Subquery is a SQL statement in the Subqueries category. Updates a table using values computed from a correlated subquery referencing the outer row. The syntax is UPDATE table1 SET col = (SELECT agg FROM table2 WHERE table2.id = table1.id);. It returns rows updated. A typical example: UPDATE employees e SET dept_name = (SELECT name FROM departments d WHERE d.id = e.dept_id); -- Sets employee dept_name from the departments table UPDATE products p SET final_price = price * ( SELECT multiplier FROM discount_rules d WHERE d.category = p.category ); A close relative is Scalar Subquery, which a subquery that returns a single value (one row, one column). Can be used in SELECT, WHERE, HAVING clauses. A close relative is Correlated Subquery, which a subquery that references columns from the outer query. Evaluated for each row of the outer query. A close relative is EXISTS, which returns TRUE if the subquery returns at least one row. Often faster than IN for large datasets.