Académique Documents
Professionnel Documents
Culture Documents
The select clause may also contain arithmetic expressions involving the
operators +, -, * and / operating on constants or attributes of tuples.
[Generalized projection in relational algebra]
select loan_no, amount * 100 from loan
SQL uses the logical connectives and, or and not. The operands of the
logical connectives can be expressions involving the comparison operators <,
<=, >, >=, = and <>. SQL allows us to use comparison operators to compare
strings and arithmetic expressions as well as special types such as dates.
SQL includes a between comparison to simplify where clause that specify <=
and >=. It is also possible to use not between operators.
b) Find the loan number of those loans with loan amounts between 400 and 800.
select loan_no
from loan
where amount between 400 and 800
or
select loan_number
from loan
where amount >= 400 and amount <=800
union
intersect
except/minus
1. Average: avg
2. Minimum: min
3. Maximum: max
4. Total: sum
5. Count: count
The input to sum and avg must be a collection of numbers, but the other
operators can operate on collection of non-numeric data types, such as string,
as well.
c) Find the average account balance, maximum account balance at each branch.
select branch_name, avg(balance), max(balance)
from account
group by branch_name
e) Find branch name and average balance where average balance is greater
than 400.
select branch_name, avg(balance)
from account
group by branch_name
having avg(balance) > 1200
f) Find branch name and average balance of Mirpur, Firmgate or Dhanmonidi
branch where average balance is greater than 1200.
select branch_name, avg(balance)
from account
where branch_name in (Mirpur, Firmgate, Dhanmondi)
group by branch_name
having avg(balance) > 1200
or
At times, we wish to treat the entire relation as a single group. In such cases,
group by is not used.
SQL does not allow the use of distinct with count(*). distinct can be used
with min and max, but result does not change.
i) Find the average balance for each customer who lives in Dhaka and has at
least three accounts.
select D.customer_name, avg(balance)
from account A, depositor D, customer C
where
A.account_no.
=
D.account_no.
C.customer_name and customer_city = Dhaka
group by D.customer_name
having count(distinct D.account_no) >=3
and
D.customer_name
returns null. So 5 + null returns null. Any comparison with null returns
unknown (other than is null and is not null). So, 5 < null or null <> null or null
= null returns unknown.
select sum(amount)
from loan
ignores null amount. Result is null if there is no non-null amount.
All aggregate functions except count(*) ignore tuple with null values on the
aggregated attributes.
The in connective tests for set membership, where the set is a collection of
values produced by a select clause. The not in connective tests for the
absence of the set membership.
d) Find all customers who have a loan at the bank and whose names are neither
Rahim nor karim.
select distinct customer_name
from borrower
where customer_name not in (Rahim, Karim)
The phrase greater than at least one is represented in SQL by > some
select branch_name
from branch
where assets > some (select assets
from branch
where branch_city = Brooklyn)
SQL also allows < some, <= some, >= some, = some and <> some
comparisons. Note that, = some is identical to in, whereas <> some is not
the same as not in.
The construct > all corresponds to the phrase greater than all.
b) Find the names of all branches that have an asset value greater than that of
each branch located in Brooklyn
select branch_name
from branch
where assets > all (select assets
from branch
where branch_city = Brooklyn)
SQL also allows < all, <= all, >= all, = all and <> all comparisons. Note that,
<> all is identical to not in.
from account
group by branch_name)
3.9 Views
a) Create a view consisting of branch names and the names of customers who
have either an account or loan at the bank.
create view all_customers as
(select branch_name, customer_name
from depositor d, account a
where d.account_no = a.account_no )
union
(select branch_name, customer_name
from borrower b, loan l
where b.loan_no = l.loan_no )
c) Delete all loans with loan amounts between 1300 and 1500.
delete from loan
where amount between 1300 and 1500
d) Delete all accounts at branches located in Brooklyn
delete from account
where branch_name in
(select branch_name
from branch
where branch_city = Brooklyn)
e) Delete the records of all accounts with balances below the average
delete from account
where balance < (select avg(balance)
from account)
3.10.3 Updates
a) Increase all balances by 5 percent.
update account
set balance = balance * 1.05
b) Give 6 percent interest for all accounts with balance over 700 and 5 percent
for the rest.
update account
set balance = balance * 1.06
where balance > 700
update account
set balance = balance * 1.05
where balance <= 700
Using case:
update account
set balance = case
when balance <= 700 then balance * 1.05
else balance * 1.06
end