九、Sql Server 基础培训《进度9-复杂查询练习》(实际操作)

知识点:

复杂查询1统计全校有多少个男生、有多少个女生?

写法1(分组):

select sex as 性别,count(*) as 人数

from student

group by sex

 

写法2(合并):

select '男' as 性别,count(*) as 人数

from student

where sex='男'

 

union all

select '女' as 性别,count(*) as 人数

from student

where sex='女'

 

写法3(条件):

select '男' as 性别,sum(case sex when '男' then 1 end) as 人数

from student

union all

select '女' as 性别,sum(case sex when '女' then 1 end) as 人数

from student

 

 

=========================================

=========================================

复杂查询2功课全部不及格的同学被留级处理,统计哪些同学符合留级条件?

写法1(嵌套):

select *

from

(

         select t2.name as 姓名,count(*) as 不及格数

         from exam t1

         left join student t2 on t1.StudentID=t2.id

         where t1.score<60

         group by t2.name

) t

where 不及格数=2

 

写法2(子查询):

select t2.name as 姓名,count(*) as 不及格数

from exam t1

left join student t2 on t1.StudentID=t2.id

where not exists (select * from exam where score>=60 and StudentID=t1.StudentID)

group by t2.name

写法3(分组):

select t2.name as 姓名,count(*) as 不及格数

from exam t1

left join student t2 on t1.StudentID=t2.id

where t1.score<60

group by t2.name having count(*)>1

 

 

==================================================

==================================================

学习作业9

问题1用复杂SQL语句,算出全校师生的年龄是多少?

备注:年龄计算用convert(int,datediff(day,出生日期,getdate())/365)来计算,其中datediff 是日期相减的函数,使用公式为(当前日期 - 出生日期) / 365 ,getdate()是取当前系统显示日期,即是你运算的当天日期。

补充:由于教师表没有出生日期,暂以入职日期代替,作为作业训练。

显示格式要求如下:

姓名

出生日期

年龄

张XX

2010-1-1

8

李XX

 

 

 

 

问题2用复杂SQL语句,算出成绩60分以下、60-80分之间、80-95分之间、95分以上各有多少人。

显示格式要求如下:

班级

60以下

60至80

80至95

95以上

XX班

10

45

12

3

YY班

12

36

13

4

 

 

问题3用复杂SQL语句,算出全校每个班级的及格率、良率、优秀率。

备注:60分以上及格、60分至84分为良好、85分至100分为优秀

显示格式要求如下:

班级

及格率(%)

良率(%)

优秀率(%)

XX班

30

10

2

YY班

23

13

2

 

 

 

 

 

 

 

问题4用复杂SQL语句,算出全校每个班级、每个学科的平均分。

备注:平均分精确到2位小数位。

显示格式要求如下:

班级

专业

平均分

XX班

XX专业

56.34

YY班

YY专业

67.10

 

 

问题5用复杂SQL语句,算出每个班级、每个科目要补考的同学有哪些?

显示格式要求如下:

班级

所属班主任

同学姓名

补考专业

补考科目

不及格分数

XX班

张XX

王XX

XX专业

XX科目

54

YY班

李XX

钱XX

YY专业

YY科目

43

 

 

 

 

学习作业9答案参考:

问题1

--全校师生的年龄

select t1.Name as 姓名,t1.Birthday as 出生日期,

convert(int,datediff(day,t1.Birthday,getdate())/365) as 年龄

from

(

         select Name,Birthday

         from Student

         union all

         select Name,BeginDate

         from Teacher

) t1

 

问题2

--成绩60分以下、60-80分之间、80-95分之间、95分以上各有多少人

select t3.Name as 班级,

SUM(case when t1.Score < 60 then 1 else 0 end) as '60以下',

SUM(case when t1.Score >= 60 and t1.Score < 80 then 1 else 0 end) as '60-80',

SUM(case when t1.Score >= 80 and t1.Score <= 95 then 1 else 0 end) as '80-95',

SUM(case when t1.Score > 95 then 1 else 0 end) as '95以上'

from Exam t1

left join Student t2 on t1.StudentID = t2.ID

left join Class t3 on t2.ClassID = t3.ID

group by t3.Name

 

问题3

--用复杂SQL语句,算出全校每个班级的及格率、良率、优秀率。

--备注:60分以上及格、60分至84分为良好、85分至100分为优秀

select

t3.Name as 班级名称,

SUM(case when t1.Score >= 60 then 1.00 else 0.00 end)/(t4.班级人数*2)*100 as '及格率',

SUM(case when t1.Score >= 60 and t1.Score <= 84 then 1.00 else 0.00 end)/(t4.班级人数*2)*100 as '良率',

SUM(case when t1.Score >= 85 and t1.Score <= 100 then 1.00 else 0.00 end)/(t4.班级人数*2)*100 as '优秀率'

from exam t1

left join Student t2 on t1.StudentID=t2.ID

