--.学生表
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 常见题练习的更多相关文章

  1. 【T-SQL基础】01.单表查询-几道sql查询题

    概述: 本系列[T-SQL基础]主要是针对T-SQL基础的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础]02.联接查询 [T-SQL基础]03.子查询 [T-SQL基础 ...

  2. SQL常见笔试面试题

    sql理论题 1.触发器的作用? 答:触发器是一中特殊的存储过程,主要是通过事件来触发而被执行的.它可以强化约束,来维护数据的完整性和一致性,可以跟踪数据库内的操作从而不允许未经许可的更新和变化.可以 ...

  3. PL/SQL常见设置--Kevin的专栏

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...

  4. SQL语句题

    SQL语句题 Student(Sno,Sname,Sage,Ssex)注释:学生表(学号,姓名,性别年龄,性别) Course(Cno,Cname,Tno) 注释:课程表(课程号,课程名称,教师编号) ...

  5. 牛客SQL刷题第三趴——SQL必知必会

    01检索数据 SQL60 从 Customers 表中检索所有的 ID 编写 SQL 语句,从 Customers 表中检索所有的cust_id select * from Customers; SQ ...

  6. SQL常见优化Sql查询性能的方法有哪些?

    常见优化Sql查询性能的方法有哪些? 1.查询条件减少使用函数,避免全表扫描 2.减少不必要的表连接 3.有些数据操作的业务逻辑可以放到应用层进行实现 4.可以使用with as 5.使用“临时表”暂 ...

  7. sql常见的面试题

    1.用一条SQL语句 查询出每门课都大于80分的学生姓名 name   kecheng   fenshu 张三     语文       81张三     数学       75李四     语文   ...

  8. SQL语句题库

    一.    填空题 Not Only SQL数据库 泛指  非关系型数据库  . SYS和SYSTEM用户都是Oracle 的系统用户,它们都使用SYSTEM表空间,其中 sys 拥有更大的权限. O ...

  9. SQL常见的可优化点

    # 索引相关 # ################################################### 1. 查询(或更新,删除,可以转换为查询)没有用到索引 这是最基础的步骤,需要 ...

随机推荐

  1. [转]我是蒟蒻,但我有我的OI信仰

    我想最大的浪漫莫过于有人陪你征战OI吧 有多少无眠的夜晚?我总是在想, 到底是为了什么? 为了自招?为了省队?为了签约? 这条路很艰难,不可谓不凶险, 当你第一次踏上复试, 你肯定有看到过那些很厉害很 ...

  2. Coursera公开课-Machine_learing:编程作业

    第二周编程作业:Linear Regression 分为单一变量和多变量,假想函数为:hθ(x)=θ0+θ1x1+θ2x2+θ3x3+⋯+θnxn.明显已经包含单一变量的情况,所以完成多变量可以一并解 ...

  3. Hadoop Hive概念学习系列之hive里的桶(十一)

    不多说,直接上干货!  Hive还可以把表或分区,组织成桶.将表或分区组织成桶有以下几个目的: 第一个目的是为看取样更高效,因为在处理大规模的数据集时,在开发.测试阶段将所有的数据全部处理一遍可能不太 ...

  4. Hadoop Hive概念学习系列之hive里如何显示当前数据库及传参(十九)

    这个小知识点,看似简单,用处极大. $ hive --hiveconf hive.cli.print.current.db=true $ hive --hiveconf hive.cli.print. ...

  5. 在mac上快捷找到nodejs的安装路径

    打开控制台输入 which node ,得到的输出结果就是node安装路径

  6. jQuery——自定义动画

    动画方法:animate(json,1000, function (){}) 参数说明:json代表属性设置,1000是动画时间,最后一个是回调函数,其中动画时间可选 属性支持:http://www. ...

  7. Android ExpandableListView的使用详解

    ExpandableListView(可扩展的ListView) ExpandableListVivew是ListView的子类,它在普通ListView的基础上进行了扩展,它把应用中的列表项分为几组 ...

  8. php用户注册常用检测、写入

    // 判断数据库是否已经存在 $check_sql = "select * from user where idNumber='$idNumber'"; $check_query ...

  9. zTree 模糊搜索

    /** * 搜索树,高亮显示并展示[模糊匹配搜索条件的节点s] * @param treeId * @param searchConditionId 搜索条件Id */ function search ...

  10. 什么是ACID

    ACID是衡量事务的四个特性: 原子性(Atomicity,或称不可分割性) 一致性(Consistency) 隔离性(Isolation) 持久性(Durability) 原子性:原子性是指一个事务 ...