1 / 40

Structured Query Language 2

Structured Query Language 2. Presented by: Annisa , M.Kom . Source: Database System Concepts 5 th edition. ACCOUNT. DEPOSITOR. BRANCH. CUSTOMER. BORROWER. LOAN. Tuple Variables. Find the names of all branches that have greater assets than some branch located in Brooklyn.

yair
Télécharger la présentation

Structured Query Language 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Structured Query Language 2 Presented by: Annisa, M.Kom. Source: Database System Concepts 5th edition

  2. ACCOUNT DEPOSITOR BRANCH CUSTOMER BORROWER LOAN

  3. Tuple Variables • Find the names of all branches that have greater assets than some branch located in Brooklyn. select distinct T.branch_namefrom branch as T, branch as Swhere T.assets > S.assetsand S.branch_city = 'Brooklyn' > without distinct with distinct >

  4. Aggregate Functions (Cont.) • Find the number of depositors in the bank. select customer_name from depositor select distinct customer_name from depositor select count (distinct customer_name)from depositor Not supported in Access select count (customer_name)from (select distinct customer_name from depositor) without distinct with distinct 7 6

  5. Aggregate Functions – Group By select branch_name, customer_name from depositor, accountwhere depositor.account_number = account.account_number • Find the number of depositors for each branch. select branch_name, count (customer_name)from depositor, accountwhere depositor.account_number = account.account_numbergroup by branch_name

  6. Aggregate Functions – Having Clause select branch_name, balancefrom account • Find the names of all branches where the average account balance is more than $600. select branch_name, sum(balance)from accountgroup by branch_name select branch_name, avg (balance)from accountgroup by branch_name select branch_name, avg (balance)from accountgroup by branch_namehaving avg(balance) > 600

  7. 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

  8. Aggregate Functions & NULL values select * from account select sum(balance)from account select count(balance)from account select branch_name, avg (balance)from accountgroup by branch_name selectavg (balance)from account select branch_name, count (balance)from accountgroup by branch_name

  9. 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.

  10. Example Query • Find all customers who have both an account and a loan at the bank. select distinctcustomer_namefrom borrowerwhere customer_namein(selectcustomer_namefromdepositor ) • 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_namenot in(select customer_namefrom depositor )

  11. Example Query select branch_namefrom branchwhere branch_city = 'Brooklyn‘; • Find all customers who have an account at branches located in Brooklyn. selectaccount_number from account wherebranch_nameIN (select branch_name from branch where branch_city = 'Brooklyn')); selectdistinct (customer_name) from depositor where account_numberIN (selectaccount_numberfrom account wherebranch_nameIN (selectbranch_namefrom branch where branch_city = 'Brooklyn'));

  12. Example Query • Find all customers who have both an account and a loan at the Perryridge branch select distinct customer_namefrom borrower, loan whereborrower.loan_number = loan.loan_number and branch_name = 'Perryridge' and (customer_name) in (select distinct customer_namefrom depositor, account wheredepositor.account_number = account.account_number and branch_name = 'Perryridge' )

  13. Set Comparison • Find all branches that have greater assets than some branch located in Brooklyn. select distinct T.branch_namefrom branch as T, branch as Swhere T.assets > S.assets andS.branch_city = 'Brooklyn' • Same query using > some clause selectbranch_namefrom branchwhere assets > some (select assetsfrom branchwherebranch_city = 'Brooklyn') >

  14. 0 0 5 5 6 6 Definition of Some Clause (5 > some ) = true (5 < some ) = true (read: 5 < some tuple in the relation) 0 (5 < some ) = false 5 0 ) = true (5 = some 5 0 (5 some ) = true (since 0  5) 5 (= some)  in However, ( some)  not in

  15. 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')

  16. 0 5 6 Definition of all Clause • F <comp> all r t r (F <comp> t) (5 < all ) = false 6 ) = true (5 < all 10 4 ) = false (5 = all 5 4 ) = true (since 5  4 and 5  6) (5 all 6 (all)  not in However, (= all)  in

  17. Rehat

  18. Test for Empty Relations • The exists construct returns the value true if the argument subquery is nonempty. • exists r  r  Ø • not exists r  r = Ø

  19. Exists Example Employee Table Job Table

  20. Example Query Carilahsemuacabang yang tidakmemiliki account number SELECT branch_name FROM branch WHERE NOT EXISTS (SELECT account_number FROM account WHERE branch_name = branch.branch_name)

  21. 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_namefrom borrower, loanwhere 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.

  22. 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.

  23. Example Queries create view all_customeras(select branch_name, customer_namefrom depositor, accountwhere depositor.account_number = account.account_number) union(select branch_name, customer_namefrom borrower, loanwhere borrower.loan_number = loan.loan_number) • A view consisting of branches and their customers • Find all customers of the Perryridge branch select customer_namefrom all_customerwhere branch_name = 'Perryridge'

  24. Modification of the Database – Deletion • Delete all account tuples at the Perryridge branch delete from accountwherebranch_name = 'Perryridge' • Delete all accounts at every branch located in the city ‘Needham’. delete from accountwhere branch_namein (select branch_namefrom branchwhere branch_city = 'Needham')

  25. Example Query • Delete the record of all accounts with balances below the average at the bank. delete from accountwhere 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)

  26. Modification of the Database – Insertion • Add a new tuple to account insert into accountvalues ('A-9732', 'Perryridge', 1200) or equivalentlyinsert 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 )

  27. 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

  28. Joined Relations – Datasets for Examples • Relation borrower • Relation loan • Note: borrower information missing for L-260 and loan information missing for L-155

  29. Joined Relations – Examples • loan inner join borrower onloan.loan_number = borrower.loan_number • loan left outer join borrower onloan.loan_number = borrower.loan_number

  30. Joined Relations – Examples • loan natural inner joinborrower • loan natural right outer join borrower

  31. Rehat

  32. PembahasanLatihan

  33. Buat sebuah tabel branch, terdiri dari branch_name char(15) not null, branch_city char(30), dan assets integer • Buat sebuah tabel branch, terdiri dari branch_name char(15) primary key, branch_city char(30), dan assets integer • Tambahkan sebuah field bonus integer ke dalam tabel Branch • Hapus field bonus dari tabel branch • Find the names of all branches in the loan relations • Find the names of all branches in the loan relations, and remove duplicates • Find all loan_number, branch_name, and amount (in thousand) • Find all loan number for loans made at the Perryridge branch with loan amounts greater than $1200. • Find all loan number with loan amounts greater than $1200 or lower than $1000 . • Find all loan number with loan amounts is not 1300 . • Find customer name with loan amounts between $500 and $1300 • Find the name, loan number and loan amount of all customers having a loan at the Perryridge or downtown branch. • Find the name, loan number and loan amount of all customers; rename the column name loan_number as loan_id. • List in alphabetic order the names of all customers having a loan in Perryridge branch • Find the number of depositors for each branch. • Find the names of all branches where the average account balance is more than $1,200. • 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 • Find all customers who have a loan at Perryridge branch and also have an account • Find all customers who have both an account and a loan at the Perryridge branch

  34. 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) • Buat sebuah tabel branch, terdiri dari branch_name char(15) primary key, branch_city char(30), dan assets integer • create table branch(branch_name char(15),branch_city char(30),assets integer,primary key (branch_name))

  35. 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) selectloan_number, branch_name, amount  1000from loan

  36. 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

  37. 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.amountbetween 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, amountfrom borrower, loanwhere (borrower.loan_number = loan.loan_number andbranch_name = 'Perryridge‘) or(borrower.loan_number = loan.loan_number andbranch_name = ‘Downtown‘)

  38. 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

  39. 15. Find the number of depositors for each branch. select branch_name, count (distinctcustomer_name)from depositor, accountwhere depositor.account_number = account.account_numbergroup 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 accountgroup by branch_namehaving avg(balance) > 1200 17. Find all customers who have both an account and a loan at the bank. select distinct customer_namefrom borrowerwhere customer_name in (select customer_namefromdepositor )

  40. 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_namefrom depositor ) 19. Find all customers who have a loan at Perryridge branch and also have an account selectdistinct 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")

More Related