不确定我是否正确执行 sql 查询

Not sure im doing sql queries correctly

department(dept_name, building, budget)

course(course_id, title, dept_name, credits)

instructor(ID, name, dept_name, salary)

section(course_id, sec_id, semester, year, building, room_number, time_slot_id)

teaches(ID, course_id, sec_id, semester, year)

student(ID, name, dept_name, tot_cred)

takes(ID, course_id, sec_id, semester, year, grade)
  1. 查找超过 50 名学生选修的每门课程的课程名称、学期和年份

    select course.title , takes.semester , takes.year
    from course
    natural join takes
    where course.course_id = takes.course_id
    having count(distinct ID) > 50
    
  2. 查找包含多个部分的每门课程的标题

    select title 
    from course
    natural join section 
    where course.course_id = section.course_id
    having count(distinct sec_id) > 1
    
  3. 查找在 Comp 中教授超过 5 门课程的所有教师的 ID。科学。部门

    select ID
    from instructor
    natural join course
    where course.dept_name = instructor. dept_name
    having count(credits)>5
    

也应该是学分或者course_id

  1. 查找所有未教授生物系提供的任何模块的讲师

这个我都无从下手了

您的所有查询都缺少 GROUP BY 个子句。

如果您使用 NATURAL JOIN,则不需要 WHERE 子句来关联 table——NATURAL JOIN 会自动完成。

select course.title , takes.semester , takes.year
from course
natural join takes
GROUP BY course.title, takes.semester, takes.year    
having count(distinct ID) > 50

select title 
from course
natural join section 
GROUP BY title
having count(distinct sec_id) > 1

select ID
from instructor
natural join course
GROUP BY ID
having count(credits)>5

您上次的查询还有其他几个问题。您没有使用 teaches table 将讲师链接到课程,也没有检查课程是否在 Comp 中。科学。部门。

SELECT i.id
FROM instructor AS i
JOIN teaches AS t ON i.id = t.sec_id
JOIN course AS c ON t.course_id = c.id
WHERE c.dept_name = "Comp. Sci."
HAVING COUNT(*) > 5
select count(t.id), c.title , t.semester , t.year
from course c
left join takes t on t.cource_id=c.cource_id
group by 2,3,4
having count(t.id) > 50

select c.title, count(s.sec_id) 
from course c
left join section  s on c.course_id = s.course_id
group by 1
having count(s.sec_id) > 1

select i.ID, count(c.id)
from instructor i
left join course c on c.dept_name = i. dept_name
group by 1
having count(c.id)>5

select i.ID
from instructor i
left join department d on d.dept_name=i.dept_name
where d.dept_name<>'Biology'