SQL Server 实践练习

  • 说明
    • 一 、准备数据表
    • 二、数据表记录
    • 三、练习

说明

sql语句综合练习题目,包含简单查询,聚合,嵌套,多表等。对于问题的解答可能有多种sql语句写法,这里只是提供一种思路方案,不考虑实际效率。 如有错误,欢迎指正!

一 、准备数据表

新建数据库,命名随意,然后添加下列数据表:

/*学生信息表*/
create table Student(
StuNo varchar(10) not null primary key,
StuName nvarchar(20) not null,
age int not null,
sex varchar(2) not null
)
/*教师表*/
create table Teacher(
TeacherNo varchar(10) NOT NULL primary key,
TeacherName Nvarchar(20) NOT NULL
)
/*课程表 (含课程老师) */
create table Course(
CouNo varchar(10) NOT NULL primary key,
CouName nvarchar(20) NOT NULL,
TeacherNo varchar(10) NOT NULL
)
/* 分数表 / 学生选课分数表 */
create table Score(
StuNo varchar(10) NOT NULL,
CouNo varchar(10) NOT NULL,
Credit float NOT NULL,
constraint PK_Score primary key (StuNo,CouNo)
)

简单的外键关系 ,加不加无所谓

alter table course
add constraint FK_Course_Teacher foreign key (TeacherNo)
references Teacher(TeacherNo)
alter table Score
add constraint FK_Score_Student foreign key (StuNo)
references Student(StuNo)
alter table Score
add constraint FK_Score_Course foreign key (CouNo)
references Course(CouNo)

二、数据表记录

然后添加一些数据记录,方便做练习

学生表

/*** 初始化学生表 ***/
insert into Student values ('s001','张三',23,'男');
insert into Student values ('s002','李四',22,'男');
insert into Student values ('s003','吴鹏',22,'男');
insert into Student values ('s004','琴沁',20,'女');
insert into Student values ('s005','王丽',20,'女');
insert into Student values ('s006','李波',21,'男');
insert into Student values ('s007','刘玉',21,'男');
insert into Student values ('s008','萧蓉',21,'女');
insert into Student values ('s009','陈萧晓',23,'女');
insert into Student values ('s010','陈美',22,'女');

教师表

/*** 初始化教师表 ***/
insert into teacher values ('t001', '刘阳');
insert into teacher values ('t002', '陈燕');
insert into teacher values ('t003', '胡明星');
insert into teacher values ('t004', '张顺');
insert into teacher values ('t005', '林超');

课程表

/*** 初始化课程表 **/
insert into course values ('c001','Phyton','t002');
insert into course values ('c002','Java','t002');
insert into course values ('c003','GO','t001');
insert into course values ('c004','Oracle','t001');
insert into course values ('c005','SQL SERVER 2005','t003');
insert into course values ('c006','C#','t003');
insert into course values ('c007','JavaScript','t004');
insert into course values ('c008','DIV+CSS','t004');
insert into course values ('c009','PHP','t003');
insert into course values ('c010','.net core','t003');
insert into course values ('c011','Vue','t005');
insert into course values ('c012','Rust','t005');
insert into course values ('c013','c/c++','t003');

成绩表

/***  分数表 / 学生选课分数表 ***/
insert into Score values ('s001','c001',78.9);
insert into Score values ('s001','c002',82);
insert into Score values ('s001','c003',59);
insert into Score values ('s002','c001',72);
insert into Score values ('s002','c002',80.9);
insert into Score values ('s003','c001',88.9);
insert into Score values ('s003','c002',81.9);
insert into Score values ('s004','c001',60.9);
insert into Score values ('s005','c005',78.9);
insert into Score values ('s005','c006',80);
insert into Score values ('s005','c013',97);
insert into Score values ('s006','c004',60);
insert into Score values ('s006','c007',88.9);
insert into Score values ('s006','c008',72);
insert into Score values ('s006','c011',54);
insert into Score values ('s007','c002',72.9);
insert into Score values ('s007','c003',59);

三、练习

  1. 查询男生、女生人数 。打印: 性别,人数
select Sex as '性别', count(*) as '人数' from student group by Sex
  1. 查询姓“张”的学生名单。
select  '学生姓名'=StuName from Student where StuName like '张%'
  1. 统计各课程的 学生数量 : 课程名,人数
select c.CouName as '课程','人数'=count(s.StuNo)
from Course c
left join Score s
on c.CouNo=s.CouNo
group by s.CouNo,c.CouName,c.CouNo
order by c.CouNo asc
  1. 查询所有同学的选课数及总成绩,学号、姓名、选课数、总成绩
select s1.StuNo,s1.StuName,'选课数'=ISNULL(s2.选课数,0),'总成绩'=ISNULL(s2.总成绩,0)
from Student s1
left join (select StuNo, '选课数'=COUNT(CouNo),'总成绩'=sum(Credit) from Score group by stuno
) s2
on s1.StuNo=s2.StuNo
  1. 查询“c001”课程比“c002”课程成绩高的所有学生 打印 姓名 学号
 select s1.StuName as '姓名',t1.StuNo as '学号' from Score t1left join Student s1on t1.StuNo=s1.StuNowhere CouNo='c001' and Credit>(select Credit from Score t2 where CouNo='c002' and t1.StuNo=t2.StuNo)
select s1.StuName as '姓名',t1.StuNo as '学号'
from (select a1.StuNo,'c001_credit'=a1.Credit,'c002_credit'=a2.Credit from Score a1left join (select * from Score )a2on a1.stuno=a2.stunowhere a1.couNo='c001' and a2.couNo='c002'
) t1
left join Student s1
on t1.StuNo=s1.StuNo
where t1.c001_credit>t1.c002_credit
  1. 查询平均成绩大于60 分的同学的学号姓名和平均成绩
select '姓名'=s1.StuName, '学号'=sc.StuNo,'平均成绩'=sc.Credit from Student s1
right join (select StuNo,'Credit'=avg(Credit) from Score group by StuNohaving AVG(Credit)>60
)sc
on s1.StuNo=sc.StuNo
  1. 查询姓“刘”的老师的人数;
select count(*) as '姓“刘”的老师的人数' from Teacher where TeacherName like '刘%'
  1. 查询学过“陈燕”老师课的同学的学号、姓名
select t1.StuNo, s1.StuName
from Student s1
right join (select distinct c2.StuNo from Course c1 left join (select StuNo,CouNo from Score group by StuNo,CouNo) c2on c1.CouNo= c2.CouNowhere TeacherNo=(select TeacherNo from Teacher where TeacherName='陈燕')
) t1
on s1.StuNo=t1.StuNo
select t1.StuNo, s1.StuName
from Student s1
right join (select  distinct StuNo from Score where CouNo in (select CouNo from Course where TeacherNo=(select TeacherNo from Teacher where TeacherName='陈燕'))
)t1
on s1.StuNo=t1.StuNo
  1. 查询没学过“陈燕”老师课的所有同学的学号、姓名;
select s1.StuNo, s1.StuName
from Student s1
left join (select distinct c2.StuNo from Course c1 left join (select StuNo,CouNo from Score group by StuNo,CouNo) c2on c1.CouNo= c2.CouNowhere TeacherNo=(select TeacherNo from Teacher where TeacherName='陈燕')
) t1
on s1.StuNo=t1.StuNo
where t1.StuNo is null
select stuno,stuname
from Student s
where StuNo not in
(select stuNo from Score sc,(select t.TeacherNo,c.CouNo from Teacher tleft join Course c on t.TeacherNo=c.TeacherNo where t.TeacherName='陈燕')as bwhere sc.CouNo=b.CouNo
)
  1. 每位老师课程占比 (课程表中 每位老师的课 所占比例)
select a.TeacherName ,'课程占比'=CONVERT(decimal(4,2),a.课程数)/CONVERT(decimal(4,2),b.总数) from (
select t1.TeacherName,t2.课程数 from Teacher t1
left join (
select TeacherNo,'课程数'=count(CouNo) from Course
group by TeacherNo
)t2
on t1.TeacherNo=t2.TeacherNo)as a,(select '总数'=count(CouNo) from Course ) as b
  1. 查询学过“c001”并且也学过编号“c002”课程的同学的学号、姓名;
select a.StuName,b.StuNo from Student as a
right join(select t1.StuNo from Score t1inner join (select StuNo from Score  where CouNo='c002')t2on t1.StuNo=t2.StuNowhere t1.CouNo='c001'
) as b
on a.StuNo=b.StuNo
  1. 查询所有课程成绩小于60 分的同学的学号、姓名
select b.StuNo,s.StuName
from Student s
right join(select sc.Stuno ,' s'=AVG(credit) from Score sc
group by sc.StuNo
having avg(Credit)<60
)as b
on s.StuNo=b.StuNo
  1. 查询没有学全课程表中所有课的同学的学号、姓名
