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

部分查询结果:

相关推荐
无泡汽水几秒前
MySQL入门练习50题
数据库·mysql
JIngJaneIL31 分钟前
助农惠农服务平台|助农服务系统|基于SprinBoot+vue的助农服务系统(源码+数据库+文档)
java·前端·数据库·vue.js·论文·毕设·助农惠农服务平台
云外天ノ☼35 分钟前
待办事项全栈实现:Vue3 + Node.js (Koa) + MySQL深度整合,构建生产级任务管理系统的技术实践
前端·数据库·vue.js·mysql·vue3·koa·jwt认证
小光学长1 小时前
基于Vue的智慧楼宇报修平台设计与实现066z15wb(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
前端·数据库·vue.js
CodeBlossom1 小时前
Spring Cache快速入门
java·数据库·spring
tuokuac1 小时前
ps -ef | grep redis
数据库·redis·缓存
⑩-2 小时前
如何保证Redis和Mysql数据缓存一致性?
java·数据库·redis·mysql·spring·缓存·java-ee
征尘bjajmd2 小时前
Java使用okhttp发送get、post请求
java·服务器·数据库
清风6666662 小时前
基于单片机的智能高温消毒与烘干系统设计
数据库·单片机·嵌入式硬件·毕业设计·课程设计·期末大作业
Databend2 小时前
Databend 十月月报:存储过程正式可用,数据流程全面自动化
数据库