Structured query language 2
This presentation is the property of its rightful owner.
Sponsored Links
1 / 40

Structured Query Language 2 PowerPoint PPT Presentation


  • 52 Views
  • Uploaded on
  • Presentation posted in: General

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.

Download Presentation

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Structured query language 2

Structured Query Language 2

Presented by: Annisa, M.Kom.

Source: Database System Concepts 5th edition


Structured query language 2

ACCOUNT

DEPOSITOR

BRANCH

CUSTOMER

BORROWER

LOAN


Tuple variables

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

>


Aggregate functions cont

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


Aggregate functions group by

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


Aggregate functions having clause

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


Null values and aggregates

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 values

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


Nested subqueries

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

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 )


Example query1

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


Example query2

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


Set comparison

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

>


Definition of some clause

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


Example query3

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

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


Rehat

Rehat


Test for empty relations

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 example

Exists Example

Employee Table

Job Table


Example query4

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)


Views

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.


View definition

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

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'


Modification of the database deletion

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


Example query5

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)


Modification of the database insertion

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 )


Modification of the database updates

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

Joined Relations – Datasets for Examples

  • Relation borrower

  • Relation loan

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


Joined relations examples

Joined Relations – Examples

  • loan inner join borrower onloan.loan_number = borrower.loan_number

  • loan left outer join borrower onloan.loan_number = borrower.loan_number


Joined relations examples1

Joined Relations – Examples

  • loan natural inner joinborrower

  • loan natural right outer join borrower


Rehat1

Rehat


Pembahasan l atihan

PembahasanLatihan


Structured query language 2

  • 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


Structured query language 2

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

  • 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_citychar(30),assetsinteger,primary key (branch_name))


Structured query language 2

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


Structured query language 2

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


Structured query language 2

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


Structured query language 2

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


Structured query language 2

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 )


Structured query language 2

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


  • Login