Você está na página 1de 3

Assignment #4

Joins
1. List all classes (course_no, section_no) taught by instructor Hanks. (9 rows)
SELECT
FROM
WHERE
AND

s.course_no, s.section_no
section s, instructor i
s.instructor_id = i.instructor_id
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
FROM
WHERE
AND
AND
ORDER

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


section s, instructor i, zipcode z
s.instructor_id = i.instructor_id
i.zip = z.zip
z.state <> 'NJ'
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
FROM
WHERE
AND
AND

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


section s, instructor i, enrollment e, student st
s.instructor_id = i.instructor_id
e.section_id = s.section_id
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
FROM
WHERE
AND
AND
AND

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


section s, instructor i, enrollment e, student st
s.instructor_id = i.instructor_id
e.section_id = s.section_id
e.student_id = st.student_id
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
FROM
WHERE
AND
AND
GROUP

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


section s, enrollment e
e.section_id = s.section_id
s.course_no = 350
s.section_no = 1
BY s.course_no, s.section_no

7. Show the number of enrollments for all sections of course number 122. (5 rows)
SELECT
FROM
WHERE
AND
GROUP

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


section s, enrollment e
e.section_id = s.section_id
s.course_no = 122
BY s.course_no, s.section_no

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

COUNT(*) "TOTAL ENROLLED"


section s, enrollment e
e.section_id = s.section_id
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

Você também pode gostar