Еще одна альтернатива. Примечание:
1 - Я не верю, что какие-либо решения, представленные до сих пор, являются переносимыми между диалектами SQL. Если бы было разрешено подсчет 4-го столбца (*), я считаю, что следующее решение может быть достаточно переносимым, поскольку оно в основном использует «стандартный SQL». Однако я тестировал только на SQLite3.
2- Я намеренно придерживаюсь вашей спецификации, воспроизводя опечатку для таблицы couses. [Я предполагаю, что это были курсы вместо курсов]
select
c.id, c.courseName, i.instructor as instructor1, null as instructor2
from
couses c, instructors i
where
c.id = i.courseId
group by
courseId having count(*) = 1
union
select /* Case 2: Two instructors */
c.id, c.courseName, i1.instructor as instructor1, i2.instructor as instructor2
from
couses c, instructors i1, instructors i2
where
c.id = i1.courseId and c.id = i2.courseId and i1.id != i2.id and i1.id < i2.id
group by
c.id having count(*) = 1
union
select /* Case 3: Three or more instructors */
c.id, c.courseName, "Commitee" as instructor1, null as instructor2
from
couses c, instructors i1, instructors i2, instructors i3
where
c.id = i1.courseId and c.id = i2.courseId and c.id = i3.courseId and
i1.id != i2.id and i1.id != i3.id and i2.id != i3.id and i1.id < i2.id
and i2.id < i3.id