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

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

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

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

Structured Query Language 2

Presented by: Annisa, M.Kom.

Source: Database System Concepts 5th edition

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

BRANCH

ACCOUNT

LOAN

CUSTOMER

DEPOSITOR

BORROWER

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

Figure 3.3: Tuples inserted into loan and borrower

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

Figure 3.4:The loan and borrower

relations

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

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

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 )

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

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'));

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

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

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

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'

>

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

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

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

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

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

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

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

Rehat

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

Test for Empty Relations• The exists construct returns the value true if the argument subquery is

nonempty.• exists r r Ø• not exists r r = Ø

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

Exists ExampleEmployee Table Job Table

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

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

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

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.

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

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.

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

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'

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

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

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

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)

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

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 )

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

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

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

Joined Relations – Datasets for Examples• Relation loan

Relation borrower

Note: borrower information missing for L-260 and loan information missing for L-155

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

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

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

Joined Relations – Examples• loan natural inner join borrower

loan natural right outer join borrower

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

Rehat

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

Pembahasan Latihan

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

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

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

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

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

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

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

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

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

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‘)

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

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

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

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 )

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

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")