【SQL Server数据库】带函数查询和综合查询(1)

目录

1.统计年龄大于30岁的学生的人数。

2.统计数据结构有多少人80分或以上。

3.查询"0203"课程的最高分的学生的学号。

4.统计各系开设班级的数目(系名称、班级数目),并创建结果表。

5.选修了以"01"开头的课程的学生学号,姓名,选课的课程号。

7.所有成绩都在70分以上的学生姓名。

8."数据库"课程得最高分的学生的学号、姓名和所在系。

9.至少选修了两门课及以上的学生姓名和性别。


1.统计年龄大于30岁的学生的人数。

sql 复制代码
/*(1)*/

select count(*) '年龄大于30岁的学生人数'

from Student s

where year(getdate()) - year(Birthday) > 30;



/*(2)*/

select count(*) '年龄大于30岁的学生人数'

from Student s

where DATEDIFF(year,Birthday,getdate()) > 30;

查询结果:

2.统计数据结构有多少人80分或以上。

sql 复制代码
/*嵌套查询*/

select count(*) '数据结构成绩大于或等于80的人数'

from StudentGrade

where Grade >= 80

  and Course_id in (select Course_id from Course where Course_name = '数据结构');



/*连接查询*/

select count(*) '数据结构成绩大于或等于80的人数'

from Course c,

     StudentGrade sg

where c.Course_name = '数据结构'

  and c.Course_id = sg.Course_id

  and sg.Grade >= 80;

查询结果:

3.查询"0203"课程的最高分的学生的学号。

(请分别用TOP1和函数来进行查询,并分析它们的区别)

sql 复制代码
-- TOP1查询

select top 1 Stu_id

from StudentGrade

where Course_id = '0203'

order by Grade desc;



-- 函数查询

select Stu_id

from StudentGrade sg

where Course_id = '0203'

  and Grade = (select max(Grade) from StudentGrade where Course_id = '0203');

查询结果:

4.统计各系开设班级的数目(系名称、班级数目),并创建结果表。

(需考虑没有班级的系)

sql 复制代码
/*根据对应的class_id,如果没有班级就是为null*/

select d.Depar_name, count(cl.Class_id) 'classCount'

into Depar_class

from Deparment d

         left join Class cl on (d.Depar_id = cl.Depar_id)

group by d.Depar_name;

运行结果:

5.选修了以"01"开头的课程的学生学号,姓名,选课的课程号。

sql 复制代码
select distinct s.Stu_id, s.Stu_name, sg.Course_id

from Student s,

     StudentGrade sg

where sg.Course_id like '01%'

  and s.Stu_id = sg.Stu_id;

部分查询结果:

6.统计每科目的最高分、最低分,平均分、总分,并以中文列名显示。

sql 复制代码
select c.Course_name, max(Grade) '最高分', min(Grade) '最低分', avg(Grade) '平均分', sum(Grade) '总分'

from StudentGrade sg,

     Course c

where sg.Course_id = c.Course_id

group by c.Course_name;

部分查询结果:

sql 复制代码
/*没人选的科目也查出来*/

select c.Course_name, max(Grade) '最高分', min(Grade) '最低分', avg(Grade) '平均分', sum(Grade) '总分'

from Course c

         left join StudentGrade sg on c.Course_id = sg.Course_id

group by c.Course_name;

部分查询结果:

7.所有成绩都在70分以上的学生姓名。

(提示:使用子查询。需考虑未选课的学生)

sql 复制代码
select distinct s.Stu_name

from Student s,

     StudentGrade sg

where not exists(select sg.Grade

                 from StudentGrade sg

                 where sg.Stu_id = s.Stu_id

                   and (sg.Grade is null or sg.Grade < 70))

  and s.Stu_id = sg.Stu_id;



/*最低的一门成绩高于70*/

select s.Stu_name

from Student s

where Stu_id in (select sg.Stu_id

                 from StudentGrade sg

                 group by sg.Stu_id

                 having min(Grade) > 70);

查询结果:

8."数据库"课程得最高分的学生的学号、姓名和所在系。

(提示:使用子查询)。

sql 复制代码
select s.Stu_id, s.Stu_name, d.Depar_name

from Student s,

     Class cl,

     Deparment d,

     StudentGrade sg,

     Course c

where s.Stu_id = sg.Stu_id

  and d.Depar_id = cl.Depar_id

  and s.Class_id = cl.Class_id

  and sg.Course_id = c.Course_id

  and c.Course_name='数据库'

  and sg.Grade in (select max(Grade)

                   from StudentGrade

                   where Course_id = (select Course_id from Course where Course_name = '数据库'));

查询结果:

9.至少选修了两门课及以上的学生姓名和性别。

sql 复制代码
select Stu_name, Stu_sex

from Student

where Stu_id in (select Stu_id from StudentGrade group by Stu_id having count(*) > 2);

部分查询结果:

相关推荐
light blue bird3 分钟前
多页签Razor组支轴业务整顿组件
数据库·.net·ai大数据·多功能图表报表·web mvc + razor
wregjru8 分钟前
【mysql】2.数据表操作
数据库·mysql
手握风云-10 分钟前
基于 Java 的网页聊天室(三)
服务器·前端·数据库
LcVong19 分钟前
MySQL 5.2/5.7 开启Binlog日志详细步骤(附验证+查看+恢复)
数据库·mysql·adb
FL4m3Y4n37 分钟前
MySQL缓存策略
数据库·mysql·缓存
wsx_iot39 分钟前
TDengine学习
数据库·学习·tdengine
不吃香菜的小趴菜1 小时前
mysql数据库打包与导入
数据库·mysql
野犬寒鸦1 小时前
Redis复习记录day1
服务器·开发语言·数据库·redis·缓存
njidf1 小时前
实战:用Python开发一个简单的区块链
jvm·数据库·python
Rick19931 小时前
慢SQL优化
数据库·python·sql