Why can't i update a table and select from the same table in a subquery?
Example:
-- Trying to update while selecting from the same table
UPDATE employees SET salary = (SELECT AVG(salary) FROM employees);
Solution:
-- Correct way using a variable or a separate query
-- Pseudo-code:
SET @avg_salary = (SELECT AVG(salary) FROM employees);
UPDATE employees SET salary = @avg_salary;
Some SQL databases have restrictions on updating and selecting from the same table in a single query to avoid ambiguous situations. Using variables or breaking the operation into multiple steps can help.