CSE 3110: Database Systems Laboratory
Dept. of CSE, KUET
Lab 04
Subquery, Set Operations and Views
Page |1
In this part of lab, we will create a database that consists of 4 tables. Below is the schema diagram:
departments teachers courses students
dept_id t_id course_no s_id
dept_name t_name course_name s_name
faculty gender credit cgpa
no_of_students salary d_id dept_id
dept_id
Subquery:
Subquery/Nested query is a query in a query. A subquery is usually added in the WHERE, FROM and/or
SELECT clauses of the sql statement. A Subquery is used when you know how to search for a value using a
SELECT statement, but do not know the exact value.
Run “Tables [Link]” and “Data [Link]” scripts, then run these following statements and study the
outputs.
SELECT t_name, salary, (SELECT AVG(salary) FROM teachers) AS
avg_salary FROM teachers;
--Find the teacher’s name who has maximum salary
SELECT t_name FROM teachers WHERE salary = (SELECT MAX(salary)
FROM teachers);
--Find the average salary of the departments along with the department names
SELECT dept_name, avg_sal
FROM (
SELECT dept_id, AVG(salary) AS avg_sal
FROM teachers
GROUP BY dept_id
) dept_avg, departments d where dept_avg.dept_id = d.dept_id;
--Find the records of teachers whose departments have 120 students
SELECT * FROM teachers WHERE dept_id IN (SELECT dept_id FROM
departments WHERE no_of_students = 120);
Md Mehrab Hossain Opi, Lecturer, Dept. of CSE, KUET
Waliul Islam Sumon, Lecturer, Dept. of CSE, KUET
CSE 3110: Database Systems Laboratory
Dept. of CSE, KUET
Lab 04
Subquery, Set Operations and Views
Page |2
We can also use subqueries to insert, update or delete records.
INSERT INTO teachers(t_id, t_name, dept_id) SELECT s_id, s_name,
dept_id FROM students WHERE cgpa >= 3.80;
Set Operations:
Union:
The UNION operator merges the result sets of two queries. It returns all rows retrieved by either query, while
automatically eliminating duplicates. The result sets of the queries must be union-compatible, i.e., they must
have the same number of columns, with matching data types, in the same order. Column names are taken from
the first query.
SELECT t_id, t_name FROM teachers
UNION
SELECT s_id, s_name FROM students;
We can use “UNION ALL” instead of “UNION” to retain the duplicate records.
Intersect:
The INTERSECT operator returns only the rows that are common to the result sets of two queries. Like UNION,
the queries must be union-compatible, meaning they must have the same number of columns with matching data
types in the same order. Column names are taken from the first query.
SELECT t_id, t_name FROM teachers
INTERSECT
SELECT s_id, s_name FROM students;
Minus:
The MINUS operator returns all rows from the first query that are not present in the second query. Like UNION
and INTERSECT, the queries must be union-compatible: the same number of columns, with matching data
types, in the same order. Column names are taken from the first query.
SELECT t_id, t_name FROM teachers
MINUS
SELECT s_id, s_name FROM students;
Precedence of Set Operators: If multiple set operators are used in a single query, Oracle evaluates the set
operators from left to right. To influence a particular order of evaluation of the set operators, you can use
parentheses (). The query inside the parentheses will be executed first, then the rest.
Md Mehrab Hossain Opi, Lecturer, Dept. of CSE, KUET
Waliul Islam Sumon, Lecturer, Dept. of CSE, KUET
CSE 3110: Database Systems Laboratory
Dept. of CSE, KUET
Lab 04
Subquery, Set Operations and Views
Page |3
Views:
A view is a virtual table that represents the result of a stored query. It does not store data physically; instead, it
dynamically retrieves data from the underlying tables whenever queried.
A view can be simple (from one table) or complex (involving joins, aggregations, or subqueries).
Views can be used to:
• Simplify complex queries.
• Restrict access to specific columns or rows.
• Provide a level of abstraction from underlying table structures.
Changes to the underlying table are reflected in the view automatically.
We can query a view just like a table.
Views improve security and maintainability by hiding table complexity.
Views can also be updatable if they satisfy certain conditions (like selecting from a single table without
aggregates).
Let’s create a view:
CREATE OR REPLACE VIEW dept_teacher_view AS
SELECT t.t_name AS "Teacher’s Name", d.dept_name AS
"Department", [Link] AS Gender
FROM departments d, teachers t
WHERE d.dept_id = t.dept_id;
SELECT * FROM dept_teacher_view;
Now, update the gender of the teacher W (t_id = 1004) to 'Male' and observe that the change is also reflected in
the view.
SELECT * FROM teachers;
UPDATE teachers SET gender = 'Male' WHERE t_name = 'W';
SELECT * FROM teachers;
SELECT * FROM dept_teacher_view;
Let’s create another view:
CREATE OR REPLACE VIEW teachers_view AS
SELECT t_id AS "Teacher’s ID", t_name AS "Teacher’s Name"
FROM teachers;
Md Mehrab Hossain Opi, Lecturer, Dept. of CSE, KUET
Waliul Islam Sumon, Lecturer, Dept. of CSE, KUET
CSE 3110: Database Systems Laboratory
Dept. of CSE, KUET
Lab 04
Subquery, Set Operations and Views
Page |4
Now, execute these statements and try to grasp what’s happening.
SELECT * FROM teachers;
SELECT * FROM teachers_view;
UPDATE teachers_view SET "Teacher's Name" = 'Waliul' WHERE
"Teacher's ID" = 1004;
SELECT * FROM teachers_view;
SELECT * FROM teachers;
SELECT * FROM dept_teacher_view;
THE END
Md Mehrab Hossain Opi, Lecturer, Dept. of CSE, KUET
Waliul Islam Sumon, Lecturer, Dept. of CSE, KUET