Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Conceptually, I would start with the <code>Instructor</code> table, since that's what I want to return.</p> <p>I'd join that to the <code>Offering</code> table, so I have all the <code>Offering</code> for each instructor.</p> <p>(But we don't really need anything from the <code>Instructor</code> table at all, since we have InstrId in the <code>Offering</code> table.)</p> <p>Then I'd join that to <code>Enrollment</code>, to basically get all the students that were enrolled in each course offering.</p> <p>Then I'd join that to <code>Student</code>. But again, we don't really need anything from the <code>Student</code> table, we already have <code>stdId</code> in the <code>Enrollment</code> table.</p> <p>The tricky part is that join between <code>Offering</code> and <code>Enrollment</code>. The join predicate will need to be on <code>year</code> and <code>semester</code>, as well as <code>crsId</code>.</p> <p>So, to satisfy the stated requirements, only two tables need to be queried:</p> <pre><code>SELECT e.stdId , o.instrId , e.year FROM Offering o JOIN Enrollment e ON e.crsId = o.crsId AND e.year = o.year AND e.semester = o.semester WHERE e.stdID = 'A01234567' AND e.year &gt;= 2006 AND e.year &lt;= 2008 GROUP BY e.stdId , o.instrId , e.year ORDER BY e.stdId , o.instrId , e.year </code></pre> <hr> <p>There's a peculiarity with the database design. Consider what happens when there are two or more instructors teaching a given course in the same year and semester.</p> <p>But the student was enrolled in only one of those.</p> <p>This query will pick up the instructors of both offerings.</p> <p>It's a problem with the query, but the given database design doesn't give us any way to resolve the issue... because there's no way to tell which instructor the student had.</p> <p>The query satisfies the stated requirements, but the results will be a bit odd, showing a particular student had six different instructors for 'Calc I' in the fall of 2006.</p> <p>Unless there's something that's eluding me here.</p> <p><b>EDIT:</b></p> <p>As Emily Litella would have intoned, "Never mind...". There's a unique constraint on the <code>Offering</code> table. There can be only one offering of a given <code>crsId</code> in a semester. So there's no problem there. (Except that it's peculiar that there would be only one offering of a given course in a given semester.)</p> <p><strike>The <code>GROUP BY</code> is not necessary. The constraints already guarantee that there will be no duplicate rows returned.</strike></p> <p>Actually, I think the GROUP BY is needed, because it's possible that the student was enrolled in two separate courses led by the same instructor. The purpose of the <code>GROUP BY</code> is to eliminate any duplicate rows from the result set (as indicated in the specification.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload