Suppose have a schema for Suppliers, Catalogue and Parts and we are trying to figure out what suppliers supply all parts except one.
Would this be the correct query? I’m unsure only because of that -1
at the end of the last select statement, I am unsure if this valid or not.
x
SELECT S.sid
FROM SUPPLIER S, CATALOGUE C
WHERE S.sid = C.sid
GROUP BY S.sid
HAVING COUNT(*) = ((SELECT COUNT(*) FROM PARTS) - 1))
Advertisement
Answer
You seem to want:
SELECT c.sid
FROM CATALOGUE c
GROUP BY c.sid
HAVING COUNT(*) = (SELECT COUNT(*) - 1 FROM PARTS)
You don’t need the SUPPLIERS
table if you only want the id.
I suppose a catalog could contain the same product multiple times. If this is possible, then you want COUNT(DISTINCT)
:
SELECT c.sid
FROM CATALOGUE c
GROUP BY c.sid
HAVING COUNT(DISTINCT c.<partid>) = (SELECT COUNT(*) - 1 FROM PARTS)
<partid>
refers to whatever column has the part information.