Lecture Notes For DBMS: Set Operations
Lecture Notes For DBMS: Set Operations
Lecture Notes For DBMS: Set Operations
Set Operations
union, intersect and except operations are set operations available in SQL.
Relations participating in any of the set operation must be compatible; i.e. they must have
the same set of attributes.
Union Operation:
o Find all customers having a loan, an account, or both at the bank
(select customer-name
from Depositor )
union
(select customer-name
from Borrower )
It will automatically eliminate duplicates.
o If we want to retain duplicates union all can be used
(select customer-name
from Depositor )
union all
(select customer-name
from Borrower )
Intersect Operation
o Find all customers who have both an account and a loan at the bank
(select customer-name
from Depositor )
intersect
(select customer-name
from Borrower )
o If we want to retail all the duplicates
(select customer-name
from Depositor )
intersect all
(select customer-name
from Borrower )
Except Opeartion
o Find all customers who have an account but no loan at the bank
(select customer-name
from Depositor )
except
(select customer-name
from Borrower )
o If we want to retain the duplicates:
(select customer-name
from Depositor )
except all
(select customer-name
from Borrower )
Aggregate Functions
Aggregate functions are those functions which take a collection of values as input and
return a single value.
SQL offers 5 built in aggregate functions-
o Average: avg
o Minimum:min
o Maximum:max
o Total: sum
o Count:count
The input to sum and avg must be a collection of numbers but others may have
collections of non-numeric data types as input as well
Find the average account balance at the Sadar branch
select avg(balance)
from Account
where branch-name= “Sadar”
The result will be a table which contains single cell (one row and one column) having
numerical value corresponding to average balance of all account at sadar branch.
group by clause is used to form groups, tuples with the same value on all attributes in
the group by clause are placed in one group.
Find the average account balance at each branch
select branch-name, avg(balance)
from Account
group by branch-name
By default the aggregate functions include the duplicates.
distinct keyword is used to eliminate duplicates in an aggregate functions:
Find the number of depositors for each branch
select branch-name, count(distinct customer-name)
from Depositor, Account
where Depositor.account-number = Account.account-number
group by branch-name
having clause is used to state condition that applies to groups rather than tuples.
Find the average account balance at each branch where average account balance is more
than Rs. 1200
select branch-name, avg(balance)
from Account
group by branch-name
having avg(balance) > 1200
Count the number of tuples in Customer table
select count(*)
from Customer
SQL doesn’t allow distinct with count(*)
When where and having are both present in a statement where is applied before having.
Nested Subqueries
A subquery is a select-from-where expression that is nested within another query.
Set Membership
o The in and not in connectives are used for this type of subquery.
o “Find all customers who have both a loan and an account at the bank”, this query
can be written using nested subquery form as follows
select distinct customer-name
from Borrower
where customer-name in(select customer-name
from Depositor )
o Select the names of customers who have a loan at the bank, and whose names are
neither “Smith” nor “Jones”
select distinct customer-name
from Borrower
where customer-name not in(“Smith”, “Jones”)
Set Comparison
o Find the names of all branches that have assets greater than those of at least one
branch located in Mathura
select branch-name
from Branch
where asstets > some (select assets
from Branch
where branch-city = “Mathura” )
o Apart from > some others comparison could be < some , <= some , >= some ,
= some , < > some.
o Find the names of all branches that have assets greater than that of each branch
located in Mathura
select branch-name
from Branch
where asstets > all (select assets
from Branch
where branch-city = “Mathura” )
o Apart from > all others comparison could be < all , <= all , >= all , = all ,
< >all.