当前位置:Gxlcms > 数据库问题 > 数据库的有关知识==>>我们的血泪史之经典练习(1-2)

数据库的有关知识==>>我们的血泪史之经典练习(1-2)

时间:2021-07-01 10:21:17 帮助过:3人阅读

抒情一下,想在好困,真的,虽然我在这温暖的教室,身边有知心的盆友, ,很高兴还能是学生的一员,我们还年轻,我们也不会想的太多,高高兴兴上学,快快乐乐回家,每天吃的饱饱,听着老湿讲课,和童鞋打闹....还是幸福的,不是吗?好像有点精神了...

学习:

给大家说一下这些有关关键字的使用顺序和内部运行顺序(切记哟)

下面以如何查询每门课程的平均分为例:

使用顺序                                                                           内部运行顺序

select SubjectId as 课程编号,AVG(studentresult) as 平均分    --04.投影结果
from Result                                                                     --01.定位到表
where SubjectId<3                                                          --02.分组前第一道过滤
group by SubjectId                                                          --03.分组
having COUNT(studentno)>3                                            --05.分组后第二道过滤
order by 课程编号 desc                                                     --06.最后排序

 

 

**************经典练习1*******************


--01.查询每个年级的总学时数,并按照升序排列

select GradeId as 年级编号,SUM(ClassHour) as 总学时数
from Subject
group by GradeId
order by sum(ClassHour)

--02.查询每个参加考试的学员平均分

select studentno as 学生编号,AVG(studentresult) as 平局分
from Result
group by StudentNo

--03.查询每门课程的平均分,并按照降序排列

select subjectid as 学科编号,AVG(studentresult)as 平均分
from Result
group by SubjectId
order by 平均分 desc  --‘平均分‘也可以为‘AVG(studentresult)‘哟!

 --04.查询每个学生参加所有考试的总分,并按照降序排列

select studentno as 学生编号,SUM(studentresult) as 总分
from Result
group by StudentNo
order by 总分 desc   --‘ 总分‘也可以为‘SUM(studentresult)‘哟!

--05.每个年级 男女生总人数(gradeid,gender,人数)

select gradeid as 年级编号,gender as 性别,COUNT(1) as 人数
from student
group by GradeId,Gender
order by GradeId,Gender

--06.每个年级的总人数,满足总人数必须大于等于3

select gradeid as 年级编号,COUNT(1) as 总人数
from student
group by GradeId
having COUNT(1)>=3
select * from Student

 *****************经典练习2****************

--01.查询每年级学时数超过50 的课程数 S1

select gradeid as年级编号,COUNT(subjectid) as 课程数
from Subject
where  ClassHour>50
group by GradeId

--02.查询每年级学生的平均年龄:

select gradeid as 年级编号,AVG(DATEDIFF(yy,birthday,GETDATE())) as 平均年龄
from student
group by gradeid

--03.查询每个年级包含北京的学生总数 (like ‘%北京%‘)

select gradeid as 年级编号,COUNT(1) as 总人数
from student
where address like %北京%
group by Gradeid

 --04.查询参加考试的学生中,平均分及格的学生记录(学号,平均分),按照降序排列

select studentno as 学生编号,AVG(StudentResult) as 平均分
from Result
group by StudentNo
having AVG(StudentResult)>=60
order by 平均分 desc  --‘平均分‘也可改为‘AVG(StudentResult)‘哟!

--05.查询考试时间为2015-12-20课程的及格平均分 (课程编号    所有课程平均分)

select subjectid as 学科编号,AVG(studentresult) as 平均分
from Result
where ExamDate>=2015-11-20 and  ExamDate<2015-11-20
group by SubjectId
having AVG(StudentResult)>=60

--06.统计至少有一次不及格的学生学号和次数。

select studentno as 学生编号,COUNT(1) as  次数
from Result
where StudentResult<60
group by StudentNo

 

希望我的努力可以帮助到你..,应该会吧!

就这么多吧!回见!大神赐教!

 

数据库的有关知识==>>我们的血泪史之经典练习(1-2)

标签:

人气教程排行