0% found this document useful (0 votes)
97 views3 pages

Intermediate SQL Set Operations Lab

This document discusses intermediate SQL concepts like set operations using UNION, INTERSECT, and MINUS; creating views using the CREATE VIEW statement; and provides examples of intermediate level SQL queries to implement on a university database including using set operations, nested subqueries, views, and additional exercises. Lab objectives are to understand set operations and intermediate level queries.

Uploaded by

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

Intermediate SQL Set Operations Lab

This document discusses intermediate SQL concepts like set operations using UNION, INTERSECT, and MINUS; creating views using the CREATE VIEW statement; and provides examples of intermediate level SQL queries to implement on a university database including using set operations, nested subqueries, views, and additional exercises. Lab objectives are to understand set operations and intermediate level queries.

Uploaded by

Bhavik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

LAB NO.

: 3 Date:
INTERMEDIATE SQL

Objectives:

In this lab, student will be able to:


 Understand the set operations and intermediate level queries.

SET Operations in SQL:


Multiple queries using the set operators UNION, UNION ALL, INTERSECT, and MINUS.
All set operators have equal precedence. If a SQL statement contains multiple set operators,
then Oracle Database evaluates them from the left to right unless parentheses explicitly specify
another order.

The corresponding expressions in the select lists of the component queries of a compound
query must match in number and must be in the same data type group (such as numeric or
character).

The UNION operator returns only distinct rows that appear in either result.
SELECT product_id FROM order_items
UNION
SELECT product_id FROM inventories;

The following statement combines the results with the INTERSECT operator, which returns
only those rows returned by both queries:
SELECT product_id FROM inventories
INTERSECT
SELECT product_id FROM order_items;

The following statement combines results with the MINUS operator, which returns only
unique rows returned by the first query but not by the second:
SELECT product_id FROM inventories
MINUS
SELECT product_id FROM order_items;

CREATE VIEW Statement


In SQL, a view is a virtual table based on the result-set of an SQL statement. A view contains rows and
columns, just like a real table. The fields in a view are fields from one or more real tables in the database.
SQL CREATE VIEW Syntax
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition;

LAB EXERCISE:
Implement the following Queries on UNIVERSITY Database:

Set Operations
UNION (Use union all to retain duplicates):
1. Find courses that ran in Fall 2009 or in Spring 2010
INTERSECT (Use intersect all to retain duplicates):
2. Find courses that ran in Fall 2009 and in spring 2010
MINUS:
3. Find courses that ran in Fall 2009 but not in Spring 2010
Null values
4. Find the name of the course for which none of the students registered.

Nested Subqueries
Set Membership (in / not in):
5. Find courses offered in Fall 2009 and in Spring 2010.
6. Find the total number of students who have taken course taught by the instructor with ID
10101.
7. Find courses offered in Fall 2009 but not in Spring 2010.
8. Find the names of all students whose name is same as the instructor’s name.
Set Comparison (>=some/all)
1. Find names of instructors with salary greater than that of some (at least one) instructor
in the Biology department.
10. Find the names of all instructors whose salary is greater than the salary of all instructors in
the Biology department.
11. Find the departments that have the highest average salary.
12. Find the names of those departments whose budget is lesser than the average salary of all
instructors.
Test for Empty Relations (exists/ not exists)
13. Find all courses taught in both the Fall 2009 semester and in the Spring 2010 semester.
14. Find all students who have taken all courses offered in the Biology department.
Test for Absence of Duplicate Tuples
15. Find all courses that were offered at most once in 2009.
16. Find all the students who have opted at least two courses offered by CSE department.
Subqueries in the From Clause
17. Find the average instructors salary of those departments where the average salary is greater
than 42000
Views
18. Create a view all_courses consisting of course sections offered by Physics department in
the Fall 2009, with the building and room number of each section.
19. Select all the courses from all_courses view.
20. Create a view department_total_salary consisting of department name and total salary of
that department.

ADDITIONAL EXERCISE:
1. Find the names of all departments with instructor and remove duplicates.
2. For all instructors who have taught some course, find their names and the course ID of the
courses they taught.
3. Find all the instructors with the courses they taught.
4. List all the students with student name, department name, advisor name and the number of
courses registered.

Common questions

Powered by AI

Set comparison operators like '>= SOME' are advantageous in scenarios where relative comparisons against multiple entries are needed, such as employee salary comparisons across departments. This operator allows you to find instructors with salaries larger than at least one counterpart in a specified group, facilitating targeted evaluations and strategic planning, such as determining competitive salaries within industry standards .

CREATE VIEW in SQL is used to define a virtual table based on the result of a SELECT query, allowing users to streamline complex queries, encapsulate logic, and enhance security by restricting access to sensitive data . It provides an abstraction layer by offering a simplified perspective of the database schema and reducing redundancy in query definitions, which aids in efficient database management and maintenance .

The UNION operator combines results from two queries by returning distinct rows that appear in either result, eliminating duplicates . INTERSECT returns only the rows present in both queries, providing a set of common elements . The MINUS operator yields only the unique rows from the first query that are not present in the second, essentially performing a subtraction of the second query's results from the first . Each operator requires that component queries have matching sets of expressions in number and data type .

Accurately determining the department with the highest average salary using SQL aggregation is critical for informed decision-making regarding budgeting, resource allocation, and competitive analysis. Aggregation queries, like calculating average salary, provide insights into departmental productivity and financial health, helping in strategic planning and highlighting areas needing financial attention or policy adjustments .

Nested subqueries enhance SQL queries by allowing for more complex filtering and condition application when retrieving data. For example, finding courses offered in multiple semesters or calculating aggregate student data within department constraints can benefit from filtering using nested subqueries. Such capabilities expand query functionality beyond simple joins or flat queries, providing deeper insights from hierarchical data structures .

Testing for empty relations in SQL is primarily done using the EXISTS or NOT EXISTS operators in a subquery. This technique is important for determining whether particular data exists within a subset of the database, which is essential for conditional logic and ensuring data integrity. For example, to find courses offered in both Fall 2009 and Spring 2010, EXISTS could be used to check if a course exists in both semesters' data sets .

To find instructor salaries that exceed all biology department instructors' salaries, use the ALL keyword with a subquery: SELECT name FROM instructors WHERE salary > ALL (SELECT salary FROM instructors WHERE department = 'Biology'); This type of query is significant as it evaluates salary data across departments, enabling comparisons that inform budget allocations, salary adjustments, and hiring decisions within the organization .

Views in SQL minimize redundancy by encapsulating complex query logic into single virtual tables that can be reused, which reduces the need to repeatedly write complex joins or aggregations. They improve efficiency by preprocessing and storing the necessary query structure, requiring less runtime computation when accessed. For departmental course records, such as reporting all courses in a academic term, views streamline data retrieval by simplifying and centralizing access procedures .

Retaining duplicates in SQL set operations allows for a more accurate reflection of actual data occurrences, which is useful for analyses that depend on frequency. Using INTERSECT ALL, instead of INTERSECT, retains multiple occurrences of non-distinct rows present in both queries. This ensures comprehensive data understanding where duplication represents specific analytical value, such as repeated course enrollments .

To find courses offered exclusively in Fall 2009, use the MINUS set operation to subtract course offerings in Spring 2010 from those in Fall 2009. The query can be structured as: SELECT course_id FROM courses WHERE semester = 'Fall 2009' MINUS SELECT course_id FROM courses WHERE semester = 'Spring 2010'; This ensures only unique courses from Fall 2009, not repeated in Spring 2010, are listed .

You might also like