CSU07314 Lecture 7 Working With SQL VIEW
CSU07314 Lecture 7 Working With SQL VIEW
DBMS
SQL VIEW
2 Logistics
Instructor: Siphy, A. S.
email: [email protected]/
[email protected]
Define View
Key features of SQL View
Key benefits of SQL View
Working with SQL VIEW
Intro to SQL,
Define SQL VIEW
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
Q1. List the top 10 students with the highest total marks across all
courses?
Ans;
CREATE VIEW Top10Students AS
SELECT S.studentid, S.sname, SUM(G.Mark) AS TotalMarks
FROM Student S
JOIN Grade G ON S.studentid = G.student
GROUP BY S.studentid, S.sname
ORDER BY TotalMarks DESC
LIMIT 10;
SQL View Example, Class Exercise
Q2. List 5 courses that have the highest number of students enrolled
CREATE VIEW Top5Modules AS
SELECT C.code, C.Title, COUNT(G.student) AS StudentCount
FROM Course C
JOIN Grade G ON C.code = G.Course
GROUP BY C.code, C.Title
ORDER BY StudentCount DESC
LIMIT 5;
14 Types of Views
We discuss
Virtual views: only virtual
Used in databases views in class
Computed only on-demand – slow at runtime
Always up-to-date
Materialized views
Used in data warehouses
Pre-computed offline – fast at runtime
May have stale data
15 Queries Over Views:
Query Modification
CREATE
CREATE VIEW
VIEW CustomerPrice
CustomerPrice AS AS
View: 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
16 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
17 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
18 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
??
19 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
20 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