【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);

部分查询结果:

相关推荐
高梦轩2 分钟前
MySQL 故障排查与优化
数据库·mysql
吴声子夜歌18 分钟前
Node.js——操作MySQL数据库
数据库·mysql·node.js
爱丽_19 分钟前
MySQL 锁等待与死锁进阶:怎么看等待、怎么降冲突(工程化套路)
数据库·mysql
心有—林夕29 分钟前
MySQL 误操作恢复完全指南
android·数据库·mysql
夕除30 分钟前
Mysql--15
java·数据库·mysql
野生技术架构师1 小时前
掌握SQL窗口函数,轻松处理复杂数据分析
数据库·sql·数据分析
会飞的大可1 小时前
NoSQL:从原理到实践的全景指南
数据库·nosql
刘~浪地球1 小时前
Redis 从入门到精通(四):字符串操作详解
数据库·redis·缓存
荒川之神2 小时前
MySQL 商品拉链表 完整最终版(配备了全套存储过程)
数据库·mysql
admin and root2 小时前
从资产收集FUZZ接口到SQL注入案例
网络·数据库·sql·安全·web安全·渗透测试·log4j