1.查询每个年级的总学时数,按照升序排列 (SUN 是求总和)
select gradeID as 年级编号,SUM(classhour)as 总学时数
from Subject
group by gradeID(分组)
order by 总学时数 asc
2.查询每个参加考试的学员平均分
select studenttno as 学号,AVG(studentresult)as 平均分
from result
group by studentno
3-查询每门课程的平均分,并按照降序排列 (AVG是求平均分)
select subjectid as 课程编号,AVG(studentresult) as 平均分 from Result group by SubjectId (分组) order by 平均分 desc (降序)
4.查询每个学生参加所有考试的总分,并按照降序排列
select studentno as 学号,SUM(studentresult)as 总分 from Result group by Student order by 总分 desc (降序)
5.查询每年级学时数超过的课程数(COUNT 的意思是统计行数 where是条件判断语句)
select gradeeid as 年级编号,COUNT(subjectid) as 课程总数 from Subject where ClassHour>=50 group by GradeID
6.查询每年级学生的平均年龄 (DATEDIFF指2个日期之间的间隔 GETDATE()是获取当前日期 于是当前的平均年纪就出来了)
select gradeid as 年级,AVG(DATEDIFF(YY,birthday,getdate())) as 平均年纪 from student group by Gradeid
7.查询每个年级包含北京的学生总数(%代表一段字里面包含北京2字就算北京地区)
select gradeID as 年级,COUNT(sudentno)as 总人数 from student where Address like ‘%北京%‘ group by GradeId
8.查询参加年级考试的学生中,平均分及格的学生记录,按照降序排列(having的用法是对分组进行筛选)
select studentno as 学号 ,avg(StudentResult)as 平均分 from Result group by StudentNo having avg(Student Result)>60 order by 平均分 desc
9.-查询考试时间为-9-9课程的及格平均分 课程编号 所有课程平均分
select subjectid as 课程编号,avg(studentresult)as所有课程平均分 select from Result where ExamDate>=‘2009-9-9‘ and ExamDate<=‘2009-9-10‘ group by subjectid having avg(stubentResult)>60
10.统计至少有一次不及格的学生学号和次数。
select studentno as 学号,count(studentresult)as 次数 from Result where StudentResult<60 group by studentno
如果语句中有group by关键字,那么select后只能跟group by后出现的列,或者是聚合函数
--max() min() count(),sum(),avg()
时间: 2024-11-09 08:40:56