1. SQL has the set operations union, intersect and except.
2. Find all customers having an account.
select distinct cname
from depositor
3. union: Find all customers having a loan, an account, or both. branch.
(select cname
from depositor)
union
(select cname
from borrower)
4. intersect: Find customers having a loan and an account.
(select distinct cname
from depositor)
intersect
(select distinct cname
from borrower)
5. except: Find customers having an account, but not a loan.
(select distinct cname
from depositor)
except
(select cname
from borrower)
6. Some additional details:
- union eliminates duplicates, being a set operation. If we want to retain duplicates, we may use union
all, similarly for intersect and except. - Not all implementations of SQL have these set operations.
- except in SQL-92 is called minus in SQL-86.
- It is possible to express these queries using other operations.
