ch04 SQL
ch04 SQL
Basic Structure
Set Operations
Aggregate Functions
Null Values
Nested Subqueries
Derived Relations
Views
Modification of the Database
Joined Relations
Data Definition Language
Embedded SQL, ODBC and JDBC
select branch-name
from branch
where assets > some
(select assets
from branch
where branch-city = ‘Brooklyn’)
0
(5< some 5 ) = true
(read: 5 < some tuple in the relation)
6
0
(5< some 5 ) = false
0
(5 = some 5 ) = true
0
(5 some 5 ) = true (since 0 5)
(= some) in
However, ( some) not in
Database System Concepts 4.29 ©Silberschatz, Korth and Sudarshan
Definition of all Clause
0
(5< all 5 ) = false
6
6
(5< all 10 ) = true
4
(5 = all 5 ) = false
4
(5 all 6 ) = true (since 5 4 and 5 6)
( all) not in
However, (= all) in
Database System Concepts 4.30 ©Silberschatz, Korth and Sudarshan
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’)
select customer-name
from all-customer
where branch-name = ‘Perryridge’
update account
set balance = balance 1.05
where balance 10000
The order is important
Can be done better using the case statement (next slide)
update account
set balance = case
when balance <= 10000 then balance *1.05
else balance * 1.06
end
Motivating example
Transfer of money from one account to another involves two steps:
deduct from one account and credit to another
If one steps succeeds and the other fails, database is in an inconsistent state
Therefore, either both steps should succeed or neither should
If any step of a transaction fails, all work done by the transaction can be
undone by rollback work.
Rollback of incomplete transactions is done automatically, in case of
system failures
Relation borrower
customer-name loan-number
Jones L-170
Smith L-230
Hayes L-155
Note: borrower information missing for L-260 and loan
information missing for L-155
Find all customers who have either an account or a loan (but not
both) at the bank.
select customer-name
from (depositor natural full outer join borrower)
where account-number is null or loan-number is null
BRANCH_NAME CUSTOMER_NAME
------------- --------------
select branch_name, customer_name Perryridge Hayes
Downtown Johnson
from depositor, account Brighton Johnson
where depositor.account_number=account.account_number; Brighton Jones
Redwood Lindsay
Mianus Smith
select distinct customer_name Round Hill Turner
from borrower, loan
CUSTOMER_NAME
where borrower.loan_number = loan.loan_number and --------------
branch_name = 'Perryridge' Adams
Hayes
Database System Concepts 4.60 ©Silberschatz, Korth and Sudarshan
Example Query - 3
Find all customers who do have a loan at the bank, but do not have an
account at the bank.
select distinct customer_name
CUSTOMER_NAME
from borrower -------------
Adams
where customer_name not in ( select customer_name Curry
from depositor ) Williams
CUSTOMER_NAME CUSTOMER_NAME
------------- -------------
Hayes Adams
Johnson Curry
Johnson Hayes
Jones Johnson
Lindsay Jones
Smith Smith
Turner Williams
Database System Concepts 4.61 ©Silberschatz, Korth and Sudarshan
Example Query - 4
Find the name of branches having asset grater than at least one branch
located in 'Horseneck'.
BRANCH_NAME
select branch_name
------------
from branch Brighton
Downtown
where assets >some ( North Town
Perryridge
select assets Redwood
Round Hill
from branch
where branch_city = 'Horseneck')
ASSETS
select assets --------
400000
from branch
1700000
where branch_city = 'Horseneck'; 8000000
BRANCH_NAME AVG(BALANCE)
----------- ------------
Brighton 825
Downtown 500
Mianus 700
Perryridge 400
Redwood 700
Round Hill 350
Database System Concepts 4.63 ©Silberschatz, Korth and Sudarshan
Example Query - 6
Find the name of the customers having account in all the branches located in
Brooklyn city.
select distinct S.customer_name
from depositor S
CUSTOMER_NAME
where not exists ( --------------
(select branch_name Johnson
from branch
where branch_city = 'Brooklyn')
minus
(select R.branch_name
from depositor T, account R
where T.account_number = R.account_number and
T.customer_name = S.customer_name) );
(select R.branch_name
(select branch_name
from depositor T, account R
from branch
where T.account_number = R.account_number and
where branch_city = 'Brooklyn');
T.customer_name = ‘Johnson’);
BRANCH_NAME
------------
Downtown
Brighton
Database System Concepts 4.64 ©Silberschatz, Korth and Sudarshan
Example Query – 6 (cont)
BRANCH_NAME BRANCH_NAME
------------ ------------
Perryridge Brighton
BRANCH_NAME BRANCH_NAME
------------ ------------
Redwood Mianus
Customer
Depositor
Branch
Borrower