You are on page 1of 5

Solution Manual for Oracle SQL By Example, 4/E 4th Edition Alice Rischert

Solution Manual for Oracle SQL By Example, 4/E 4th


Edition Alice Rischert

To download the complete and accurate content document, go to:


https://testbankbell.com/download/solution-manual-for-oracle-sql-by-example-4-e-4th-
edition-alice-rischert/

Visit TestBankBell.com to get complete for all chapters


Assignment #5
Subqueries
1. Show all the sections whose enrollment is greater than 5. Display course and section
number. Do two versions – one using a join and the other with a correlated subquery. (10
rows)

SELECT s.course_no, s.section_no


FROM section s
WHERE 5 < (SELECT COUNT(*)
FROM enrollment e
WHERE e.section_id = s.section_id)

SELECT course_no, section_no,


COUNT(student_id)
FROM section, enrollment
WHERE section.section_id = enrollment.section_id
GROUP BY course_no, section_no
HAVING COUNT(student_id) > 5

2. Show all students (use the format: <last name>, <first initial> in a single column) who are
enrolled in more than two classes. (7 rows)

SELECT st.last_name||', '||substr(st.first_name,1,1)


FROM student st
WHERE 2 < (SELECT COUNT(*)
FROM enrollment e
WHERE e.student_id = st.student_id)

3. List courses and their description whose prerequisites are taught by Nina Schorin. (13 rows)
SELECT c.course_no, c.description
FROM course c, course pr
WHERE c.prerequisite = pr.course_no
AND pr.course_no IN (SELECT s.course_no
FROM section s, instructor i
WHERE s.instructor_id = i.instructor_id
AND i.last_name = 'Schorin')

4. Show instructors (along with their course numbers and section numbers) teaching class
sections with students whose last name begins with ‘M’. (16 rows)

SELECT i.first_name||' '||i.last_name, s.course_no, s.section_no


FROM section s, instructor i
WHERE s.instructor_id = i.instructor_id
AND s.section_id IN (SELECT section_id
FROM enrollment e, student s
WHERE e.student_id = s.student_id
AND last_name like 'M%')
ORDER BY i.last_name, s.course_no, s.section_no

5. List all sections (course_no, description, section_no) taught by instructors that do not live in
Connecticut. Sort the result by course_no, section_no. (78 rows) Write two versions, and use a
correlated subquery in one.

SELECT s.course_no, s.section_no, c.description


FROM section s, course c, instructor i
WHERE s.course_no = c.course_no
AND i.instructor_id = s.instructor_id
AND i.zip NOT IN (SELECT zip
FROM zipcode
WHERE state ='CT')

SELECT s.course_no, s.section_no, c.description


FROM section s, course c
WHERE s.course_no = c.course_no
AND EXISTS (SELECT null
FROM instructor i
WHERE i.instructor_id = s.instructor_id
AND i.zip NOT IN (SELECT zip
FROM zipcode
WHERE state ='CT'))

6. List all classes (course_no, section_no) taught by Fernand Hanks (Use a correlated subquery)
(9 rows).
SELECT s.course_no, s.section_no
FROM section s
WHERE EXISTS (SELECT null
FROM instructor i
WHERE s.instructor_id = i.instructor_id
AND i.last_name = 'Hanks')

8. List all students (use the format: <last name>, <first initial> in a single column) in sections with
fewer than 5 students enrolled. Do not show any duplicate student names (87 rows).

SELECT last_name||', '||first_name


FROM student
WHERE student_id IN (SELECT student_id
FROM enrollment
WHERE section_id IN (SELECT section_id
FROM enrollment
GROUP BY section_id
HAVING COUNT(*) < 5))

9. List all zip codes that are not assigned to students. Write two versions: using a NOT EXISTS
and using an OUTER JOIN. (82 rows)

SELECT zip
FROM zipcode z
WHERE NOT EXISTS (SELECT null
FROM student s
WHERE s.zip=z.zip)

SELECT z.zip
FROM zipcode z, student s
WHERE z.zip = s.zip (+)
AND s.zip IS NULL

SELECT z.zip
FROM zipcode z LEFT OUTER JOIN student s
ON z.zip = s.zip
WHERE s.zip IS NULL
Solution Manual for Oracle SQL By Example, 4/E 4th Edition Alice Rischert

10. Display the first & last names of students (use the format: <first_name last_name> in a single
column), and the number of classes they are enrolled in, for students who are enrolled in
more than 2 classes. (7 rows)

SELECT first_name||' '||last_name, COUNT(e.student_id)


FROM student s, enrollment e
WHERE s.student_id = e.student_id
GROUP BY first_name, last_name
HAVING COUNT(e.student_id) > 2

SELECT first_name||' '||last_name


FROM student st
WHERE 2 < (SELECT COUNT(*)
FROM enrollment e
WHERE e.student_id=st.student_id)

11. Using a correlated sub-query, display course name, course number and the average capacity
of all sections of each course, rounded to 4 decimal places, where students are enrolled, and the
average capacity is less than 25. Use a column alias of AVERAGE_CAPACITY to represent the
number. (16 rows)

SELECT description, c.course_no, ROUND(AVG(capacity), 4)


AVERAGE_CAPACITY
FROM section s, course c
WHERE c.course_no = s.course_no
AND EXISTS (SELECT null
FROM enrollment e
WHERE e.section_id=s.section_id)
GROUP BY description, c.course_no
HAVING ROUND(AVG(capacity), 4) < 25

Visit TestBankBell.com to get complete for all chapters

You might also like