select st.StuName,st.StuNo
from Student st
where st.StuNo not in(select StuNo from Score sgroup by StuNohaving count(couno)=(select count(*) from Course)
)
select st.StuNo,st.StuName,'课程数'=count(s.couno) from student st
left join Score s
on st.StuNo=s.StuNo
group by st.StuNo,st.StuName
having count(s.couno)<(select '总课程数'=count(*) from Course )
  1. 查询至少有一门课与学号为“s001”的同学所学相同 的同学的学号和姓名
select st.StuName,t1.StuNo from Student st
right join (select distinct StuNo from Score  where StuNo<>'s001'and CouNo in(select CouNo from Score where StuNo='s001')
) t1
on st.StuNo=t1.StuNo
select distinct b.StuNo,st.StuName from Score s
inner join(select Stuno,CouNo from Score where StuNo!='s001'
)as b
on s.CouNo=b.CouNo and s.StuNo='s001'
left join Student st
on b.StuNo=st.StuNo
  1. 查询课程表中各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
select c.CouName,t1.最高分,t1.最低分 from Course  c
left join (select CouNo, '最高分'=MAX(Credit),'最低分'=min(Credit) from Scoregroup by CouNo
) t1
on c.CouNo=t1.CouNo
  1. Score表中 按各科成绩及格率从高到低顺序(70 及格)
select s1.CouNo, '及格率'=CONVERT(float, ISNULL( s2.及格人数,0))/count(s1.Stuno)
from Score s1
left join (select CouNo, count(Stuno) as '及格人数'  from Scorewhere Credit>=70group by CouNo
) s2
on s1.CouNo=s2.CouNo
group by s1.CouNo,s2.及格人数
order by 及格率 desc
select CouNo,convert(float,sum(case when Credit>=70 then 1 else 0 end))/count(Credit) as '及格率'
from Score
group by CouNo
order by 及格率 desc
  1. 查看学生c001 c002 c003 课程 成绩。打印:学生姓名 C001 C002 C003
select st.StuName as '学生姓名',sc.c001,sc.c002,sc.c003 from student st
left join (select StuNo,sum(case when CouNo='c001' then Credit else null end) as 'c001',sum(case when CouNo='c002' then Credit else null end ) as  'c002',sum(case when CouNo='c003' then Credit else null end) as 'c003'from Scoregroup by StuNo) sc
on st.StuNo=sc.StuNo
  1. 统计列印各科成绩,各分数段人数。
    打印:课程ID,课程名称,[100-85],(85-70],(70-60],[ <60]
select c.CouNo, c.CouName,t1.* from Course c
left join (
select CouNo, sum(case when Credit>=85 then 1 else 0 end) as '100-85',sum(case when Credit>=70 and Credit<85 then 1 else 0 end) as '85-10',sum(case when Credit>=60 and Credit<70 then 1 else 0 end) as '70-60',sum(case when Credit<60  then 1 else 0 end) as '<60'
from Score
group by CouNo
)t1
on c.CouNo=t1.CouNo
  1. 查询不同老师所教不同课程平均分从高到低显示
select t.TeacherName ,t1.CouName,t1.平均分 from Teacher t
left join (select c.CouName,c.TeacherNo,s.平均分 from Course cleft join (select CouNo,'平均分'=avg(credit) from Scoregroup by CouNo) son c.CouNo=s.CouNo
) t1
on t.TeacherNo=t1.TeacherNo
order by t1.平均分 desc
  1. 查询分数表中 每门功课成绩最好的前两名
select * from (select *,rank()over(partition by couno order by credit desc) as num from Score
)as a
where num<3
  1. 查询各科成绩前三名的记录:(不考虑成绩并列情况) 课程名称,学生,成绩,排名,
select t1.CouName,st.StuName,t2.Credit,t2.rn from Course t1
left join(select * from (select StuNo,CouNo,Credit,ROW_NUMBER() over(partition by CouNo order by credit desc) as 'rn' from Score)scwhere sc.rn<4
) t2
on t1.CouNo=t2.CouNo
left join Student st
on t2.StuNo=st.StuNo
where t2.rn<4

