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

部分查询结果:

相关推荐
Coder_Boy_1 天前
基于SpringAI的智能平台基座开发-(六)
java·数据库·人工智能·spring·langchain·langchain4j
热爱专研AI的学妹1 天前
数眼搜索API与博查技术特性深度对比:实时性与数据完整性的核心差异
大数据·开发语言·数据库·人工智能·python
hopsky1 天前
ShardingSphere功能简介
数据库·sql
talenteddriver1 天前
mysql: MySQL索引和排序相关名词概念汇总
数据库·mysql
武昌库里写JAVA1 天前
iview-CRUD模板
vue.js·spring boot·sql·layui·课程设计
6极地诈唬1 天前
【PG漫步】DELETE不会改变本地文件的大小,VACUUM也不会
linux·服务器·数据库
MZWeiei1 天前
Redis持久化机制中的 AOF机制简单介绍
数据库·redis
Elastic 中国社区官方博客1 天前
Elasticsearch:在 X-mas 吃一些更健康的东西
android·大数据·数据库·人工智能·elasticsearch·搜索引擎·全文检索
酷柚易汛1 天前
酷柚易汛ERP 2025-12-26系统升级日志
java·前端·数据库·php
wang6021252181 天前
阿里云存储的一些简要概述
数据库·阿里云·fastapi