Structured Query Language 2
Presented by: Annisa, M.Kom.
Source: Database System Concepts 5th edition
BRANCH
ACCOUNT
LOAN
CUSTOMER
DEPOSITOR
BORROWER
Figure 3.3: Tuples inserted into loan and borrower
Figure 3.4:The loan and borrower
relations
Tuple Variables Find the names of all branches that have greater assets than some branch located in Brooklyn.
select distinct T.branch_name from branch as T, branch as S where T.assets > S.assets and S.branch_city = 'Brooklyn'
>
>
without distinct with distinct
Aggregate Functions Find the number of depositors in the bank.
select count (distinct customer_name)from depositor
Not supported in Access
select count (customer_name)from (select distinct customer_name from depositor)
select distinct customer_name from depositor
select customer_name from depositor
67
without distinct with distinct
Aggregate Functions – Group By• Find the number of depositors for each branch.
select branch_name, customer_name
from depositor, account where depositor.account_number = account.account_number
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 $600.
select branch_name, balance from account
select branch_name, avg (balance) from account group by branch_name
select branch_name, sum(balance) from account group by branch_name
select branch_name, avg (balance) from account group by branch_name having avg (balance) > 600
Null Values and Aggregates• All aggregate operations except count(*) ignore tuples with null
values on the aggregated attributes.
http://www.sqlsnippets.com/en/topic-12656.html
Aggregate Functions & NULL valuesselect * from account
select avg (balance) from account
select sum(balance) from account
select branch_name, avg (balance) from account group by branch_name
select count(balance) from account
select branch_name, count (balance) from account group by branch_name
Nested Subqueries• SQL provides a mechanism for the nesting of subqueries.• A subquery is a select-from-where expression that is nested
within another query.
Example Query Find all customers who have both an account and a loan at the bank.
Find all customers who have a loan at the bank but do not have an account at the bank
select distinct customer_namefrom borrowerwhere customer_name not in (select customer_name
from depositor )
select distinct customer_namefrom borrowerwhere customer_name in (select customer_name
from depositor )
Example Query• Find all customers who have an account at branches located in
Brooklyn.
select branch_namefrom branchwhere branch_city = 'Brooklyn‘;
select distinct (customer_name) from depositorwhere account_number IN
(select account_number from account where branch_name IN (select branch_name from branch where branch_city = 'Brooklyn'));
select account_number from account where branch_name IN
(select branch_name from branch where branch_city = 'Brooklyn'));
Example Query• Find all customers who have both an account and a loan at the
Perryridge branch
select distinct customer_name from borrower, loan
where borrower.loan_number = loan.loan_number and branch_name = 'Perryridge'
and (customer_name) in
(select distinct customer_name from depositor, account
where depositor.account_number = account.account_number
and branch_name = 'Perryridge' )
Set Comparison Find all branches that have greater assets than some branch located
in Brooklyn.
Same query using > some clause
select branch_namefrom branchwhere assets > some (select assets from branch
where branch_city = 'Brooklyn')
select distinct T.branch_namefrom branch as T, branch as Swhere T.assets > S.assets and S.branch_city = 'Brooklyn'
>
Definition of Some Clause
056
(5 < some ) = true
05
0
) = false
5
05(5 some ) = true (since 0 5)
(read: 5 < some tuple in the relation)
(5 < some
) = true(5 = some
(= some) inHowever, ( some) not in
056
(5 > some ) = true
Example Query• Find the names of all branches that have greater assets than all
branches located in Brooklyn.
select branch_namefrom branchwhere assets > all
(select assetsfrom branchwhere branch_city = 'Brooklyn')
Definition of all Clause• F <comp> all r t r (F <comp> t)
056
(5 < all ) = false
610
4
) = true
5
46(5 all ) = true (since 5 4 and 5 6)
(5 < all
) = false(5 = all
( all) not inHowever, (= all) in
Rehat
Test for Empty Relations• The exists construct returns the value true if the argument subquery is
nonempty.• exists r r Ø• not exists r r = Ø
Exists ExampleEmployee Table Job Table
Example Query
SELECT branch_name
FROM branch
WHERE NOT EXISTS
(SELECT account_number
FROM account
WHERE branch_name = branch.branch_name)
Carilah semua cabang yang tidak memiliki account number
Views• In some cases, it is not desirable for all users to see the entire logical
model (that is, all the actual relations stored in the database.)• Consider a person who needs to know a customer’s name, loan number
and branch name, but has no need to see the loan amount. This person should see a relation described, in SQL, by
(select customer_name, borrower.loan_number, branch_name from borrower, loan where borrower.loan_number = loan.loan_number )
• A view provides a mechanism to hide certain data from the view of certain users.
• Any relation that is not of the conceptual model but is made visible to a user as a “virtual relation” is called a view.
View Definition• A view is defined using the create view statement which has the
form
create view v as < query expression >
where <query expression> is any legal SQL expression. The view name is represented by v.
• Once a view is defined, the view name can be used to refer to the virtual relation that the view generates.
• When a view is created, the query expression is stored in the database; the expression is substituted into queries using the view.
Example Queries• A view consisting of branches and their customers
• Find all customers of the Perryridge branch
create view all_customer as (select branch_name, customer_name from depositor, account where depositor.account_number =
account.account_number ) union (select branch_name, customer_name from borrower, loan where borrower.loan_number = loan.loan_number )
select customer_namefrom all_customerwhere branch_name = 'Perryridge'
Modification of the Database – Deletion• Delete all account tuples at the Perryridge branch
delete from accountwhere branch_name = 'Perryridge'
• Delete all accounts at every branch located in the city ‘Needham’.
delete from accountwhere branch_name in (select branch_name
from branch where branch_city = 'Needham')
Example Query
• Delete the record of all accounts with balances below the average at the bank.
delete from account where balance < (select avg (balance )
from account )
1. First, compute avg balance and find all tuples to delete
2. Next, delete all tuples found above (without recomputing avg or retesting the tuples)
Modification of the Database – Insertion• Add a new tuple to account
insert into accountvalues ('A-9732', 'Perryridge', 1200)
or equivalently
insert into account (branch_name, balance, account_number) values ('Perryridge', 1200, 'A-9732')
• Add a new tuple to account with balance set to null
insert into accountvalues ('A-777','Perryridge', null )
Modification of the Database – Updates
• Increase all accounts with balances over $10,000 by 6%, all other accounts receive 5%.– Write two update statements:
update accountset balance = balance 1.06where balance > 10000
update accountset balance = balance 1.05where balance 10000
Joined Relations – Datasets for Examples• Relation loan
Relation borrower
Note: borrower information missing for L-260 and loan information missing for L-155
Joined Relations – Examples • loan inner join borrower on
loan.loan_number = borrower.loan_number
loan left outer join borrower onloan.loan_number = borrower.loan_number
Joined Relations – Examples• loan natural inner join borrower
loan natural right outer join borrower
Rehat
Pembahasan Latihan
1. Buat sebuah tabel branch, terdiri dari branch_name char(15) not null, branch_city char(30), dan assets integer
2. Buat sebuah tabel branch, terdiri dari branch_name char(15) primary key, branch_city char(30), dan assets integer
3. Tambahkan sebuah field bonus integer ke dalam tabel Branch4. Hapus field bonus dari tabel branch5. Find the names of all branches in the loan relations6. Find the names of all branches in the loan relations, and remove duplicates7. Find all loan_number, branch_name, and amount (in thousand)8. Find all loan number for loans made at the Perryridge branch with loan amounts greater than
$1200.9. Find all loan number with loan amounts greater than $1200 or lower than $1000 .10. Find all loan number with loan amounts is not 1300 .11. Find customer name with loan amounts between $500 and $130012. Find the name, loan number and loan amount of all customers
having a loan at the Perryridge or downtown branch.
13. Find the name, loan number and loan amount of all customers; rename the column name loan_number as loan_id.
14. List in alphabetic order the names of all customers having a loan in Perryridge branch15. Find the number of depositors for each branch.16. Find the names of all branches where the average account balance is more than $1,200.17. Find all customers who have both an account and a loan at the bank.18. Find all customers who have a loan at the bank but do not have
an account at the bank19. Find all customers who have a loan at Perryridge branch and also have an account20. Find all customers who have both an account and a loan at the Perryridge branch
1. Buat sebuah tabel branch, terdiri dari branch_name char(15) not null, branch_city char(30), dan assets integer
create table branch(branch_name char(15) not null,branch_city char(30),assets integer)
2. Buat sebuah tabel branch, terdiri dari branch_name char(15) primary key, branch_city char(30), dan assets integer
3. create table branch (branch_name char(15), branch_city char(30), assets integer, primary key (branch_name))
3. Tambahkan sebuah field bonus integer ke dalam tabel Branch
alter table branch add bonus integer
4. Hapus field bonus dari tabel branch
alter table branch drop bonus
5. Find the names of all branches in the loan relations
select branch_namefrom loan
6. Find the names of all branches in the loan relations, and remove duplicates
select distinct branch_namefrom loan
7. Find all loan_number, branch_name, and amount (in thousand)
select loan_number, branch_name, amount 1000 from loan
8. Find all loan number for loans made at the Perryridge branch with loan amounts greater than $1200.
select loan_numberfrom loanwhere branch_name = 'Perryridge' and amount > 1200
9. Find all loan number with loan amounts greater than $1200 or lower than $1000 .
select loan_numberfrom loanwhere amount > 1200 or amount < 1000
10. Find all loan number with loan amounts is not 1300 .
select loan_numberfrom loanwhere not amount = 1300
11. Find customer name with loan amounts between $500 and $1300
select b.customer_namefrom borrower b, loan lwhere b.loan_number = l.loan_number and l.amount between
500 and 1300
12. Find the name, loan number and loan amount of all customers having a loan at the Perryridge or downtown branch.
select customer_name, borrower.loan_number, amount from borrower, loan where (borrower.loan_number = loan.loan_number and branch_name = 'Perryridge‘) or (borrower.loan_number =
loan.loan_number and branch_name = ‘Downtown‘)
13. Find the name, loan number and loan amount of all customers; rename the column name loan_number as loan_id.
select customer_name, borrower.loan_number as loan_id, amountfrom borrower, loanwhere borrower.loan_number = loan.loan_number
14. List in alphabetic order the names of all customers having a loan in Perryridge branch
select distinct customer_namefrom borrower, loanwhere borrower loan_number = loan.loan_number and branch_name = 'Perryridge' order by customer_name
15. 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
16. Find the names of all branches where the average account balance is more than $1,200.
select branch_name, avg (balance) from account group by branch_name having avg (balance) > 1200
17. Find all customers who have both an account and a loan at the bank.select distinct customer_name
from borrowerwhere customer_name in (select customer_name
from depositor )
18. Find all customers who have a loan at the bank but do not have an account at the bank
select distinct customer_namefrom borrowerwhere customer_name not in (select customer_name
from depositor )19. Find all customers who have a loan at Perryridge branch and also have an account
select distinct customer_name from (select customer_name from borrower,loan where borrower.loan_number=loan.loan_number and loan.branch_name="perryridge") where customer_name in (select customer_name from depositor )
20. Find all customers who have both an account and a loan at the Perryridge branch select distinct customer_name from (select customer_name from borrower,loan where
borrower.loan_number=loan.loan_number and loan.branch_name="perryridge") where customer_name in (select customer_name from account,depositor where account.account_number=depositor.account_number and account.branch_name="perryridge")
Top Related