Académique Documents
Professionnel Documents
Culture Documents
Todays Class
Relational
SQL
Model
Answer relation:
Sailors instance:
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.
What Is a Subquery?
Main
Query
SELECT . . .
FROM . . .
WHERE . . (SELECT
.
...
FROM . . .
WHERE . . .)
Subquery
Subqueries
SELECT
FROM
WHERE
select_list
table
expr operator (SELECT select_list
FROM table);
Using a Subquery
SQL> SELECT ename
2 FROM
emp
2975
3 WHERE sal >
4
(SELECT sal
5
FROM
emp
6
WHERE empno = 7566);
ENAME
ENAME
------------------KING
KING
FORD
FORD
SCOTT
SCOTT
Correlated Subqueries
EXECUTE
inner query using candidate row value
USE
value(s) from inner query to qualify
candidate row
Correlated Subqueries
SELECT outer1, outer2, ...
FROM
table1 alias1
WHERE outer1 operator
(SELECT inner1
FROM
table2 alias2
WHERE
alias1.outer2 =
alias2.inner1);
2
3
4
5
FROM
WHERE
is processed the
emp outer
inner query is
sal > (SELECT AVG(sal)
evaluated.
FROM
emp inner
WHERE outer.deptno = inner.deptno);
EMPNO
SAL
DEPTNO
EMPNO
SAL
DEPTNO
--------------- ----------------- ----------------7839
5000
10
7839
5000
10
7698
2850
30
7698
2850
30
7566
2975
20
7566
2975
20
...
...
66 rows
rows selected.
selected.
In Construct
from borrower
where customer_name not in (select customer_name
from depositor )
Nested Queries
Example Query
manner. The
formulation above is simply to illustrate SQL
features.
Some Construct
All Construct
select branch_name
from branch
where assets > all
(select assets
from branch
where branch_city = 'Brooklyn')
S.sid=R.sid)
Exists Construct
(1)
SELECT S.sname
FROM Sailors S
Division in SQL
WHERE NOT EXISTS
((SELECT B.bid
FROM Boats B)
ind sailors whove reserved all boats.
EXCEPT
Lets do it the hard way,
(SELECT R.bid
without EXCEPT:
FROM Reserves R
WHERE R.sid=S.sid
ELECT
(2) S.sname
ROM Sailors S
HERE NOT EXISTS (SELECT B.bid
FROM Boats B
Sailors S such that ... WHERE NOT EXISTS (SELECT R.bid
FROM Reserves R
WHERE R.bid=B.
there is no boat B without ...
AND R.sid=S
a Reserves tuple showing S reserved B
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')
Example Query
correlation variable
Derived Relations
o
o
Select max(tot_balance)
From ( select branch_name, sum(balance)
from account
group by branch_name) as
branch_total(branch_name,tot_balance)
Null Values
or
null = null
Joined Relations
Relation loan
Relation borrower
Joined
Relations
Examples
Joined
Relations
Examples
Solution:
loan full outer join borrower using (loan_number)
View Definition
A relation that is not of the conceptual model
but is made visible to a user as a virtual
relation is called a view.
A view is defined using the create view
statement which has the form
create view v as < query expression >
Example
Queries
Uses of Views
Processing of Views
View Expansion
A way to define the meaning of views defined in terms
of other views.
Let view v be defined by an expression e that may
itself contain uses of view relations.
View expansion of an expression repeats the following
replacement step:
repeat
Find any view relation v in e
Replace the view relation v by the expression defining
vi
until no more view relations are present in e
As long as the view definitions are not recursive, this
loop will terminate
With
Clause