--.学生表
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. 用python语言写一个简单的计算器

    假如我们有这样一个式子: 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2 ...

  2. CSS怎样改变行内样式(通过外部级联样式表) css !important用法CSS样式使用优先级判断

    CSS样式优先级 行内>内部>外部 使用!important的css定义是拥有最高的优先级的.只是在ie6下出了一点小的bug,注意书写方式一般可以轻松避开的. CSS中的!importa ...

  3. Java保存错误日志信息

    我们平时在撸代码的时候,有时候需要将某个代码块的具体错误信息保存到数据库或文件中,以便日后方便快速的查找问题. 使用e.printStackTrace(),我们可以将信息保存在具体的变量中,然后写入数 ...

  4. ansible publishing service

    # ansible 初始化服务机 - hosts: newserver vars: - basedir: opt tasks: - name: 安装常用依赖环境 yum: name={{ item } ...

  5. x264

    x264 x264  h264  1. x264调用主要过程 x264_param_default():设置参数集结构体x264_param_t的缺省值. x264_picture_alloc():为 ...

  6. 查看/进入mac根目录的方式

    1.通过“前往文件夹”快捷键组合 (1)打开finder,点击上部菜单栏“前往”,然后“个人”,直接跳转. (2)快捷键组合:command + shift + G:注意:打开finder后,再快捷键 ...

  7. 文件下载之ServletOutputStream

    使用response.getOutputStream可以获取ServletOutputStream,从而实现向页面发送流数据.但是需要注意的是,不能使用ajax进行请求,因为这样页面不会有任何反应,可 ...

  8. sql日期提取

    --插入数据修改不行:必须提供学号 insert into Student(生日类型) values('阳历') --把月份提取出来 显示两位数 select DATENAME(month,getda ...

  9. 简述cookie ,localStrage,sessionStorage的区别?

    1.cookie: 是一个回话跟踪技术,信息存储在用户硬盘,可以做全局变量. 什么是会话:用户进入网站,开始浏览到结束的这样的一个过程,称为一次会话. 会话跟踪技术:浏览器和服务器之间进行多次请求数据 ...

  10. 初学者怎么才能快速学会Python?

    提起对Python的印象,除了全能之外恐怕就是简单易学了.很多人都在推荐新手学Python入门,毕竟语法简单.语句简洁,所谓“人生苦短我用Python”绝不是一句空话.不过也不能忽视一点:Python ...