CSU/ITU07314:
DBMS
SQL VIEW
2 Logistics
Instructor: Siphy, A. S.
email: dullextz@[Link]/
[Link]@[Link]
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 [Link],
[Link], [Link]
[Link]
FROM
FROM Purchase
Purchase x,
x, Product
Product yy
WHERE
WHERE [Link]
[Link] == [Link]
[Link]
CustomerPrice(customer, price) “virtual table”
9
Purchase(customer, product, store)
Product(pname, price)
CustomerPrice(customer, price)
We can later use the view:
SELECT
SELECT [Link],
[Link], [Link]
[Link]
FROM
FROM CustomerPrice
CustomerPrice u, u, Purchase
Purchase vv
WHERE
WHERE [Link]
[Link] == [Link]
[Link] AND
AND
[Link]
[Link] >> 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 [Link]=[Link]
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 [Link], [Link], SUM([Link]) AS TotalMarks
FROM Student S
JOIN Grade G ON [Link] = [Link]
GROUP BY [Link], [Link]
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 [Link], [Link], COUNT([Link]) AS StudentCount
FROM Course C
JOIN Grade G ON [Link] = [Link]
GROUP BY [Link], [Link]
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 [Link],
[Link], [Link]
[Link]
FROM
FROM Purchase
Purchase x,
x, Product
Product yy
WHERE
WHERE [Link]
[Link] == [Link]
[Link]
SELECT
SELECT [Link],
[Link], [Link]
[Link]
Query: FROM
FROM CustomerPrice
CustomerPrice u, u, Purchase
Purchase vv
WHERE
WHERE [Link]
[Link] == [Link]
[Link] AND
AND
[Link]
[Link] >> 100
100
16 Queries Over Views:
Query Modification
Modified query:
SELECT
SELECT [Link],
[Link], [Link]
[Link]
FROM
FROM (SELECT
(SELECT [Link],
[Link], [Link]
[Link]
FROM
FROM Purchase
Purchase x,x, Product
Product yy
WHERE
WHERE [Link]
[Link] == [Link])
[Link]) u,
u, Purchase
Purchase v)
v)
WHERE
WHERE [Link]
[Link] == [Link]
[Link] ANDAND
[Link]
[Link] >> 100
100
17 Queries Over Views:
Query Modification
Modified and rewritten query:
SELECT
SELECT [Link],
[Link], [Link]
[Link]
FROM
FROM Purchase
Purchase x,x, Product
Product y,
y, Purchase
Purchase v,
v,
WHERE
WHERE [Link]
[Link] == [Link]
[Link] AND
AND
[Link]
[Link] >> 100
100 AND
AND
[Link]
[Link] == [Link]
[Link]
18 But What About This ?
SELECT
SELECT DISTINCT
DISTINCT [Link],
[Link], [Link]
[Link]
FROM
FROM CustomerPrice
CustomerPrice u, u, Purchase
Purchase vv
WHERE
WHERE [Link]
[Link] == [Link]
[Link] AND
AND
[Link]
[Link] >> 100
100
??
19 Answer
SELECT
SELECT DISTINCT
DISTINCT [Link],
[Link], [Link]
[Link]
FROM
FROM CustomerPrice
CustomerPrice u, u, Purchase
Purchase vv
WHERE
WHERE [Link]
[Link] == [Link]
[Link] AND
AND
[Link]
[Link] >> 100
100
SELECT
SELECT DISTINCT
DISTINCT [Link],
[Link], [Link]
[Link]
FROM
FROM Purchase
Purchase x,x, Product
Product y,
y, Purchase
Purchase v,
v,
WHERE
WHERE [Link]
[Link] == [Link]
[Link] AND
AND
[Link]
[Link] >> 100
100 AND
AND
[Link]
[Link] == [Link]
[Link]
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