SQL Server 初学综合练习题相关推荐

  1. SQL Server数据库连续集成(CI)最佳实践以及如何实现它们–测试,处理和自动化

    测试中 (Testing) Test databases should be processed with unit tests In many shops code is unit tested a ...

  2. SQL Server 练习题(初学)

    SQL Server 练习题(基础) 一.前言 二.准备 三.练习 1.创建练习题目的数据库(新建查询) 2.练习题 一.前言 新学了一门<数据库原理及应用>,布置了一些基础题目,互相学习 ...

  3. 【SQL Server】数据库开发指南(五)T-SQL 高级查询综合应用与实战

    本系列博文还在更新中,收录在专栏:#MS-SQL Server 专栏中. 本系列文章列表如下: [SQL Server] Linux 运维下对 SQL Server 进行安装.升级.回滚.卸载操作 [ ...

  4. SQL SERVER练习题及答案2

    SQL SERVER 2009-07-11 21:59 阅读156 评论1 字号: 大大 中中 小小 一.选择题(每题1分,共25分) 1. 数据库.数据库系统以及数据库管理系统的英文缩写分别是__A ...

  5. SQL server练习题

    目录 第一章 数据库基础 选择题 说明题 第二章 数据库创建 选择题 填空题 操作题 第三章 数据库的查询和视图 说明题 第一章 数据库基础 选择题 SQL server是(数据库管理系统) SQL ...

  6. SQL Server 练习题及答案1

    SQL Server 期末考试题目及答案 一.单选题 1. Microsoft公司的SQL Server2000数据库管理系统一般只能运行于( ). A. Windows平台 B. UNIX平台 C. ...

  7. 数据库原理(三):Sql Server操作语句

    文章目录 数据库 创建数据库 数据库表创建 删除数据库表 主键.外键.检查约束 7.2 插入数据 查询 9.1.2 查询不重复的信息 9.1.3 查询前五个,且显示固定列的数据 9.1.4 查询前百分 ...

  8. SQL Server 存储过程

    本章内容简介: • 存储过程的定义以及何时需要使用一个存储过程 • 如何创建.修改和删除存储过程 • 传递输入和输出参数的方式 • 错误处理 • 性能考虑事项 • 如何使用调试器 存储过程很有用.如果 ...

  9. 隆重推荐【SQLServer】127个SQL server热门资料汇总(转)

    最近有许多关于如何学习SQLSERVER的问题,其实新手入门的资源和贴子很多,现在向大家隆重推荐经过精心整理的[SQLServer]127个SQL server热门资料汇总 ,希望能对学习SQLSER ...

最新文章

  1. jfinal为weebox弹出框传递参数
  2. 恢复后缀phobos勒索病毒 解密成功 百分百恢复sql文件
  3. 库洛游戏首次公开分享:《战双帕弥什》的动作打击感是怎么做出来的
  4. html中dd dt的效果,html中dt dd
  5. UDT源码剖析(四):UDT的GC线程相关过程代码注释
  6. 微软提高 Microsoft 365 的漏洞奖励
  7. 捷克论坛最新ip地址_最新macOS破坏SSH默认规则,程序员无法登录Web服务器
  8. php基础语法学习汇总
  9. 怎么用html打开图片,viewerjs 在html打开图片或打开pdf文件使用案例
  10. 2019上半年系统集成项目管理工程师下午真题及答案解析
  11. Win32扫雷(根据以前的控制台扫雷实现)
  12. Groovy语法介绍
  13. 苹果手机照片误删如何找回
  14. 20个案例掌握PL/SQL 基础
  15. jenkins邮件模板配置
  16. 根据卫星的方位角和仰角画卫星星空图(QT实现)
  17. 苏黎世联邦理工学院计算机博士去向,2019年5月31日学术报告(李文 研究员,瑞士苏黎世联邦理工学院)...
  18. 题解:P4961 小埋与扫雷
  19. 22年最强Java面试八股文界的“六边形战士”,堪称天花板!
  20. 细数STM32开发板有哪些,官方板/正点原子/野火/安富莱等

热门文章

  1. 如何抠图去除背景?怎样抠图出来干净?
  2. js遍历数组的几种方式
  3. 在桌面拔和平精英改成计算机,和平精英电脑版怎么设置键位?和平精英桌面版键位一览...
  4. websocket如何携带header或参数
  5. 聊聊几乎已成为现代事实标准的“box-sizing: border-box”
  6. 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
  7. PuTTY用户手册(五)
  8. android加入摄像功能,Android Camera使用之录像功能
  9. [Hyper-v]删除系统保留分区,修复克隆win7/win8虚拟磁盘后无法引导问题
  10. java求三角形周长 面积_Java代码计算三角形的周长和面积