摘: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. 查询(或更新,删除,可以转换为查询)没有用到索引 这是最基础的步骤,需要 ...
随机推荐
- Spring Cloud (13) 服务网关-路由配置
传统路由配置 所谓传统路由配置方式就是在不依赖于服务发现机制情况下,通过在配置文件中具体制定每个路由表达式与服务实例的映射关系来实现API网关对外部请求的路由.没有Eureka服务治理框架帮助的时候, ...
- Java系列学习(十四)-集合
1.java中的集合学习 2.Collection中常用方法 1.添加功能 boolean add(E e):添加一个元素 boolean addAll(Collection<? extends ...
- Struts2 之 实现文件上传(多文件)和下载
Struts2 之 实现文件上传和下载 必须要引入的jar commons-fileupload-1.3.1.jar commons-io-2.2.jar 01.文件上传需要分别在struts.xm ...
- js 学习笔记---BOM
window对象 1. window 对象是Global对象,在全局作用域中声明的变量和函数都可以通过window.来访问.跟直接在window上添加属性效果一样.唯一的区别就是delete时,如果是 ...
- Zynq7000系列之芯片系统结构概述
相比较经典的FPGA,Zynq7000系列最大的特点是将处理系统PS和可编程资源PL分离开来,固化了PS系统的存在,实现了真正意义上的SOC(System On Chip). 1. Zynq7000 ...
- String数据类型转换
String是final类,提供字符串不可修改.强制类型转换,String类型无处不在.下面介绍一些常见的String数据类型转换. String数据类型转换成long.int.double.floa ...
- lnmp环境搭建后续-php安装
安装PHP7: 下载# wget http://PHP.net/get/php-7.0.2.tar.gz/from/a/mirror 建议安装之前先看看安装帮助文件INSTALL 解压安装 # tar ...
- Ubuntu Mysql 常用指令
mysql 常用指令及中文乱码解决 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...
- sqlserver 批量更新
select * from [LPicture] UPDATE [dbo].[LPicture] SET [picGroup] = ' WHERE LPictureid ,); select * fr ...
- css流光效果
css流光效果1: <!DOCTYPE html> <html> <head> <title>ww</title> </head> ...