SELECT I.iid FROM Item I
means "IDs of all items".
SELECT R.iid FROM Order R WHERE R.cid=C.cid
means "IDs of all items that customer C
has ever ordered" (where C
is determined by the containing query).
In general, query1 EXCEPT query2
means "all rows that are returned by query1
and are not returned by query2
". In your specific case, (SELECT I.iid FROM Item I) EXCEPT (SELECT R.iid FROM Order R WHERE R.cid=C.cid)
means "IDs of all items that customer C
has never ordered".
Inside a NOT EXISTS (subquery)
expression, all that really matters is whether subquery
returns any rows (in which case it "exists") or not. So (SELECT I.iid FROM Item I) EXCEPT (SELECT R.iid FROM Order R WHERE R.cid=C.cid)
exists if there are is any item that customer C
has never ordered — and it doesn't exist if there isn't any such item.
So the query as a whole finds the name of customers who have ordered every single item.