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

部分查询结果:

相关推荐
曲幽8 小时前
FastAPI + PostgreSQL 实战:从入门到不踩坑,一次讲透
python·sql·postgresql·fastapi·web·postgres·db·asyncpg
DolphinDB14 小时前
集成 Prometheus 与 DolphinDB 规则引擎,构建敏捷监控解决方案
数据库
IvorySQL14 小时前
PostgreSQL 技术日报 (3月10日)|IIoT 性能瓶颈与内核优化新讨论
数据库·postgresql·开源
DBA小马哥17 小时前
时序数据库是什么?能源行业国产化替换的入门必看
数据库·时序数据库
爱可生开源社区20 小时前
某马来西亚游戏公司如何从 SQL Server 迁移至 OceanBase?
数据库
小瓦码J码1 天前
PostgreSQL表名超长踩坑记
数据库·postgresql
yhyyht1 天前
InfluxDB入门记录(三)flux-dsl
数据库·后端
IvorySQL2 天前
PostgreSQL 技术日报 (3月9日)|EXPLAIN ANALYZE 计时优化与复制语法讨论
数据库·postgresql·开源
用户8307196840822 天前
Java 告别繁琐数据统计代码!MySQL 8 窗口函数真香
java·sql·mysql
stark张宇2 天前
MySQL 核心内幕:从索引原理、字段选型到日志机制与外键约束,一篇打通数据库任督二脉
数据库·mysql·架构