摘:SQL 常见题练习
--.学生表
Student(SId,Sname,Sage,Ssex)
--SId 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别 --.课程表
Course(CId,Cname,TId)
--CId 课程编号,Cname 课程名称,TId 教师编号 --.教师表
Teacher(TId,Tname)
--TId 教师编号,Tname 教师姓名 --.成绩表
SC(SId,CId,score)
--SId 学生编号,CId 课程编号,score 分数
SQL 常见题练习
1.查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数
解:因为需要全部的学生信息,则需要在sc表中得到符合条件的SId后与student表进行join
select * from Student RIGHT JOIN (
select t1.SId,class1,class2 from
(select SId ,score as class1 from SC where SC.CId = '01')as t1,
(select SId ,score as class2 from SC where SC.CId = '02')as t2 where t1.SId = t2.SId and t1.class1 > t2.class2 )r
on Student.SId = r.SId
1.1查询同时存在" 01 "课程和" 02 "课程的情况
select * from
(select * from SC where SC.CId = '01') as t1,(select * from SC where SC.CId = '02') as t2
where t1.SId = t2.SId
1.2 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )
select * from
(select * from SC where SC.CId = '01') as t1
left join
(select * from SC where SC.CId = '02') as t2
on t1.SId = t2.SId
1.3 查询不存在" 01 "课程但存在" 02 "课程的情况
select * from sc
where sc.SId not in(select SId from where sc.CId = '01')
and sc.CId = '02';
2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
select Student.SId,Sname,ss from Student,(
select SId,avg(score) as ss from sc group by SId having avg(score)>60)r
where Student.SId = r.SId;
3.查询在 SC 表存在成绩的学生信息
select DISTINCT student.* from Student,sc where Student.SId = sc.SId
ps:DISTINCT 用于返回唯一不同的值
4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
select s.sid,s.sname,r.coursenumber,r.scoresum from(
(select student.sid,student.sname from student)s
left join
(select sc.sid,sum(sc.score)as scoresum ,count(sc.cid)as coursenumber from sc group by sc.sid )r
on s.sid = r.sid
);
4.1 查有成绩的学生信息
select *from student where student.sid in (select sc.sid from sc);
5.查询「李」姓老师的数量
select count(*) from teacher where tname like '李%';
6.查询学过「张三」老师授课的同学的信息
select student.* from student,teacher,course,sc
where student.sid = sc.sid and course.cid = sc.cid and course.tid = teacher.tid and tname = '张三';
7.查询没有学全所有课程的同学的信息
select * from student where student.sid not in(
select sc.sid from sc group by sc.sid having count(sc.cid) = (select count(cid) from course)
)
8.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信
9.查询没学过"张三"老师讲授的任一门课程的学生姓名
select * from student where student.sid not in(
select sc.sid from sc,course,teacher where
sc.cid = course.cid
and course.tid = teacher.tid
and teacher.tname = "张三"
);
10.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select student.sid,student.sname,avg(sc.score) from student,sc
where
student.sid = sc.sid and sc.score<60 group by sc.sid having count(*)>1;
11.检索" 01 "课程分数小于 60,按分数降序排列的学生信息
select student.* ,sc.score from student,sc
where student.sid = sc.sid and sc.score < 60 and cid = '01' order by sc.score desc;
12.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select * from sc
left join(
select sid,avg(score) as avscore from sc group by sid)r
)
on sc.sid = r.sid order by avscore desc;
13.查询各科成绩最高分、最低分和平均分:
以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率。
及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90。
要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select sc.CId ,max(sc.score) as 最高分,min(sc.score) as 最低分, avg(sc.score) as 平均分,count(*)as 选修人数,
sum(case when sc.score>=60 then 1 else 0 end)/count(*) as 及格率
from sc group by sc.CId order by count(*) desc,sc.CId asc
ps:case when sc.score>=60 then 1 else 0 end,即符合sc.score>=60加1,否则加0
14.按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺
用sc中的score和自己进行对比,来计算“比当前分数高的分数有几个”。
select a.cid,a.sid,a.score,count(b.score)+1 as rank from sc as a
left join sc as b
on a.score < b.score and a.cid = b.cid
group by a.cid,a.sid,a.score order by a.cid,rank asc;
15.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比
select course.cname,course.cid,
sum(case when sc.score<=100 and sc.score>85 then 1 else 0 end) as "[100-85]",
sum(case when sc.score<=85 and sc.score>70 then 1 else 0 end) as "[85-70]",
sum(case when sc.score<=70 and sc.score>60 then 1 else 0 end) as "[70-60]",
sum(case when sc.score<=60 and sc.score>0 then 1 else 0 end) as "[0-60]",
from sc left join course on sc.cid = course.cid group by sc.cid;
16.查询每门课程被选修的学生数
select cid,count(sid) from sc group by cid;
17.查询出只选修两门课程的学生学号和姓名
select student.sid,student.sname from sc,student where student.sid =sc.sid
group by sc.sid having count(*) = 2
18.查询男生、女生人数
select ssex,cout(*) from student group by ssex
摘:SQL 常见题练习的更多相关文章
- 【T-SQL基础】01.单表查询-几道sql查询题
概述: 本系列[T-SQL基础]主要是针对T-SQL基础的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础]02.联接查询 [T-SQL基础]03.子查询 [T-SQL基础 ...
- SQL常见笔试面试题
sql理论题 1.触发器的作用? 答:触发器是一中特殊的存储过程,主要是通过事件来触发而被执行的.它可以强化约束,来维护数据的完整性和一致性,可以跟踪数据库内的操作从而不允许未经许可的更新和变化.可以 ...
- PL/SQL常见设置--Kevin的专栏
body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...
- SQL语句题
SQL语句题 Student(Sno,Sname,Sage,Ssex)注释:学生表(学号,姓名,性别年龄,性别) Course(Cno,Cname,Tno) 注释:课程表(课程号,课程名称,教师编号) ...
- 牛客SQL刷题第三趴——SQL必知必会
01检索数据 SQL60 从 Customers 表中检索所有的 ID 编写 SQL 语句,从 Customers 表中检索所有的cust_id select * from Customers; SQ ...
- SQL常见优化Sql查询性能的方法有哪些?
常见优化Sql查询性能的方法有哪些? 1.查询条件减少使用函数,避免全表扫描 2.减少不必要的表连接 3.有些数据操作的业务逻辑可以放到应用层进行实现 4.可以使用with as 5.使用“临时表”暂 ...
- sql常见的面试题
1.用一条SQL语句 查询出每门课都大于80分的学生姓名 name kecheng fenshu 张三 语文 81张三 数学 75李四 语文 ...
- SQL语句题库
一. 填空题 Not Only SQL数据库 泛指 非关系型数据库 . SYS和SYSTEM用户都是Oracle 的系统用户,它们都使用SYSTEM表空间,其中 sys 拥有更大的权限. O ...
- SQL常见的可优化点
# 索引相关 # ################################################### 1. 查询(或更新,删除,可以转换为查询)没有用到索引 这是最基础的步骤,需要 ...
随机推荐
- 亚马逊EC2构建代理服务器心血历程
1.亚马逊上申请一台免费的EC2服务器,有相应的教程,绑定信用卡,预支付1美元,据说可以退回(防止到期后直接扣款,支付后通过修改卡信息,但好象有提示了,说卡不对了,也不管它了,到期后再说,美国人也不是 ...
- 将MongoDB服务器设置成Windows启动服务(win10)
如题,这个问题也百度了很久,百度还是挺给力的,但是都没能解决问题,后来在大神(原谅我不知道大神叫什么)的指导下,终于设置成功,特分享下设置过程.. MongoDB设置数据库我就不说了...额..算了, ...
- 离线安装Selenium
https://blog.csdn.net/poem_ruru/article/details/79032140
- Python开发工具搭建-Pycharm
PyCharm2017. 3.X专业版 安装使用. 注册码激活 本文以 Windows系统 为例: 1.开发工具获取及下载 Anaconda(Python 的集成工具 ) 下载地址: https:// ...
- 06--Qt窗口布局
Qt窗口布局 标签: qtlayout 2012-05-05 07:56 3076人阅读 评论(0) 收藏 举报 分类: Qt开发(33) 版权声明:本文为博主原创文章,未经博主允许不得转载. 布 ...
- C# 获取表中最大值
; if (db.LPicture.Any()) { // LPicture Newmode = db.LPicture.Where(n => ).FirstOrDefault(); start ...
- 不用float也可以让div横向显示
display: inline-block; vertical-align: top; 就这两个属性,给div设置上,div就不会换行显示啦,而且还不影响横向的其他元素的显示.
- 数字化婚姻配对尝试问题(C++实现)
问题描述:一.标题: 数字化婚姻配对尝试 二.题目: 建立一个模型,来模拟推导社会男女择偶过程. 为了模型简化,一个人的特性指标有三个,这里假设为财富.样貌.品格,每个指标均可取值1-100之间任意数 ...
- vue刷新本页面
顶层app.vue页面 <template> <div id="app"> <router-view v-if="isRouterAlive ...
- SweetAlert弹出框
以前也用过,那个时候没有写过,突然看见了,就写上了. 网址:http://mishengqiang.com/sweetalert2/ swal({ title: '确定删除吗?', text: '你将 ...