Can EXISTS be used with UPDATE?

Yes, EXISTS can be used with the UPDATE statement in SQL. It allows you to conditionally update records based on the existence of related records in another table or based on a subquery.

Example

Here’s an example of using EXISTS with an UPDATE statement:

UPDATE employees e
SET e.salary = e.salary * 1.10
WHERE EXISTS (
    SELECT 1
    FROM departments d
    WHERE d.department_id = e.department_id AND d.budget > 100000
);

In this example, the UPDATE statement increases the salary of employees by 10% only if their department exists and has a budget greater than 100,000. The EXISTS clause checks for the condition before performing the update.

0 Comments

no data
Be the first to share your comment!