0% found this document useful (0 votes)
2 views

CSU07314 Lecture 7 Working With SQL VIEW

Uploaded by

abasichay1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CSU07314 Lecture 7 Working With SQL VIEW

Uploaded by

abasichay1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

CSU/ITU07314:

DBMS

SQL VIEW
2 Logistics

Instructor: Siphy, A. S.
email: [email protected]/
[email protected]

Office: Block D, 020


Consultation Time
BY
Appointment

Intro to SQL, 04/11/2022


3 OVERVIEW

 Define View
 Key features of SQL View
 Key benefits of SQL View
 Working with SQL VIEW

Intro to SQL,
Define SQL VIEW

 An SQL View is a virtual table in a database


that provides a way to simplify complex
queries by encapsulating them in a reusable,
named structure.
 It does not store data itself but fetches it
dynamically from underlying tables when queried.
Define SQL VIEW..
Views are relations, except that they are not physically stored.

They are used mostly in order to simplify complex queries and


to define conceptually different views of the database to
different classes of users.

Employee(ssn, name, department, project, salary)


CREATE
CREATEVIEW
VIEW Developers
DevelopersASAS
SELECT
SELECTname,
name,project
project
FROM
FROM Employee
Employee
WHERE
WHEREdepartment
department==‘Development’
‘Development’
Key Features of SQL View

 Simplification: Abstracts complex queries into a


single command, making it easier to work with.
 Reusability: Can be queried like a regular table
multiple times.
 Data Security: Restricts access to specific columns
or rows, enhancing data protection.
 Maintainability: Centralizes query logic, reducing
redundancy and simplifying updates
Key Benefits of SQL VIEW

 Presenting aggregated or summarized


data for reporting.
 Combining data from multiple tables
(joining).

 Filtering sensitive information for


different user roles.
8 SQL VIEW: Example
Purchase(customer, product, store)
Product(pname, price)

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

CustomerPrice(customer, price) “virtual table”


9
Purchase(customer, product, store)
Product(pname, price)
CustomerPrice(customer, price)

We can later use the view:


SELECT
SELECT 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
SQL View Example…
 Creating a view to show high-performing students, mark >80.
Student(studentid, sname, dob, address)
Grade( student, Course, Grade, Mark)
Course(code, Title, Crdtunit)
 ---------------------------------------------------------------
 CREATE VIEW TopStudents AS
SELECT StudentID, Name, Grade
FROM Student
JOIN Grade ON Student.studentid=Grade.Student
WHERE Mark > 80;
SQL View Example, Class
 CREATE Exercise
SQL VIEW ;
 Q1. List the top 10 students with the highest total marks
across all courses, include id and names of the students
 Q2. List 5 courses that have the highest number of
students enrolled. Include code and title
 Q3. What is the average mark scored by students in each
course. Include Code and title.
 Q4. list students who scored above 80 in the Database
Management System course
 Q5. List students who failed any course with pass mark
below 40. include the id, name, title and respective marks.
SQL View Example, Class Exercise

 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

You might also like