left join class t3 on t2.ClassID=t3.ID

left join

(

select t1.ClassID,count(t1.id) as 班级人数

from student t1

group by t1.ClassID

) t4 on t4.ClassID=t3.ID

group by t3.Name,t4.班级人数

注:带小数的数值,默认为float浮点型数值。

    每个班级考两门,班级及格率怎么计算,取两门及格率相加的平均值。

 

问题4

--用复杂SQL语句,算出全校每个班级、每个学科的平均分。

--备注:平均分精确到2位小数位。

select

t3.name as 班级名称,

t4.Name as 专业名称,

sum(t1.Score)/(t5.班级人数*2) as 平均分

from exam t1

left join Student t2 on t1.StudentID=t2.id

left join class t3 on t2.ClassID=t3.ID

left join Speciality t4 on t3.SpecialID=t4.ID

left join

(

select t1.ClassID,count(t1.id) as 班级人数

from student t1

group by t1.ClassID

) t5 on t5.ClassID=t3.id

group by t3.name,t4.name,t5.班级人数

注:每个班级考两门,统计平均分时,班级人数要乘2

 

问题5

--用复杂SQL语句,算出每个班级、每个科目要补考的同学有哪些?

select

t3.name as 班级名称,

t6.Name+'老师'  as 班主任,

t2.name  as 同学姓名,

t5.name  as 专业名称,

t4.Name  as 科目名称,

t1.Score as 不及格分数

from exam t1

left join Student t2 on t1.StudentID=t2.id

left join class t3 on t2.ClassID=t3.ID

left join [Subject] t4 on t1.SubjectID=t4.ID

left join Speciality t5 on t5.ID=t3.SpecialID

left join Teacher t6 on t3.TeacherID=t6.ID

where t1.Score<60

知识点:

复杂查询1统计全校有多少个男生、有多少个女生?

写法1(分组):

select sex as 性别,count(*) as 人数

from student

group by sex

 

写法2(合并):

select '' as 性别,count(*) as 人数

from student

where sex=''

 

union all

select '' as 性别,count(*) as 人数

from student

where sex=''

 

写法3(条件):

select '' as 性别,sum(case sex when '' then 1 end) as 人数

from student

union all

select '' as 性别,sum(case sex when '' then 1 end) as 人数

from student

 

 

=========================================

=========================================

复杂查询2功课全部不及格的同学被留级处理,统计哪些同学符合留级条件?

写法1(嵌套):

select *

from

(

         select t2.name as 姓名,count(*) as 不及格数

         from exam t1

         left join student t2 on t1.StudentID=t2.id

         where t1.score<60

         group by t2.name

) t

where不及格数=2

 

写法2(子查询):

select t2.name as 姓名,count(*) as 不及格数

from exam t1

left join student t2 on t1.StudentID=t2.id

where not exists(select * from exam where score>=60 and StudentID=t1.StudentID)

group by t2.name

写法3(分组):

select t2.name as 姓名,count(*) as 不及格数

from exam t1

left join student t2 on t1.StudentID=t2.id

where t1.score<60

group by t2.name having count(*)>1

 

 

==================================================

==================================================

学习作业9

问题1用复杂SQL语句,算出全校师生的年龄是多少?

备注:年龄计算用convert(int,datediff(day,出生日期,getdate())/365)来计算,其中datediff 是日期相减的函数,使用公式为(当前日期 - 出生日期) / 365 getdate()是取当前系统显示日期,即是你运算的当天日期。

补充:由于教师表没有出生日期,暂以入职日期代替,作为作业训练。

显示格式要求如下:

姓名

出生日期

年龄

XX

2010-1-1

8

XX

 

 

 

 

问题2用复杂SQL语句,算出成绩60分以下、60-80分之间、80-95分之间、95分以上各有多少人。

显示格式要求如下:

班级

60以下

6080

8095

95以上

XX

10

45

12

3

YY

12

36

13

4

 

 

问题3用复杂SQL语句,算出全校每个班级的及格率、良率、优秀率。

备注:60分以上及格、60分至84分为良好、85分至100分为优秀

显示格式要求如下:

班级

及格率(%

良率(%

优秀率(%

XX

30

10

2

YY

23

13

2

 

 

 

 

 

 

 

问题4用复杂SQL语句,算出全校每个班级、每个学科的平均分。

备注:平均分精确到2位小数位。

显示格式要求如下:

班级

专业

平均分

XX

XX专业

56.34

YY

YY专业

67.10

 

 

问题5用复杂SQL语句,算出每个班级、每个科目要补考的同学有哪些?

显示格式要求如下:

班级

所属班主任

同学姓名

补考专业

补考科目

不及格分数

XX

XX

XX

XX专业

XX科目

54

YY

XX

XX

YY专业

YY科目

43

 

猜你喜欢

转载自www.cnblogs.com/star-studio/p/9315823.html