Search code examples
sqljoinsql-delete

SQL Delete row if no relationships present


I have a Purchase Order table and an items table, a Purchase Order can have many Items but I want to remove all those purchase orders that have no items. Is that possible in SQL?


Solution

  • Of course.

    delete from purchase_orders
        where not exists (select 1
                          from purchase_order_items poi
                          where poi.purchase_order_id = purcahse_orders.purchase_order_id
                         );