Are you looking for SetIDs that have only properties 2 and 3 and only 2 and 3. Meaning not setIDs with just 2 or SetIDs with 2,3,4 etc?If so, you could try something like this (I populated a Temp table (variable) to join to):DECLARE @Sets TABLE (SetID INT)INSERT @SetsSELECT 1UNION ALL SELECT 2UNION ALL SELECT 3DECLARE @SetsProperties TABLE (ID INT, SetID INT, PropertyID INT)INSERT @SetsPropertiesSELECT 1, 1, 2UNION ALL SELECT 2, 1, 3UNION ALL SELECT 3, 2, 2UNION ALL SELECT 4, 2, 3UNION ALL SELECT 5, 2, 4UNION ALL SELECT 6, 3, 2DECLARE @Temp TABLE (PropertyID INT)INSERT @TempSELECT 2UNION ALL SELECT 3SELECT S.SetIDFROM @SetsProperties AS SINNER JOIN ( SELECT A.SetID FROM @SetsProperties AS A INNER JOIN @Temp AS B ON A.PropertyID = B.PropertyID GROUP BY A.SetID HAVING COUNT(*) = (SELECT COUNT(*) FROM @Temp) ) AS T ON S.SetID = T.SetIDGROUP BY S.SetIDHAVING COUNT(*) = (SELECT COUNT(*) FROM @Temp)
Also, you don't need the ID on SetsProperties as the SetID and PropertyID should be unique (f I understand your schema).