SQL 查询:对于每个有一个或多个GPA低于1.0的专业的系,打印系名及其专业的平均GPA

SQL Query: For each department that has one or more majors with a GPA under 1.0, print the name of the department and the average GPA of its majors

mySQL 的新手。完成一个项目只剩下 2 个查询要写,但我几乎坚持不懈。

表格:

需要查询:

1.对于拥有一个或多个 GPA 低于以下专业的每个部门 1.0,打印系名及其专业平均GPA

2。打印当前正在学习所有土木工程课程的学生的 ID、姓名和 GPA。

下面是数据库中的一些示例数据:

student
sid     sname           sex     age     year    gpa
1       Jacobs, T.      m       29      5       3.60
2       Pierson, E.     m       32      5       3.50
3       Zeene, Ben N.   m       21      5       3.90

dept
dname                   numphds
Chemical Engineering    32
Civil Engineering       88
Computer Sciences       47

prof
pname        dname
Brian, C.    Computer Sciences
Brown, S.    Civil Engineering
Bucket, T.   Sanitary Engineering

course
cno          cname                  dname
302          Intro to Programming   Computer Sciences
310          Thermodynamics         Chemical Engineering
310          Intro to Garbage       Sanitary Engineering

major
dname                   sid
Chemical Engineering    25
Chemical Engineering    26
Chemical Engineering    27

section
dname                   cno     sectno  pname
Chemical Engineering    310     1       Edison, L.
Civil Engineering       365     1       Randolph, B.
Civil Engineering       375     1       Brown, S.

enroll
sid     grade   dname                   cno     sectno
1       3.00    Chemical Engineering    310     1
2       3.00    Computer Sciences       302     1
3       3.50    Civil Engineering       375     1

如有任何帮助,我们将不胜感激。

更新:

这是我对问题 1 的看法:

select d.dname, AVG(s.gpa) from dept d, student s, major m 
                 where s.sid = m.sid and d.dname = m.dname 
                 and s.gpa <1 group by dname;

这有效,但没有给我该系的正确 gpa,只有那个特定学生的 gpa?

问题 2:

   select s.sid, s.sname, s.gpa
      from student s
        inner join enroll e
          on s.sid = e.sid
      where e.dname = 'Civil Engineering'
      group by sid
      having count(distinct cno) = 
        (select count(cno) from course where dname = 'Civil Engineering');

示例 fiddle 此处:http://sqlfiddle.com/#!9/807bc/1

我们加入 student to enroll tables 以获取学生注册的所有课程的列表,我们将其过滤为仅土木工程系的课程,然后我们按学生分组,并计算学生注册的不同课程的数量(因为在现实生活中,随着时间的推移,学生最终可能会多次注册同一门课程),并将其与土木工程系的课程总数进行比较,以及仅包含与最后一个条件匹配的结果行​​。

问题 1:

select d.dname, avg(s.gpa)
  from dept d
    inner join major m
      on d.dname = m.dname
    inner join student s
      on s.sid = m.sid
  group by d.dname
  having min(s.gpa) < 1.0

  select m.dname, avg(s.gpa)
    from major m
      inner join student s
        on s.sid = m.sid
  group by m.dname
  having min(s.gpa) < 1.0

已更新 fiddle 此处:http://sqlfiddle.com/#!9/d12f4/5

答案是以类似的方式构建的。我已经给出了第二个答案,因为对我来说似乎很奇怪部门 table 没有其他 table 部门使用的 department_id 字段,而本能会建议会的。

  1. For each department that has one or more majors with a GPA under 1.0, print the name of the department and the average GPA of its majors.

它的伪代码。如果有效请尝试,否则会出现问题。

SELECT D.DNAME,AVG(S.GPA) FROM DEPT D, STUDENT S,MAJOR M
WHERE D.DNAME = M.DNAME AND M.SID = S.SID
GROUP BY DNAME HAVING MIN(S.GPA) < 1