You are on page 1of 3

RDBMS Unit: Introduction To RDBMS and SQL

Module 9: Union-Intersect-Minus

Exercise 01 – Union/Intersect/Minus
functions against the table
Estimated Completion Time: 45 minutes

Overview
This exercise will require participants to query on the tables in Oracle SQL PLUS
using the SQL language.

Instructions
Querying records in tables should be done in Oracle SQL PLUS.
1. In order to proceed with querying records in the tables, login using your
user name created in Exercise 01 and then proceed to use the SET
operators.

Hints
1. Ensure the following while creating the queries on the table:
 Execute the query in SQL PLUS
 After each successful command save the command in the file
 To save the file - C:\RDBMS\Day3\UnIntMin.sql append

RDBMS 1 52354326.doc
 2007 Accenture. All Rights Reserved.
RDBMS Unit: Introduction To RDBMS and SQL
Module 9: Union-Intersect-Minus

Union/Intersect and Minus

1) Display the first name and last name of both the instructor and student in 1 result set.
Solution:
SELECT FIRST_NAME, LAST_NAME FROM INSTRUCTOR_INFO
UNION
SELECT FIRST_NAME, LAST_NAME FROM STUDENT_INFO

2) Modify the above query to display duplicate names also.


Solution:
SELECT FIRST_NAME, LAST_NAME FROM INSTRUCTOR_INFO
UNION ALL
SELECT FIRST_NAME, LAST_NAME FROM STUDENT_INFO

3) Modify the above query to add a column Name to display text as 'Instructor' or 'Student'
in each row.
Solution:
SELECT FIRST_NAME, LAST_NAME, ‘INSTRCUTOR NAME' FROM
INSTRUCTOR_INFO UNION
SELECT FIRST_NAME, LAST_NAME, 'STUDENT NAME' FROM STUDENT_INFO

4) Display the instructor id not having any section


Solution:
SELECT INSTRUCTOR_ID FROM INSTRUCTOR_INFO
MINUS
SELECT INSTRUCTOR_ID FROM SECTION_INFO

5) Display the course no having section.


Solution:
SELECT COURSE_NO FROM COURSE_INFO
INTERSECT
SELECT COURSE_NO FROM SECTION_INFO

RDBMS 2 52354326.doc
 2007 Accenture. All Rights Reserved.
RDBMS Unit: Introduction To RDBMS and SQL
Module 9: Union-Intersect-Minus

6) Display a list of courses and section having no students enrolled. Add a new column
called as status that displays text as 'No Student Enrolled'
Solution:
SELECT COURSE_NO, SECTION_NO, 'NO ENROLLMENTS ' AS STATUS FROM
SECTION_INFO MINUS
SELECT COURSE_NO, SECTION_NO, ' ‘NO ENROLLMENTS ' FROM
SECTION_INFO
WHERE SECTION_ID IN
(SELECT SECTION_ID FROM SECTION_INFO
INTERSECT
SELECT SECTION_ID FROM ENROLLMENT_INFO)

7) Display all the zip codes that are present in both the instructor and the student tables.
Solution:
SELECT 'INSTRCUTOR ZIP' ZIP, ZIP FROM INSTRUCTOR_INFO
INTERSECT
SELECT 'INSTRCUTOR ZIP' ZIP, ZIP FROM STUDENT_INFO

8) Display the student ids who are enrolled.


Solution:
SELECT STUDENT_ID FROM STUDENT
INTERSECT
SELECT STUDENT_ID FROM ENROLLMENT

RDBMS 3 52354326.doc
 2007 Accenture. All Rights Reserved.