Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

DELETE SUBQUERY

485900Oct 26 2006 — edited Oct 26 2006
I am trying to do a delete which really requires a join and I'm not sure how to do it.
Here is my scenario, say I have the following two tables:

create table items
(
id INT,
Name varchar2(50)
)

create table ItemAssign
(
itemid INT,
userid INT
)

There is a one to many relationship between items and ItemAssign, hence more than one person can be assigned to an item.

Now I want to delete all items assigned to a particular person but ONLY if they are the only person assigned to the item.
Here is query which performs very badly:

DELETE FROM items
WHERE id IN (SELECT itemid
FROM ItemAssign
WHERE (userid= 1)) AND (NOT EXISTS
(SELECT itemid
FROM ItemAssign IA
WHERE (items.id = IA.itemid) AND (IA.userid <> 1)))

Is there another way to write this query?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 23 2006
Added on Oct 26 2006
8 comments
642 views