Link to home
Start Free TrialLog in
Avatar of frosty1
frosty1

asked on

how to delete records that contain no child records

I need to delete all order records that do not container order items.

the tables are linked by the orderId on the orderItems table. So i can do the following

select * from orders o
inner join orderitems oi on o.id = oi.orderid

How can i detect which order records contain no child orderItems
Avatar of devlab2012
devlab2012
Flag of India image

delete from orders o where not exists(select * from orderitems oi where oi.orderid = o.orderid)
backup your db before tyr
delete from orders where id not in
 (select orderid from orderitems )

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jmro20
jmro20
Flag of Puerto Rico image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I would recommend using jmro20's answer.  Among other points in its favor is the fact that it is probably going to provide the best performance.
Did you solved your problem?