Instructor: Dr. Mohsin Dar
Position: Assistant Professor, Cloud & Software Operations Cluster (SOCS)
Institution: UPES
Course: Database Systems - MTech First Semester
A view is a virtual table based on the result-set of an SQL query. It contains rows and columns just like a real table, but the fields in a view are fields from one or more real tables in the database.
In this lab, we will create four different views that demonstrate:
Create a view that displays the department name, manager name, and manager salary for every department.
CREATE VIEW DEPT_MANAGER_INFO AS SELECT D.Dname AS Department_Name, CONCAT(E.Fname, ' ', E.Minit, ' ', E.Lname) AS Manager_Name, E.Salary AS Manager_Salary FROM DEPARTMENT D INNER JOIN EMPLOYEE E ON D.Mgr_ssn = E.Ssn;
SELECT * FROM DEPT_MANAGER_INFO;
| Department_Name | Manager_Name | Manager_Salary |
|---|---|---|
| Research | Franklin T Wong | 40000 |
| Administration | Jennifer S Wallace | 43000 |
| Headquarters | James E Borg | 55000 |
Create a view that displays the employee name, supervisor name, and employee salary for each employee who works in the 'Research' department.
CREATE VIEW RESEARCH_DEPT_EMP AS SELECT CONCAT(E.Fname, ' ', E.Minit, ' ', E.Lname) AS Employee_Name, CONCAT(S.Fname, ' ', S.Minit, ' ', S.Lname) AS Supervisor_Name, E.Salary AS Employee_Salary FROM EMPLOYEE E LEFT JOIN EMPLOYEE S ON E.Super_ssn = S.Ssn INNER JOIN DEPARTMENT D ON E.Dno = D.Dnumber WHERE D.Dname = 'Research';
The EMPLOYEE table is joined with itself using table aliases (E for employee, S for supervisor):
The WHERE clause filters for only Research department employees:
CREATE VIEW RESEARCH_DEPT_EMP_V2 AS SELECT CONCAT(E.Fname, ' ', E.Minit, ' ', E.Lname) AS Employee_Name, CONCAT(S.Fname, ' ', S.Minit, ' ', S.Lname) AS Supervisor_Name, E.Salary AS Employee_Salary FROM EMPLOYEE E LEFT JOIN EMPLOYEE S ON E.Super_ssn = S.Ssn WHERE E.Dno = (SELECT Dnumber FROM DEPARTMENT WHERE Dname = 'Research');
| Employee_Name | Supervisor_Name | Employee_Salary |
|---|---|---|
| John B Smith | Franklin T Wong | 30000 |
| Franklin T Wong | James E Borg | 40000 |
| Ramesh K Narayan | Franklin T Wong | 38000 |
| Joyce A English | Franklin T Wong | 25000 |
Create a view showing project name, controlling department name, number of employees, and total hours worked per week on the project for each project.
CREATE VIEW PROJECT_STATS AS SELECT P.Pname AS Project_Name, D.Dname AS Controlling_Department, COUNT(W.Essn) AS Number_Of_Employees, SUM(W.Hours) AS Total_Hours_Per_Week FROM PROJECT P INNER JOIN DEPARTMENT D ON P.Dnum = D.Dnumber LEFT JOIN WORKS_ON W ON P.Pnumber = W.Pno GROUP BY P.Pnumber, P.Pname, D.Dname;
Using LEFT JOIN ensures that projects with no employees assigned are still displayed:
Groups results by project to allow aggregate functions:
CREATE VIEW PROJECT_STATS_ENHANCED AS SELECT P.Pname AS Project_Name, D.Dname AS Controlling_Department, COUNT(W.Essn) AS Number_Of_Employees, COALESCE(SUM(W.Hours), 0) AS Total_Hours_Per_Week FROM PROJECT P INNER JOIN DEPARTMENT D ON P.Dnum = D.Dnumber LEFT JOIN WORKS_ON W ON P.Pnumber = W.Pno GROUP BY P.Pnumber, P.Pname, D.Dname;
| Project_Name | Controlling_Department | Number_Of_Employees | Total_Hours_Per_Week |
|---|---|---|---|
| ProductX | Research | 3 | 52.5 |
| ProductY | Research | 2 | 37.5 |
| ProductZ | Research | 2 | 50.0 |
| Computerization | Headquarters | 1 | 10.0 |
| Reorganization | Headquarters | 1 | 10.0 |
Create a view showing project name, controlling department name, number of employees, and total hours worked per week on the project for projects with more than one employee working on it.
This view adds a HAVING clause to filter only projects with multiple employees (> 1 employee).
CREATE VIEW MULTI_EMP_PROJECT_STATS AS SELECT P.Pname AS Project_Name, D.Dname AS Controlling_Department, COUNT(W.Essn) AS Number_Of_Employees, SUM(W.Hours) AS Total_Hours_Per_Week FROM PROJECT P INNER JOIN DEPARTMENT D ON P.Dnum = D.Dnumber INNER JOIN WORKS_ON W ON P.Pnumber = W.Pno GROUP BY P.Pnumber, P.Pname, D.Dname HAVING COUNT(W.Essn) > 1;
| Aspect | WHERE Clause | HAVING Clause |
|---|---|---|
| When Applied | Before grouping | After grouping |
| Filters | Individual rows | Grouped results |
| Can Use Aggregates | No | Yes |
| Example | WHERE Salary > 30000 | HAVING COUNT(*) > 1 |
In this view, we use INNER JOIN for WORKS_ON because:
-- View 3: All Projects (uses LEFT JOIN) LEFT JOIN WORKS_ON W ON P.Pnumber = W.Pno -- No HAVING clause -- View 4: Multi-Employee Projects (uses INNER JOIN) INNER JOIN WORKS_ON W ON P.Pnumber = W.Pno HAVING COUNT(W.Essn) > 1
| Project_Name | Controlling_Department | Number_Of_Employees | Total_Hours_Per_Week |
|---|---|---|---|
| ProductX | Research | 3 | 52.5 |
| ProductY | Research | 2 | 37.5 |
| ProductZ | Research | 2 | 50.0 |
Projects like "Computerization" and "Reorganization" (which have only 1 employee each) are excluded from this view, whereas they appeared in View 3.
-- View 1: Department Manager Information CREATE VIEW DEPT_MANAGER_INFO AS SELECT D.Dname AS Department_Name, CONCAT(E.Fname, ' ', E.Minit, ' ', E.Lname) AS Manager_Name, E.Salary AS Manager_Salary FROM DEPARTMENT D INNER JOIN EMPLOYEE E ON D.Mgr_ssn = E.Ssn; -- View 2: Research Department Employees CREATE VIEW RESEARCH_DEPT_EMP AS SELECT CONCAT(E.Fname, ' ', E.Minit, ' ', E.Lname) AS Employee_Name, CONCAT(S.Fname, ' ', S.Minit, ' ', S.Lname) AS Supervisor_Name, E.Salary AS Employee_Salary FROM EMPLOYEE E LEFT JOIN EMPLOYEE S ON E.Super_ssn = S.Ssn INNER JOIN DEPARTMENT D ON E.Dno = D.Dnumber WHERE D.Dname = 'Research'; -- View 3: All Project Statistics CREATE VIEW PROJECT_STATS AS SELECT P.Pname AS Project_Name, D.Dname AS Controlling_Department, COUNT(W.Essn) AS Number_Of_Employees, SUM(W.Hours) AS Total_Hours_Per_Week FROM PROJECT P INNER JOIN DEPARTMENT D ON P.Dnum = D.Dnumber LEFT JOIN WORKS_ON W ON P.Pnumber = W.Pno GROUP BY P.Pnumber, P.Pname, D.Dname; -- View 4: Multi-Employee Project Statistics CREATE VIEW MULTI_EMP_PROJECT_STATS AS SELECT P.Pname AS Project_Name, D.Dname AS Controlling_Department, COUNT(W.Essn) AS Number_Of_Employees, SUM(W.Hours) AS Total_Hours_Per_Week FROM PROJECT P INNER JOIN DEPARTMENT D ON P.Dnum = D.Dnumber INNER JOIN WORKS_ON W ON P.Pnumber = W.Pno GROUP BY P.Pnumber, P.Pname, D.Dname HAVING COUNT(W.Essn) > 1;
| Concept | View(s) | Description |
|---|---|---|
| INNER JOIN | 1, 2, 4 | Retrieves matching rows from both tables |
| LEFT JOIN | 2, 3 | Includes all rows from left table, even without matches |
| Self-Join | 2 | Joining a table with itself (for hierarchical data) |
| GROUP BY | 3, 4 | Groups rows for aggregate calculations |
| HAVING | 4 | Filters grouped results using aggregate conditions |
| Aggregate Functions | 3, 4 | COUNT(), SUM() for calculations |
| String Functions | 1, 2 | CONCAT() for name formatting |
-- Show all views in database SHOW FULL TABLES WHERE Table_type = 'VIEW'; -- Describe view structure DESCRIBE DEPT_MANAGER_INFO; -- Show view definition SHOW CREATE VIEW DEPT_MANAGER_INFO; -- Drop a view DROP VIEW IF EXISTS DEPT_MANAGER_INFO; -- Modify an existing view CREATE OR REPLACE VIEW DEPT_MANAGER_INFO AS SELECT /* new definition */;
What is the main difference between View 3 and View 4?
Why do we use LEFT JOIN when joining EMPLOYEE with itself in View 2?
Which clause is used to filter grouped results in SQL?
What does COUNT(W.Essn) do in the project statistics views?
What is a view in SQL?
Create a view that shows employee names and their total hours worked across all projects.
Modify View 1 to also include the manager's start date and the number of employees in each department.
Create a view that shows department name, average employee salary, and number of projects controlled by that department.
Create a view showing employees who work more total hours than the company average.
1. Materialized Views - When and how to use them - Refresh strategies 2. Updatable Views - Conditions for updating data through views - WITH CHECK OPTION 3. Indexed Views - Creating indexes on views - Performance benefits 4. View Security - GRANT permissions on views - Role-based access control 5. View Dependencies - Managing dependencies - Impact of table schema changes
For Questions or Clarifications:
Dr. Mohsin Dar
Assistant Professor, SOCS | UPES
Office Hours: [To be announced]