Deletion
- Delete all account records at the Kandy City branch.
delete * from account
where branch_name = ‘Kandy city’
- Delete all accounts at every branch located in Kandy.
delete * from account
where branch_name in (select branch_name
from branch
where branch_city=‘Kandy’)
Insertion
- Add a new tuple to account
insert into account
values (‘200116’, ‘Kandy City’,1200)
or equivalently
insert into account (branch_name, balance, account_number)
values (‘Kandy City’, 1200, ‘200116’)
- Add a new tuple to account with balance set to null
insert into account
values (‘100117’,‘Kandy City’, null)
Updates
- Increase all accounts with balances over Rs.1000 by 6%, all other accounts receive 5%.
- Write two update statements:
update account
set balance = balance *1.06
where balance > 1000
update account
set balance = balance * 1.05
where balance ≤ 1000
Thursday, July 30, 2009
Sunday, July 19, 2009
Learn Aggregate Functions (Group by and Having clause)
- These functions operate on the multiset of values of a column of a relation, and return a value
avg: average value
min: minimum value
max: maximum value
sum: sum of values
count: number of tuples/values
- Find the average account balance at the Kandy city branch.
select avg (balance)
from account
where branch_name = ‘kandy City’
- Find the number of depositors in the bank.
select count (*)
from customer
- Find the number of tuples in the customer relation.
select count (distinct customer_name)
from depositor
(MS Access does not allow last one)
Aggregate Functions – Group By
- Find the number of depositors for each branch.
Note: Attributes in select clause outside of aggregate functions must appear in group by list
select branch_name, count (customer_name)
from depositor, account
where depositor.account_number = account.account_number
group by branch_name
Aggregate Functions – Having Clause
- Find the names of all branches where the average account balance is more than Rs.1,200.
Note: predicates in the having clause are applied after the formation of groups whereas predicates in the where clause are applied before forming groups
select branch_name, avg (balance)
from account
group by branch_name
having avg (balance) > 1200
avg: average value
min: minimum value
max: maximum value
sum: sum of values
count: number of tuples/values
- Find the average account balance at the Kandy city branch.
select avg (balance)
from account
where branch_name = ‘kandy City’
- Find the number of depositors in the bank.
select count (*)
from customer
- Find the number of tuples in the customer relation.
select count (distinct customer_name)
from depositor
(MS Access does not allow last one)
Aggregate Functions – Group By
- Find the number of depositors for each branch.
Note: Attributes in select clause outside of aggregate functions must appear in group by list
select branch_name, count (customer_name)
from depositor, account
where depositor.account_number = account.account_number
group by branch_name
Aggregate Functions – Having Clause
- Find the names of all branches where the average account balance is more than Rs.1,200.
Note: predicates in the having clause are applied after the formation of groups whereas predicates in the where clause are applied before forming groups
select branch_name, avg (balance)
from account
group by branch_name
having avg (balance) > 1200
Saturday, July 18, 2009
Learn Joined Relations in SQL
- Join operations take two relations and return as a result another relation.
- These additional operations are typically used as sub query
expressions in the from clause
- Join condition – defines which tuples in the two relations match,
and what attributes are present in the result of the join.
- Join type – defines how tuples in each relation that do not match
any tuple in the other relation (based on the join condition) are
treated.

Joined Relations – Data sets for Examples
loan inner join borrower on
loan.loan_number = borrower.loan_number
- These additional operations are typically used as sub query
expressions in the from clause
- Join condition – defines which tuples in the two relations match,
and what attributes are present in the result of the join.
- Join type – defines how tuples in each relation that do not match
any tuple in the other relation (based on the join condition) are
treated.

Joined Relations – Data sets for Examples
loan inner join borrower onloan.loan_number = borrower.loan_number
Learn Nested Subqueries and Set Comparison use in SQL
- SQL provides a mechanism for the nesting of sub queries.
- A sub query is a select-from-where
expression that is nested within another query.
- A common use of sub queries is to perform tests for set membership, set comparisons,
and set cardinality.
Example Query For Nested Sub queries
- Find all customers who have both an account and a loan at the bank.
select distinct customer_name
from borrower
where customer_name in (select customer_name from depositor)
- Find all customers who have a loan at the bank but do not have an account at the bank
select distinct customer-name
from borrower
where customer_name not in (select customer_name from depositor)
Set Comparison
- Find all branches that have greater assets than some branch
located in kandy.
select distinct T.branch_name
from branch as T, branch as S
where T.assets > S.assets and S.branch_city = ‘kandy’
- Same query using > some clause
select branch_name
from branch
where assets > some(select assets
from branch
where branch_city = ‘kandy’)
- A sub query is a select-from-where
expression that is nested within another query.
- A common use of sub queries is to perform tests for set membership, set comparisons,
and set cardinality.
Example Query For Nested Sub queries
- Find all customers who have both an account and a loan at the bank.
select distinct customer_name
from borrower
where customer_name in (select customer_name from depositor)
- Find all customers who have a loan at the bank but do not have an account at the bank
select distinct customer-name
from borrower
where customer_name not in (select customer_name from depositor)
Set Comparison
- Find all branches that have greater assets than some branch
located in kandy.
select distinct T.branch_name
from branch as T, branch as S
where T.assets > S.assets and S.branch_city = ‘kandy’
- Same query using > some clause
select branch_name
from branch
where assets > some(select assets
from branch
where branch_city = ‘kandy’)
Subscribe to:
Posts (Atom)