CH 4 Unit 2 SQL
CH 4 Unit 2 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 loan-number
from loan
where amount between 90000 and 100000
● Find the name, loan number and loan amount of all customers
having a loan at the Perryridge branch.
● Find the name, loan number and loan amount of all customers; rename the
column name loan-number as loan-id.
● Find the names of all branches where the average account balance is
more than $1,200.
● Find all customers who have a loan at the bank but do not have
an account at the bank
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
61
(5< all ) = true
0
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’)
● The unique construct tests whether a subquery has any duplicate tuples
in its result.
● Find all customers who have at most one account at the Perryridge
branch.
select T.customer-name
from depositor as T
where unique (
select R.customer-name
from account, depositor as R
where T.customer-name = R.customer-name and
R.account-number = account.account-number and
account.branch-name = ‘Perryridge’)
● (Schema used in this example)
where:
●<query expression> is any legal expression
●The view name is represented by v
select customer-name
from all-customer
where branch-name = ‘Perryridge’
with max-balance(value) as
select max (balance)
from account
select account-number
from account, max-balance
where account.balance = max-balance.value
● Provide as a gift for all loan customers of the Perryridge branch, a $200
savings account. Let the loan number serve as the account number for the
new savings account
insert into account
select loan-number, branch-name, 200
from loan
where branch-name = ‘Perryridge’
insert into depositor
select customer-name, loan-number
from loan, borrower
where branch-name = ‘Perryridge’
and loan.account-number = borrower.account-number
● The select from where statement is fully evaluated before any of its results
are inserted into the relation (otherwise queries like
insert into table1 select * from table1
would cause problems
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
From within a host language, find the names and cities of customers
with more than the variable amount dollars in some account.
● Can update tuples fetched by cursor by declaring that the cursor is for
update
declare c cursor for
select *
from account
where branch-name = ‘Perryridge’
for update
● To update tuple at the current location of cursor
update account
set balance = balance + 100
where current of c
SQLDisconnect(conn);
SQLFreeConnect(conn);
SQLFreeEnv(env);
}
pStmt.setString(1, "A-9733");
pStmt.executeUpdate();
● Beware: If value to be stored in database contains a single quote or other
special character, prepared statements work fine, but creating a query
string and executing it directly would result in a syntax error!
Network