I found a similar question but it didn't satisfy my answer: SQL: Select records where ALL joined records satisfy some condition
I have two tables, orders
and shipments
orders
have_many shipments
shipments have attribute status open/closed
I would like to query orders, where all of its shipments are closed
:
Assuming table of:
- order1, 2 shipments: 1open, 1closed
- order2, 3 shipments: 1open, 2closed
- order3, 1 shipments: 0open, 1closed
- order4, 2 shipments: 0open, 2closed
Running the query returns records for order3
and order4
I am currently doing this with N+1 using application code, I'd like to just implement in SQL.