100% found this document useful (1 vote)
144 views3 pages

Assign4 Ans

The document describes 10 SQL queries that are retrieving information from database tables related to course enrollments. The queries are listing course sections by instructor, student rosters by instructor, students by instructor and location, instructors and sections not in New Jersey, instructors with students with last names starting with M, enrollments in a specific section, enrollments by course, total enrollments in a course, course descriptions and capacities with multiple sections, and available seats in all sections.

Uploaded by

roughrunner
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
100% found this document useful (1 vote)
144 views3 pages

Assign4 Ans

The document describes 10 SQL queries that are retrieving information from database tables related to course enrollments. The queries are listing course sections by instructor, student rosters by instructor, students by instructor and location, instructors and sections not in New Jersey, instructors with students with last names starting with M, enrollments in a specific section, enrollments by course, total enrollments in a course, course descriptions and capacities with multiple sections, and available seats in all sections.

Uploaded by

roughrunner
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
You are on page 1/ 3

Assignment #4

Joins

1. List all classes (course_no, section_no) taught by instructor Hanks. (9 rows)

SELECT s.course_no, s.section_no


FROM section s, instructor i
WHERE s.instructor_id = i.instructor_id
AND i.last_name = 'Hanks'

2. Show the student roster (use the format: <last name>, <first name> in a single column) for
each section that Todd Smythe teaches. Identify the section using course number AND section
number. (18 rows)

SELECT s.course_no Course, s.section_no Section,


st.last_name||', '||st.first_name Name
FROM section s, student st, enrollment e, instructor i
WHERE s.section_id = e.section_id
AND e.student_id = st.student_id
AND i.instructor_id = s.instructor_id
AND i.last_name = 'Smythe'
AND i.first_name = 'Todd'

3. List Charles Lowry's students (use the format: <last name>, <first name> in a single column)
that live in New Jersey. Sort the result by last name. (14 rows)

SELECT st.last_name||', '||st.first_name Name


FROM student st, enrollment e, section s, instructor i,
zipcode z
WHERE st.student_id = e.student_id
AND e.section_id = s.section_id
AND i.instructor_id = s.instructor_id
AND i.last_name = 'Lowry'
AND st.zip = z.zip
AND z.state = 'NJ'
ORDER BY st.last_name

4. List the sections taught by instructors who do not live in New Jersey, showing instructor and
state along with course and section numbers. (78 rows)

SELECT i.last_name, z.state, s.course_no, s.section_no


FROM section s, instructor i, zipcode z
WHERE s.instructor_id = i.instructor_id
AND i.zip = z.zip
AND z.state <> 'NJ'
ORDER BY i.last_name, s.course_no, s.section_no

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

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


FROM section s, instructor i, enrollment e, student st
WHERE s.instructor_id = i.instructor_id
AND e.section_id = s.section_id
AND e.student_id = st.student_id
AND st.last_name LIKE 'M%'

Alternate answers:
SELECT i.first_name||' '||i.last_name Name, s.course_no, s.section_no
FROM section s, instructor i, enrollment e, student st
WHERE s.instructor_id = i.instructor_id
AND e.section_id = s.section_id
AND e.student_id = st.student_id
AND SUBSTR(st.last_name,1,1)= 'M'

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


FROM section s, instructor i, enrollment e, student st
WHERE s.instructor_id = i.instructor_id
AND e.section_id = s.section_id
AND e.student_id = st.student_id
AND INSTR(st.last_name,'M')=1

6. Show the number of enrollments for section 1 of Course number 350. Display at least section
AND course numbers. (1 row)

SELECT s.section_no, s.course_no, COUNT(*) Count


FROM section s, enrollment e
WHERE e.section_id = s.section_id
AND s.course_no = 350
AND s.section_no = 1
GROUP BY s.course_no, s.section_no

7. Show the number of enrollments for all sections of course number 122. (5 rows)

SELECT s.section_no, s.course_no, COUNT (*) Count


FROM section s, enrollment e
WHERE e.section_id = s.section_id
AND s.course_no = 122
GROUP BY s.course_no, s.section_no

8. Show the total enrollment for course 122 in a column named TOTAL ENROLLED. (1 row)

SELECT COUNT(*) "TOTAL ENROLLED"


FROM section s, enrollment e
WHERE e.section_id = s.section_id
AND s.course_no = 122

9. Display course description, total capacity and number of sections in each course, where there
is more than 1 section. (18 rows)

SELECT description, SUM(s.capacity), COUNT(*)


FROM course c, section s
WHERE c.course_no = s.course_no
GROUP by description
HAVING COUNT(*) > 1

10. Create a list of all sections that indicates how many places are available in each section (i.e.
capacity - # of students enrolled). Use decode to display a message of "Filled" if there are no
more places; " <#> places available" (as in "10 places available) if capacity has not yet been
reached but some are enrolled; and "None enrolled" if no one has enrolled. Use TO_CHAR
to convert numbers to strings when needed. Use an outer join to include all the sections. (78
rows)
COURSE_NO SECTION_NO Places Available
---------- ---------- --------------------------
25 1 20 places available
240 2 14 places available
134 3 24 places available
142 2 12 places available
145 3 None enrolled
...

SELECT s.course_no, s.section_no,


DECODE(s.capacity - COUNT(e.section_id),
0, 'Filled',
s.capacity,
'None enrolled',
TO_CHAR(s.capacity - COUNT(e.section_id))
||' places available') "Places Available"
FROM section s, enrollment e
WHERE s.section_id = e.section_id (+)
GROUP BY s.course_no, s.section_no, s.capacity

You might also like