Student-course database-initial sql statement(3)

Group by clause:
If the columns of the select selection list have both ordinary columns and aggregate function columns, they must be grouped (that is, the group by clause is added).

How to group?
Answer: All the ordinary columns of the select selection list are used as the grouping columns of the group by clause.

select *
from Student
order by Sdept,Sage desc

/*聚集函数*/
select COUNT(*) 学生总人数
from Student

select COUNT(distinct sno) 选修了课程的学生人数
from SC

select AVG(grade) 选修1号课程的学生的平均成绩
from SC
where Cno='1'

select MAX(grade) 选修1号课程的学生最高分数
from SC
where Cno='1'

select SUM(Ccredit) 学生201215121选修课程的总学分数
from Course,SC
where course.Cno=sc.Cno and Sno='201215121' and Grade>=60

select Cno 课程号,COUNT(*) 相应的选课人数
from SC
group by Cno

select Sno 选修了三门及以上课程的学生学号
from SC
group by Sno
having COUNT(*)>=3

/*求各个课程号,课程名及相应的选课人数*/
select SC.Cno,Cname,COUNT(*) 对应的选课人数
from Course,SC
where Course.Cno=SC.Cno
group by SC.Cno,Cname

select Sno,AVG(grade) 平均成绩
from SC
group by Sno
having AVG(grade)>=80

/*连接查询*/
select *
from Student,SC
where Student.Sno=SC.Sno

select Student.*,Cno,Grade
from Student,SC
where Student.Sno=SC.Sno

select SC.Sno,Sname
from Student,SC
where Student.Sno=SC.Sno and SC.Cno='2' and Grade>=90

select Student.*,Cno,Grade
from Student left outer join SC on (Student.Sno=SC.Sno)

select *
from SC right outer join Course on SC.Cno=Course.Cno

select *
from Course left outer join SC on SC.Cno=Course.Cno

select SC.Sno,Sname,Cname,Grade
from Student,SC,Course
where student.Sno=SC.Sno and SC.Cno=Course.Cno

/*查询每个学生的学号、姓名、选修的课程名及成绩,包括没有选修课程的学生,包括没有被选修的课程*/
select SC.Sno,Sname,Cname,Grade
from (Student full outer join SC on Student.Sno=SC.Sno)
      full outer join Course on SC.Cno=Course.Cno

Essay with Treasure:
Portal

Guess you like

Origin blog.csdn.net/qq_46139801/article/details/115328676