Download presentation
Presentation is loading. Please wait.
Published bySara Wheeler Modified over 9 years ago
1
Structured Query Language 2 Presented by: Annisa, M.Kom. Source: Database System Concepts 5 th edition
2
BRANCH ACCOUNT LOAN CUSTOMER DEPOSITOR BORROWER
3
Figure 3.3: Tuples inserted into loan and borrower
4
Figure 3.4: The loan and borrower relations
5
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 distinctwith distinct
6
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 distinctwith distinct
7
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
8
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
9
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
10
Aggregate Functions & NULL values select * 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
11
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.
12
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_name from borrower where customer_name not in (select customer_name from depositor ) select distinct customer_name from borrower where customer_name in (select customer_name from depositor )
13
Example Query Find all customers who have an account at branches located in Brooklyn. select branch_name from branch where branch_city = 'Brooklyn‘; select distinct (customer_name) from depositor where 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'));
14
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' )
15
Set Comparison Find all branches that have greater assets than some branch located in Brooklyn. Same query using > some clause select branch_name from branch where assets > some (select assets from branch where branch_city = ' Brooklyn ' ) select distinct T.branch_name from branch as T, branch as S where T.assets > S.assets and S.branch_city = ' Brooklyn ' >
16
Definition of Some Clause 0 5 6 (5 < some) = true 0 5 0 ) = false 5 0 5 (5 some) = true (since 0 5) (read: 5 < some tuple in the relation) (5 < some ) = true (5 = some (= some) in However, ( some) not in 0 5 6 (5 > some) = true
17
Example Query Find the names of all branches that have greater assets than all branches located in Brooklyn. select branch_name from branch where assets > all (select assets from branch where branch_city = 'Brooklyn')
18
Definition of all Clause F all r t r (F t) 0 5 6 (5 < all) = false 6 10 4 ) = true 5 4 6 (5 all ) = true (since 5 4 and 5 6) (5 < all ) = false (5 = all ( all) not in However, (= all) in
19
Rehat
20
Test for Empty Relations The exists construct returns the value true if the argument subquery is nonempty. exists r r Ø not exists r r = Ø
21
Exists Example Employee TableJob Table
22
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
23
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.
24
View Definition A view is defined using the create view statement which has the form create view v as where 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.
25
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_name from all_customer where branch_name = 'Perryridge'
26
Modification of the Database – Deletion Delete all account tuples at the Perryridge branch delete from account where branch_name = ' Perryridge ' Delete all accounts at every branch located in the city ‘Needham’. delete from account where branch_name in (select branch_name from branch where branch_city = ' Needham ' )
27
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)
28
Modification of the Database – Insertion Add a new tuple to account insert into account values ('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 account values ('A-777','Perryridge', null )
29
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 account set balance = balance 1.06 where balance > 10000 update account set balance = balance 1.05 where balance 10000
30
Joined Relations – Datasets for Examples Relation loan Relation borrower Note: borrower information missing for L-260 and loan information missing for L-155
31
Joined Relations – Examples loan inner join borrower on loan.loan_number = borrower.loan_number loan left outer join borrower on loan.loan_number = borrower.loan_number
32
Joined Relations – Examples loan natural inner join borrower loan natural right outer join borrower
33
Rehat
34
Pembahasan Latihan
35
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 Branch 4.Hapus field bonus dari tabel branch 5.Find the names of all branches in the loan relations 6.Find the names of all branches in the loan relations, and remove duplicates 7.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 $1300 12.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 branch 15.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 bank 19.Find all customers who have a loan at Perryridge branch and also have an account 20.Find all customers who have both an account and a loan at the Perryridge branch
36
1.Buat sebuah tabel branch, terdiri dari branch_name char(15) not null, branch_city char(30), dan assets integer create table branch (branch_namechar(15) not null, branch_citychar(30), assetsinteger) 2.Buat sebuah tabel branch, terdiri dari branch_name char(15) primary key, branch_city char(30), dan assets integer 1.create table branch (branch_name char(15), branch_citychar(30), assetsinteger, primary key (branch_name))
37
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_name from loan 6. Find the names of all branches in the loan relations, and remove duplicates select distinct branch_name from loan 7. Find all loan_number, branch_name, and amount (in thousand) select loan_number, branch_name, amount 1000 from loan
38
8. Find all loan number for loans made at the Perryridge branch with loan amounts greater than $1200. select loan_number from loan where branch_name = 'Perryridge' and amount > 1200 9. Find all loan number with loan amounts greater than $1200 or lower than $1000. select loan_number from loan where amount > 1200 or amount < 1000 10. Find all loan number with loan amounts is not 1300. select loan_number from loan where not amount = 1300
39
11. Find customer name with loan amounts between $500 and $1300 select b.customer_name from borrower b, loan l where 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‘)
40
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, amount from borrower, loan where 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_name from borrower, loan where borrower loan_number = loan.loan_number and branch_name = ' Perryridge ' order by customer_name
41
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 borrower where customer_name in (select customer_name from depositor )
42
18. 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 ) 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")
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.