clear markdown compare help best fiddles feedback dbanow.uk a leap of faith?
clear markdown donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 1130824 fiddles created (16569 in the last week).

CREATE TABLE Orders ( Order_Number int, SKU varchar(10), PRIMARY KEY(Order_Number, SKU) );
 hidden batch(es)


INSERT INTO Orders (Order_Number, SKU) VALUES (1, 'abc'), (1, 'def'), (1, 'ghi'), (2, 'abc'), (2, 'ghi'), (3, 'def'), (4, 'abc'), (4, 'def'), (4, 'ghi'), (5, 'abc') ;
10 rows affected
 hidden batch(es)


-- set statistics xml on; SELECT Order_Number FROM (-- Get the list of Order_Number SELECT DISTINCT Order_Number FROM Orders ) AS onr WHERE -- Check that there is an 'abc' associated EXISTS ( SELECT * FROM Orders os1 WHERE os1.Order_Number = onr.Order_Number and os1.SKU = 'abc' ) AND -- and also Check that there is an 'ghi' associated EXISTS ( SELECT * FROM Orders os2 WHERE os2.Order_Number = onr.Order_Number and os2.SKU = 'ghi' ) ORDER BY onr.Order_Number
Order_Number
1
2
4
 hidden batch(es)


-- set statistics xml on; SELECT onr.Order_Number FROM (SELECT DISTINCT Order_Number FROM Orders ) AS onr JOIN Orders AS sk1 ON sk1.Order_Number = onr.Order_Number AND sk1.SKU = 'abc' JOIN Orders AS sk2 ON sk2.Order_Number = onr.Order_Number AND sk2.SKU = 'ghi' ORDER BY onr.Order_Number
Order_Number
1
2
4
 hidden batch(es)