Problem Statement
Which pattern safely updates a table based on a value computed by a subquery?
Explanation
You can compute a value in a subquery and set the column in one statement. This keeps the change atomic and clear.
Make sure the subquery is selective and indexed to avoid row by row scans in large tables.
Code Solution
SolutionRead Only
UPDATE orders o SET discount = ( SELECT CASE WHEN vip THEN 0.1 ELSE 0 END FROM customers c WHERE c.id = o.customer_id );
Practice Sets
This question appears in the following practice sets:
