-
SQL has the set operations union, intersect and except.
-
Find all customers having an account.
select distinct cname
from depositor
-
union: Find all customers having a loan, an account, or both.
branch.
(select cname
from depositor)
union
(select cname
from borrower)
-
intersect:
Find customers having a loan and an account.
(select distinct cname
from depositor)
intersect
(select distinct cname
from borrower)
-
except:
Find customers having an account, but not a loan.
(select distinct cname
from depositor)
except
(select cname
from borrower)
-
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.