Subqueries can also be used in UPDATE
and DELETE statements. Figure
shows two examples.
test=> DELETE FROM customer
test-> WHERE customer_id NOT IN (
test(> SELECT customer_id
test(> FROM salesorder
test(> );
DELETE 0
test=> UPDATE salesorder
test-> SET ship_date = '11/16/96'
test-> WHERE customer_id = (
test(> SELECT customer_id
test(> FROM customer
test(> WHERE name = 'Fleer Gearworks, Inc.'
test(> );
UPDATE 1
The first query deletes all customers with no sales orders. The second query sets the ship_date equal to '11/16/96' for all orders made by Fleer Gearworks, Inc. The numbers after DELETE and UPDATE indicate the number of rows affected by the queries.