Chapter 3: SQL
Chapter 3: SQL
Chapter 3: SQL
Data Definition
Basic Query Structure
Set Operations
Aggregate Functions
Null Values
Nested Subqueries
Complex Queries
Views
Modification of the Database
Joined Relations**
3.2
History
IBM Sequel language developed as part of System R project at the
SQL-86
SQL-89
SQL-92
SQL:2003
3.3
3.4
Example:
3.5
length n.
type).
digits.
3.6
3.7
3.8
relation:
All tuples in the relation are assigned null as the value for the
new attribute.
relation:
3.9
3.10
select branch_name
from loan
branch_name (loan)
NOTE: SQL names are case insensitive (i.e., you may use upper- or
lower-case letters.)
3.11
select.
Find the names of all branches in the loan relations, and remove
duplicates
3.12
select *
from loan
The select clause can contain arithmetic expressions involving the
E.g.:
3.13
To find all loan number for loans made at the Perryridge branch with
select loan_number
from loan
where branch_name = 'Perryridge' and amount > 1200
Comparison results can be combined using the logical connectives and,
3.14
select
from borrower, loan
Find the name, loan number and loan amount of all customers
having a loan at the Perryridge branch.
select customer_name, borrower.loan_number, amount
from borrower, loan
where borrower.loan_number = loan.loan_number and
branch_name = 'Perryridge'
3.15
old-name as new-name
E.g. Find the name, loan number and loan amount of all customers;
3.16
Tuple Variables
Tuple variables are defined in the from clause via the use of the as
clause.
Find the customer names and their loan numbers and amount for all
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'
3.17
String Operations
SQL includes a string-matching operator for comparisons on character
strings. The operator like uses patterns that are described using two
special characters:
Find the names of all customers whose street includes the substring
Main.
select customer_name
from customer
where customer_street like '% Main%'
3.18
Perryridge branch
3.19
Duplicates
In relations with duplicates, SQL can define how many copies of tuples
3.20
Duplicates (Cont.)
Example: Suppose multiset relations r1 (A, B) and r2 (C) are as
follows:
r1 = {(1, a) (2,a)}
A ,A ,,A ( P (r1 r2 rm ))
1
3.21
Set Operations
The set operations union, intersect, and except operate on relations
retain all duplicates use the corresponding multiset versions union all,
intersect all and except all.
Suppose a tuple occurs m times in r and n times in s, then, it occurs:
3.22
Set Operations
Find all customers who have a loan, an account, or both:
3.23
Aggregate Functions
These functions operate on the multiset of values of a column of
3.24
3.25
3.26
3.27
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.
3.28
In Construct
Find all customers who have both an account and a loan at the bank.
3.29
Example Query
Find all customers who have both an account and a loan at the
Perryridge branch
3.30
Some Construct
Find all branches that have greater assets than some branch located
in Brooklyn.
select branch_name
from branch
where assets > some
(select assets
from branch
where branch_city = 'Brooklyn')
3.31
All Construct
Find the names of all branches that have greater assets than all
select branch_name
from branch
where assets > all
(select assets
from branch
where branch_city = 'Brooklyn')
3.32
Exists Construct
Find all customers who have an account at all branches located in
Brooklyn.
3.33
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')
3.34
Example Query
Find all customers who have at least two accounts at the Perryridge
branch.
3.35
3.36
Example Query
Delete the record of all accounts with balances below the average at
the bank.
3.37
3.38
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 evaluated fully before any of its
3.39
3.40
update account
set balance = case
when balance <= 10000 then balance *1.05
else balance * 1.06
end
3.41
More Features
Joined Relations**
Join operations take two relations and return as a result another
relation.
Join condition defines which tuples in the two relations match, and
Join type defines how tuples in each relation that do not match any
tuple in the other relation (based on the join condition) are treated.
3.43
Relation borrower
3.44
loan.loan_number = borrower.loan_number
3.45
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
Database System Concepts, 5th Ed., June 2006
3.46
Solution:
3.47
Derived Relations
SQL allows a subquery expression to be used in the from clause
Find the average account balance of those branches where the average
3.48
View Definition
A relation that is not of the conceptual model but is made visible to
A view is defined using the create view statement which has the
form
3.49
Example Queries
A view consisting of branches and their customers
select customer_name
from all_customer
where branch_name = 'Perryridge'
3.50
Uses of Views
Hiding some information from some users
Define a view
(create view cust_loan_data as
select customer_name, borrower.loan_number, branch_name
from borrower, loan
where borrower.loan_number = loan.loan_number )
Grant the user permission to read cust_loan_data, but not borrower or
loan
3.51
Processing of Views
When a view is created
the query expression is stored in the database along with the view
name
to v2
3.52
View Expansion
A way to define the meaning of views defined in terms of other views.
Let view v1 be defined by an expression e1 that may itself contain uses
of view relations.
step:
repeat
Find any view relation vi in e1
Replace the view relation vi by the expression defining vi
until no more view relations are present in e1
As long as the view definitions are not recursive, this loop will
terminate
3.53
With Clause
The with clause provides a way of defining a temporary view whose
3.54
3.55
Update of a View
Create a view of all loan data in the loan relation, hiding the amount
attribute
3.56
create view v as
select loan_number, branch_name, amount
from loan
where branch_name = Perryridge
insert into v values ( 'L-99','Downtown', '23')
3.57
Null Values
It is possible for tuples to have a null value, denoted by null, for some
of their attributes
Example: Find all loan number which appear in the loan relation
with null values for amount.
select loan_number
from loan
where amount is null
3.58
or
null = null
unknown
3.59
3.60
End of Chapter 3
3.62
3.63
(5 = some
0
5
) = true
(5 some
0
5
) = true (since 0 5)
(= some) in
However, ( some) is not equivalent to not in
3.64
0
5
6
) = false
(5 < all
6
10
) = true
(5 = all
4
5
) = false
(5 all
4
6
( all) not in
However, (= all) is not equivalent to in
3.65
nonempty.
exists r r
not exists r r =
3.66
3.67
Figure 3.4:
The loan and borrower relations
3.68