Upload
monica-wilkerson
View
225
Download
0
Embed Size (px)
Citation preview
2
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 customers having a loan at some branch.
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' Keyword as is optional and may be omitted
borrower as T ≡ borrower T Some database such as Oracle require as to be omitted
select customer_name, T.loan_number, S.amount from borrower as T, loan as S where T.loan_number = S.loan_number
3
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: percent (%). The % character matches any substring. underscore (_). The _ character matches any character.
Find the names of all customers whose street includes the substring “Main”.
select customer_namefrom customerwhere customer_street like '%Main%'
Match the name “Main%”like 'Main\%' escape '\'
SQL supports a variety of string operations such as concatenation (using “||”) converting from upper to lower case (and vice versa) finding string length, extracting substrings, etc.
4
Ordering the Display of Tuples
• 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• We may specify desc for descending order
or asc for ascending order, for each attribute; ascending order is the default.Example: order by customer_name desc
5
Set Operations• The set operations union, intersect, and
except operate on relations and correspond to the relational algebra operations
• Each of the above operations automatically eliminates duplicates; to 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:• m + n times in r union all s• min(m,n) times in r intersect all s• max(0, m – n) times in r except all s
6
Set Operations
Find all customers who have a loan, an account, or both:
(select customer_name from depositor)except(select customer_name from borrower)
(select customer_name from depositor)intersect(select customer_name from borrower)
Find all customers who have an account but no loan.
(select customer_name from depositor)union(select customer_name from borrower)
Find all customers who have both a loan and an account.
7
Aggregate Functions
These functions operate on the multiset of values of a column of a relation, and return a value
avg: average valuemin: minimum valuemax: maximum valuesum: sum of valuescount: number of values
8
Aggregate FunctionsFind the average account balance at the Perryridge branch.
Find the number of depositors in the bank.
Find the number of tuples in the customer relation.
select avg (balance)from accountwhere branch_name = 'Perryridge'
select count (*)from customer
select count (distinct customer_name)from depositor
9
Motivation for Grouping So far, we’ve applied aggregate operators to all
(qualifying) tuples. Sometimes, we want to apply them to each of several groups of tuples.
Consider: Find the age of the youngest sailor for each rating level. In general, we don’t know how many rating
levels exist, and what the rating values for these levels are!
Suppose we know that rating values go from 1 to 10; we can write 10 queries that look like this (!):
SELECT MIN (S.age)FROM Sailors SWHERE S.rating = i
For i = 1, 2, ... , 10:
10
Queries With GROUP BY and HAVING
The target-list contains (i) attribute names (ii) terms with aggregate operations (e.g., MIN (S.age)). The attribute list (i) must be a subset of grouping-
list. Intuitively, each answer tuple corresponds to a group, and these attributes must have a single value per group. (A group is a set of tuples that have the same value for all attributes in grouping-list.)
SELECT [DISTINCT] target-listFROM relation-listWHERE qualificationGROUP BY grouping-listHAVING group-qualification
11
Conceptual Evaluation
The cross-product of relation-list is computed, tuples that fail qualification are discarded, `unnecessary’ fields are deleted, and the remaining tuples are partitioned into groups by the value of attributes in grouping-list.
The group-qualification is then applied to eliminate some groups. Expressions in group-qualification must have a single value per group! In effect, an attribute in group-qualification that is
not an argument of an aggregate op also appears in grouping-list.
One answer tuple is generated per qualifying group.
12
Aggregate Functions – Group By
Find the number of depositors for each branch.
Note: Attributes in select clause outside of aggregate functions must appear in group by list
select branch_name, count (distinct customer_name) from depositor, account where depositor.account_number = account.account_number group by branch_name
13
Aggregate Functions – Having Clause
Find the names of all branches where the average account balance is more than $1,200.
Note: predicates in the having clause are applied after the formation of groups whereas predicates in the where clause are applied before forming groups
select branch_name, avg (balance) from account group by branch_name having avg (balance) > 1200
14
Aggregate Functions – Having and Where Clause
Find the average balance for each customer who lives in Harrison and has at least three accounts.
Select d.customer_name, avg (balance) from account a , depositor d, customer c
Where d.account_number=a.account_number and
d.customer_name=c.customer_name and
customer_city=‘Harrison’
group by d.customer_name having count(d.account_number) >= 3
15
Find age of the youngest sailor with age 18, for each rating with at least 2 such sailors
SELECT S.rating, MIN (S.age) AS minage
FROM Sailors SWHERE S.age >= 18GROUP BY S.ratingHAVING COUNT (*) > 1
sid sname rating age
22 dustin 7 45.0
29 brutus 1 33.0
31 lubber 8 55.5
32 andy 8 25.5
58 rusty 10 35.0
64 horatio 7 35.0
71 zorba 10 16.0
74 horatio 9 35.0
85 art 3 25.5
95 bob 3 63.5
96 frodo 3 25.5
Answer relation:
Sailors instance:
rating minage 3 25.5 7 35.0 8 25.5
16
Find age of the youngest sailor with age 18, for each rating with at least 2 such sailors.
rating age
7 45.0
1 33.0
8 55.5
8 25.5
10 35.0
7 35.0
10 16.0
9 35.0
3 25.5
3 63.5
3 25.5
rating minage 3 25.5 7 35.0 8 25.5
rating age
1 33.0
3 25.5
3 63.5
3 25.5
7 45.0
7 35.0
8 55.5
8 25.5
9 35.0
10 35.0
17
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.
A common use of subqueries is to perform tests for set membership, set comparisons, and set cardinality.
18
“In” Construct 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 bankselect distinct customer_name
from borrowerwhere customer_name not in (select customer_name
from depositor )
select distinct customer_namefrom borrowerwhere customer_name in (select customer_name
from depositor )
19
Nested Queries
A very powerful feature of SQL: a WHERE clause can itself contain an SQL query! (Actually, so can FROM and HAVING clauses.)
To find sailors who’ve not reserved #103, use NOT IN. To understand semantics of nested queries, think of a
nested loops evaluation: For each Sailors tuple, check the qualification by computing the subquery.
SELECT S.snameFROM Sailors SWHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid=103)
Find names of sailors who’ve reserved boat #103:
20
Example Query Find all customers who have both an account and
a loan at the Perryridge branch
Note: Above query can be written in a much simpler manner. The formulation above is simply to illustrate SQL features.
select distinct customer_namefrom borrower, loanwhere borrower.loan_number = loan.loan_number and
branch_name = 'Perryridge' and (branch_name, customer_name ) in
(select branch_name, customer_name from depositor, account where depositor.account_number =
account.account_number )
21
“Some” Construct 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'
22
“All” Construct 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')
23
Nested Queries with Correlation
EXISTS is another set comparison operator, like IN.
SELECT S.snameFROM Sailors SWHERE EXISTS (SELECT * FROM Reserves R WHERE R.bid=103 AND S.sid=R.sid)
Find names of sailors who’ve reserved boat #103:
24
“Exists” Construct Find all customers who have an account
at all branches located in Brooklyn.select distinct S.customer_name
from depositor as Swhere not exists (
(select branch_namefrom branchwhere branch_city = 'Brooklyn')
except(select R.branch_namefrom depositor as T, account as Rwhere T.account_number = R.account_number and
S.customer_name = T.customer_name ))
25
Division in SQL
Let’s do it the hard way, without EXCEPT:
SELECT S.snameFROM Sailors SWHERE NOT EXISTS ((SELECT B.bid FROM Boats B) EXCEPT (SELECT R.bid FROM Reserves R WHERE R.sid=S.sid))SELECT S.sname
FROM Sailors SWHERE NOT EXISTS (SELECT B.bid FROM Boats B WHERE NOT EXISTS (SELECT R.bid FROM Reserves R WHERE R.bid=B.bid AND R.sid=S.sid))
Sailors S such that ...
there is no boat B without ...
a Reserves tuple showing S reserved B
Find sailors who’ve reserved all boats.
(1)
(2)
26
Absence of Duplicate Tuples
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')
27
Example Query Find all customers who have at least two
accounts at the Perryridge branch. select distinct T.customer_namefrom depositor as Twhere not 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')
Variable from outer level is known as a correlation variable
28
Derived Relationso SQL allows a subquery expression to be used in the from
clauseo Find the average account balance of those branches
where the average account balance is greater than $1200.
select branch_name, avg_balancefrom (select branch_name, avg (balance) from account group by branch_name ) as branch_avg ( branch_name, avg_balance )where avg_balance > 1200
o Note that we do not need to use the having clause, since we compute the temporary (view) relation branch_avg in the from clause, and the attributes of branch_avg can be used directly in the where clause.
29
Find the maximum total balance across all branches.
Select max(tot_balance) From ( select branch_name, sum(balance)
• from account• group by branch_name) as
branch_total(branch_name,tot_balance)
30
Null Values It is possible for tuples to have a null value,
denoted by null, for some of their attributes null signifies an unknown value or that a value
does not exist. The predicate is null can be used to check for
null values. Example: Find all loan number which appear
in the loan relation with null values for amount.
select loan_numberfrom loanwhere amount is null
The result of any arithmetic expression involving null is null Example: 5 + null returns null
However, aggregate functions simply ignore nulls
31
Null Values and Three Valued Logic
Any comparison with null returns unknown Example: 5 < null or null <> null or null = null
Three-valued logic using the truth value unknown: OR: (unknown or true) = true,
(unknown or false) = unknown (unknown or unknown) = unknown
AND: (true and unknown) = unknown, (false and unknown) = false, (unknown and unknown) = unknown
NOT: (not unknown) = unknown “P is unknown” evaluates to true if predicate P
evaluates to unknown Result of where clause predicate is treated as
false if it evaluates to unknown