CSU_07314_Lecture_4[1]
CSU_07314_Lecture_4[1]
Instructor: Siphy, A. S.
email: [email protected]/
[email protected]
SELECT * FROM
Student Student INNER JOIN
ID Name Enrolment USING (ID)
123 John
124 Mary
125 Mark ID Name ID Code
126 Jane 123 John 123 DBS
Enrolment 124 Mary 124 PRG
124 Mary 124 DBS
ID Code 126 Jane 126 PRG
123 DBS
124 PRG
124 DBS
126 PRG
SQL, More Select statements 08-Nov-2016
12 INNER JOIN
SELECT * FROM
Buyer
Buyer INNER JOIN
Name Budget
Smith 100,000
Property ON
Jones 150,000 Price <= Budget
Green 80,000
08-Nov-2016
SQL, More Select statements
15 Outer Join Syntax in Oracle
SELECT <cols>
FROM <t1> <type> OUTER JOIN <t2>
ON <condition>
Example:
SELECT *
FROM Student FULL OUTER JOIN Enrolment
ON Student.ID = Enrolment.ID
SQL, More Select statements
16 Example: inner join
Student Enrolment
ID Name ID Code Mark
123 John 123 DBS 60
124 Mary 124 PRG 70
125 Mark 125 DBS 50
DBS 80 dangles
126 Jane 128
An INNER join will only return rows from each table that have
matching rows in the other.
Join version
Subquery version
Figure 7-6 Graphical depiction of two ways to
33
answer a query with different types of joins
Figure 7-6 Graphical depiction of two ways to
34
answer a query with different types of joins
35 Correlated vs. Noncorrelated
Subqueries
Noncorrelated subqueries:
Do not depend on data from the outer query
Execute once for the entire outer query
Correlated subqueries:
Make use of data from the outer query
Execute once for each row of the outer query
Can use the EXISTS operator
Figure 7-8a Processing a noncorrelated subquery
36
First query
Combine
Second query
Figure 7-9 Combining queries using UNION
41
CREATE
CREATE VIEW
VIEW CustomerPrice
CustomerPrice AS AS
SELECT
SELECT x.customer,
x.customer, y.price
y.price
FROM
FROM Purchase
Purchase x,
x, Product
Product yy
WHERE
WHERE x.product
x.product == y.pname
y.pname
SELECT
SELECT u.customer,
u.customer, v.store
v.store
Query: FROM
FROM CustomerPrice
CustomerPrice u, u, Purchase
Purchase vv
WHERE
WHERE u.customer
u.customer == v.customer
v.customer AND
AND
u.price
u.price >> 100
100
48 Queries Over Views:
Query Modification
Modified query:
SELECT
SELECT u.customer,
u.customer, v.store
v.store
FROM
FROM (SELECT
(SELECT x.customer,
x.customer, y.price
y.price
FROM
FROM Purchase
Purchase x,x, Product
Product yy
WHERE
WHERE x.product
x.product == y.pname)
y.pname) u,
u, Purchase
Purchase v)
v)
WHERE
WHERE u.customer
u.customer == v.customer
v.customer ANDAND
u.price
u.price >> 100
100
49 Queries Over Views:
Query Modification
Modified and rewritten query:
SELECT
SELECT x.customer,
x.customer, v.store
v.store
FROM
FROM Purchase
Purchase x,x, Product
Product y,
y, Purchase
Purchase v,
v,
WHERE
WHERE x.customer
x.customer == v.customer
v.customer AND
AND
y.price
y.price >> 100
100 AND
AND
x.product
x.product == y.pname
y.pname
50 But What About This ?
SELECT
SELECT DISTINCT
DISTINCT u.customer,
u.customer, v.store
v.store
FROM
FROM CustomerPrice
CustomerPrice u, u, Purchase
Purchase vv
WHERE
WHERE u.customer
u.customer == v.customer
v.customer AND
AND
u.price
u.price >> 100
100
??
51 Answer
SELECT
SELECT DISTINCT
DISTINCT u.customer,
u.customer, v.store
v.store
FROM
FROM CustomerPrice
CustomerPrice u, u, Purchase
Purchase vv
WHERE
WHERE u.customer
u.customer == v.customer
v.customer AND
AND
u.price
u.price >> 100
100
SELECT
SELECT DISTINCT
DISTINCT x.customer,
x.customer, v.store
v.store
FROM
FROM Purchase
Purchase x,x, Product
Product y,
y, Purchase
Purchase v,
v,
WHERE
WHERE x.customer
x.customer == v.customer
v.customer AND
AND
y.price
y.price >> 100
100 AND
AND
x.product
x.product == y.pname
y.pname
52 Applications of Virtual Views
Logical data independence:
Vertical data partitioning
Horizontal data partitioning
Security
Table (view) V reveals only what the users are
allowed to know