练习主要涉及四张表,分别如下:

student(sid,sname,sage,ssex) 学生表 
course(cid,cname,tid) 课程表 
score(sid,cid,score) 成绩表 
teacher(tid,tname) 教师表

首先建立表结构

CREATE TABLE student ( sid    INT, sname varchar (32), sage  INT, ssex  varchar (8) ); CREATE TABLE course ( cid    INT, cname varchar(32), tid    INT ); CREATE TABLE score ( sid    INT, cid    INT, score INT ); CREATE TABLE teacher ( tid    INT, tname varchar(16) );

插入数据

--oracle
insert into student select 1,'刘一',18,'男' FROM dual union allselect 2,'钱二',19,'女' FROM dual union allselect 3,'张三',17,'男' FROM dual union allselect 4,'李四',18,'女' FROM dual union allselect 5,'王五',17,'男' FROM dual union allselect 6,'赵六',19,'女' FROM dual; insert into teacher select 1,'叶平' FROM dual union allselect 2,'贺高' FROM dual union allselect 3,'杨艳' FROM dual union allselect 4,'周磊' FROM dual;insert into course select 1,'语文',1 FROM dual union allselect 2,'数学',2 FROM dual union allselect 3,'英语',3 FROM dual union allselect 4,'物理',4 FROM dual;insert into scoreselect 1,1,56 FROM dual union all select 1,2,78 FROM dual union all select 1,3,67 FROM dual union all select 1,4,58 FROM dual union all select 2,1,79 FROM dual union all select 2,2,81 FROM dual union all select 2,3,92 FROM dual union all select 2,4,68 FROM dual union all select 3,1,91 FROM dual union all select 3,2,47 FROM dual union all select 3,3,88 FROM dual union all select 3,4,56 FROM dual union all select 4,2,88 FROM dual union all select 4,3,90 FROM dual union all select 4,4,93 FROM dual union all select 5,1,46 FROM dual union all select 5,3,78 FROM dual union all select 5,4,53 FROM dual union all select 6,1,35 FROM dual union all select 6,2,68 FROM dual union all select 6,4,71 FROM dual;--mysql
insert into student values (1,'刘一',18,'男'),(2,'钱二',19,'女'),(3,'张三',17,'男'),(4,'李四',18,'女'),(5,'王五',17,'男'),(6,'赵六',19,'女'); insert into teacher values (1,'叶平'),(2,'贺高'),(3,'杨艳'),(4,'周磊');insert into course values (1,'语文',1),(2,'数学',2),(3,'英语',3),(4,'物理',4);insert into score values(1,1,56), (1,2,78), (1,3,67), (1,4,58), (2,1,79), (2,2,81), (2,3,92), (2,4,68), (3,1,91), (3,2,47), (3,3,88), (3,4,56), (4,2,88), (4,3,90), (4,4,93), (5,1,46), (5,3,78), (5,4,53), (6,1,35), (6,2,68), (6,4,71);

练习题Beginning!!!

1.查询“001”课程比“002”课程成绩高的所有学生的学号

select a.sid from (select sid,score from score where cid=001) a,(select sid,score from score where cid=002) b where a.sid=b.sid and a.score>b.score;

2.查询平均成绩大于60分的同学的学号和平均成绩

--oracle
select sid,avg(nvl(score,0)) from score group by sid having avg(nvl(score,0))>60;
--nvl(字段,为空返回值)
--nvl2(字段,不为空返回值,为空返回值)--mysql
select sid,avg(ifnull(score,0)) from score group by sid having avg(ifnull(score,0))>60;--补充:MySQL中如何查询的字段中可能存在null值,可以做一些处理
--1.isnull(exper) 判断exper是否为空,是则返回1,否则返回0
--2.ifnull(exper1,exper2)判断exper1是否为空,是则用exper2代替
--3.nullif(exper1,exper2)如果expr1= expr2 成立,那么返回值为NULL,否则返回值为expr1。

3.查询所有同学的学号、姓名、选课数、总成绩

select t1.sid,t1.sname,count(t2.cid),sum(t2.score) from student t1 left join score t2 on t1.sid=t2.sid group by t1.sid,sname;

4.查询姓“李”的老师的个数

select count(distinct tname) from teacher where tname like '李%';
或
select count(distinct tname) from teacher where substr(tname,0,1)='李';

5.查询没学过“叶平”老师课的同学的学号、姓名

select sid,sname from student where sid not in
(select distinct t1.sid from score t1 left join course t2 on t1.cid=t2.cid where t2.tid=(select tid from teacher where tname='叶平'));

6.查询学过“001”并且也学过编号“002”课程的同学的学号、姓名

select sid,sname from student where sid in (select sid from score where cid=001 or cid=002 group by sid having count(cid)>1);
或
select t1.sid,t1.sname from student t1 left join score t2 on t1.sid=t2.sid where t2.cid=001 and exists (select * from score t3 where t2.sid=t3.sid and t3.cid=002);

7.查询学过“叶平”老师所教的所有课的同学的学号、姓名

select sid,sname from student where sid in
(select sid from score t1,course t2,teacher t3 where t1.cid=t2.cid and t2.tid=t3.tid and t3.tname='叶平' group by t1.sid having count(distinct t2.cid)=
(select count(distinct course.cid) from course left join teacher on course.tid=teacher.tid where teacher.tname='叶平'));

8.查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名

SELECT a.sid,a.sname from
(select t1.sid,t1.sname,t2.score from student t1,score t2 where t1.sid=t2.sid and t2.cid=001) a,
(select t1.sid,t1.sname,t2.score from student t1,score t2 where t1.sid=t2.sid and t2.cid=002) b
where a.sid=b.sid and b.score < a.score;
或
select sid,sname from (
select t1.sid,t1.sname,t2.score,(select t3.score from score t3 where t1.sid=t3.sid and t3.cid=002) score2
from student t1,score t2 where t1.sid=t2.sid and t2.cid=001) where score2 < score;

9.(1)查询所有课程成绩都小于60分的同学的学号、姓名

select t1.sid,t1.sname from student t1 left join score t2 on t1.sid=t2.sid group by t1.sid,sname having max(t2.score)<60;

(2)查询所有课程中,存在成绩小于60分的同学的学号、姓名

select sid,sname from student where sid not in (select t1.sid from student t1 left join score t2 on t1.sid=t2.sid and t2.score>60);
或
select t1.sid,t1.sname from student t1 left join score t2 on t1.sid=t2.sid group by t1.sid,sname having min(t2.score)<60;

10.查询没有学全所有课的同学的学号、姓名

select t1.sid,t1.sname from student t1 left join score t2 on t1.sid=t2.sid group by t1.sid,sname having count(t2.cid)<(select count(cid) from course);

11.查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名

select distinct t1.sid,t1.sname from student t1 left join score t2 on t1.sid=t2.sid where t2.cid in (select distinct cid from score where sid=001) and t1.sid <>001;

12.把“score”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩

update score set score=(select avg(score) from score where cid=
(select t1.cid from course t1,teacher t2 where t1.tid=t2.tid and t2.tname='叶平')) where cid=
(select t1.cid from course t1,teacher t2 where t1.tid=t2.tid and t2.tname='叶平');

13.查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名;

SELECT t1.sid,t1.sname from student t1,score t2 where t1.sid=t2.sid and t2.cid in
(select distinct cid from score where sid=002) and t1.sid != 002 group by t1.sid,t1.sname
having count(distinct t2.cid) = (select count(distinct cid) from score where sid=002);

14.删除学习“叶平”老师课的SC表记录;

delete from score where cid in (select t1.cid from course t1 left join teacher t2 on t1.tid=t2.tid where t2.tname='叶平'); 

15.向score表中插入一些记录,这些记录要求符合以下条件:没有上过编号“003”课程的同学学号、2号课的平均成绩

insert into score(sid,cid,score) select sid,'002',(select avg(score) from score where cid='002') from student
where sid not in (select sid from score where cid='003');

16.按平均成绩从高到低显示所有学生的“语文”、“数学”、“英语”、“物理”四门的课程成绩,按如下形式显示: 学生ID,语文,数学,英语,物理,有效课程数,有效平均分

select t.sid 学生ID,
(select t1.score from score t1 left join course t2 on t1.cid=t2.cid where t1.sid=t.sid and t2.cname='语文') 语文,
(select t1.score from score t1 left join course t2 on t1.cid=t2.cid where t1.sid=t.sid and t2.cname='数学') 数学,
(select t1.score from score t1 left join course t2 on t1.cid=t2.cid where t1.sid=t.sid and t2.cname='英语') 英语,
(select t1.score from score t1 left join course t2 on t1.cid=t2.cid where t1.sid=t.sid and t2.cname='物理') 物理,
count(*) 有效课程数,avg(t.score) 有效平均分 from score t group by t.sid order by avg(t.score) desc;

17.查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分

select cid,max(score),min(score) from score group by cid;

18.按各科平均成绩从低到高和及格率的百分数从高到低顺序

select t.cid,avg(t.score),100*((select count(*) from score t1 where t1.cid=t.cid and score >= 60)/
(select count(*) from score t2 where t.cid=t2.cid)) 及格率 from score t
group by t.cid order by avg(t.score) asc,及格率 desc;
或
select t.cid,avg(t.score),100*(sum(case when score >= 60 then 1 else 0 end)/count(*)) 及格率 from score t
group by t.cid order by avg(t.score) asc,及格率 desc;

19.查询如下课程平均成绩和及格率的百分数(用"1行"显示): 语文001,数学002,英语003,物理004

select (select avg(score) from score where cid=001) avg1,
100*((select count(*) from score where cid=001 and score >= 60)/(select count(*) from score where cid=001)) pass1,
(select avg(score) from score where cid=002) avg2,
100*((select count(*) from score where cid=002 and score >= 60)/(select count(*) from score where cid=002)) pass2,
(select avg(score) from score where cid=003) avg3,
100*((select count(*) from score where cid=003 and score >= 60)/(select count(*) from score where cid=003)) pass3,
(select avg(score) from score where cid=004) avg4,
100*((select count(*) from score where cid=004 and score >= 60)/(select count(*) from score where cid=004)) pass4 from dual;
或
select sum(case when cid=001 then score else 0 end)/sum(case when cid=001 then 1 else 0 end) avg1,
100*(sum(case when score >= 60 and cid=001 then 1 else 0 end)/sum(case when cid=001 then 1 else 0 end)) pass1,
sum(case when cid=002 then score else 0 end)/sum(case when cid=002 then 1 else 0 end) avg2,
100*(sum(case when score >= 60 and cid=002 then 1 else 0 end)/sum(case when cid=002 then 1 else 0 end)) pass2,
sum(case when cid=003 then score else 0 end)/sum(case when cid=003 then 1 else 0 end) avg2,
100*(sum(case when score >= 60 and cid=003 then 1 else 0 end)/sum(case when cid=003 then 1 else 0 end)) pass3,
sum(case when cid=004 then score else 0 end)/sum(case when cid=004 then 1 else 0 end) avg2,
100*(sum(case when score >= 60 and cid=004 then 1 else 0 end)/sum(case when cid=004 then 1 else 0 end)) pass4 from score;

20.查询不同老师所教不同课程平均分从高到低显示

select t2.tid,t2.tname,t1.cid,t1.cname,avg(t3.score) from course t1,teacher t2,score t3
where t1.tid=t2.tid and t1.cid=t3.cid group by t2.tid,t2.tname,t1.cid,t1.cname order by avg(t3.score) desc;

21.查询如下课程成绩第 3 名到第 6 名的学生成绩单:语文(001),数学(002),英语(003),物理(004) 
    [学生ID],[学生姓名],语文,数学,英语,物理,平均成绩

--mysql
select t1.sid,t1.sname,
(select a.score from score a where a.sid=t1.sid and a.cid=001) score1,
(select b.score from score b where b.sid=t1.sid and b.cid=002) score2,
(select c.score from score c where c.sid=t1.sid and c.cid=003) score3,
(select d.score from score d where d.sid=t1.sid and d.cid=004) score4,
(select avg(e.score) from score e where e.sid=t1.sid and e.cid in (001,002,003,004)) avg_all
from student t1 order by avg_all desc limit 2,4;--oracle
select sid,sname,score1,score2,score3,score4,avg_all from (
select rownum n,t.* from (
select t1.sid,t1.sname,
(select a.score from score a where a.sid=t1.sid and a.cid=001) score1,
(select b.score from score b where b.sid=t1.sid and b.cid=002) score2,
(select c.score from score c where c.sid=t1.sid and c.cid=003) score3,
(select d.score from score d where d.sid=t1.sid and d.cid=004) score4,
(select avg(e.score) from score e where e.sid=t1.sid and e.cid in (001,002,003,004)) avg_all
from student t1 order by avg_all desc) t ) where n between 3 and 6;

22.统计列各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]

select t2.cid 课程ID,t2.cname 课程名称,sum(case when t1.score > 85 then 1 else 0 end)  '[100-85]',
sum(case when t1.score between 70 and 85 then 1 else 0 end) '[85-70]',
sum(case when t1.score between 60 and 70 then 1 else 0 end) '[70-60]',
sum(case when t1.score <60  then 1 else 0 end) '[ <60]' from score t1,course t2
where t1.cid=t2.cid group by t2.cid,t2.cname;

23.查询学生平均成绩及其名次

select 1+(select count(distinct avg_score) from
(select avg(score) avg_score from score group by sid) t1 where t1.avg_score > t2.avg_score2) 名次,
t2.sid,t2.avg_score2 from
(select sid,avg(score) avg_score2 from score group by sid) t2 order by t2.avg_score2 desc;

24.查询各科成绩前三名的记录:(不考虑成绩并列情况)

--注意:MySQL中不可以用top N ,limit也不能直接用于in/any/all等子查询中
select t1.sid,t1.cid,t1.score from score t1 where exists
(select count(1) from score where t1.cid=score.cid and t1.score < score having count(1) < 3)
order by t1.cid,t1.score desc;
或
select t1.sid,t1.cid,t1.score from score t1 where
(select count(sid) from score where t1.cid=score.cid and t1.score < score) < 3
order by t1.cid,t1.score desc;

25.查询每门课程被选修的学生数

select cid,count(sid) from score group by cid;

26.查询出只选修了一门课程的全部学生的学号和姓名

select sid,sname from student where sid in
(select sid from score group by sid having count(cid) = 1);

27.查询男生、女生人数

select ssex,count(sid) from student group by ssex;

28.查询姓“张”的学生名单

select sid,sname from student where sname like '张%';
或
select sid,sname from student where substr(sname,1,1)='张';
--注意,在MySQL中substr( )函数是从1开始计算,而Oracle是从0开始

29.查询同名同姓学生名单,并统计同名同姓人数

select sname,count(sid) from student group by sname having count(sid)>1;

30.2001年出生的学生名单(注:student表中Sage列的类型是int)

select sid,sname from student where year(now())-2001+1=sage;

31.查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列

select cid,avg(score) from score group by cid order by avg(score),cid desc;

32.查询平均成绩大于85的所有学生的学号、姓名和平均成绩

select t1.sid,t1.sname,avg(t2.score) from student t1,score t2
where t1.sid=t2.sid group by t1.sid having avg(t2.score) >85;

33.查询课程名称为“物理”,且分数低于60的学生姓名和分数

select t1.sname,ifnull(t2.score,0) from student t1,score t2,course t3 where t1.sid=t2.sid and t2.cid=t3.cid
and t3.cname='物理' and t2.score < 60;

34.查询所有学生的选课情况

select t1.sid,t1.sname,t3.cid,t3.cname from student t1,score t2,course t3
where t1.sid=t2.sid and t2.cid=t3.cid;

35.查询任何一门课程成绩在70分以上的姓名、课程名称和分数

select t1.sname,t3.cname,t2.score from student t1,score t2,course t3
where t1.sid=t2.sid and t2.cid=t3.cid and t2.score >= 70;

36.查询不及格的课程,并按课程号从大到小排列

select cid from score where score < 60 order by cid desc;

37.查询课程编号为003且课程成绩在80分以上的学生的学号和姓名

select t1.sid,t1.sname from student t1,score t2 where t1.sid=t2.sid and t2.cid=003 and t2.score >= 80;

38.求选了课程的学生人数

select count(t.sid) from (
select sid from score group by sid having count(cid) > 0) t;
或
select count(distinct sid) from score;

39.查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩

select t1.sname,max(t2.score) from student t1,score t2,course t3,teacher t4
where t1.sid=t2.sid and t2.cid=t3.cid and t3.tid=t4.tid and t4.tname='叶平';

40.查询不同课程成绩相同的学生的学号、课程号、学生成绩

select t1.sid,t1.cid,t1.score,t2.sid,t2.cid,t2.score from score t1,score t2
where t1.cid <> t2.cid and t1.score=t2.score; 

41.查询每门功成绩最好的前两名

--与24题雷同
select t1.cid,t1.sid,t1.score from score t1 where
(select count(t2.sid) from score t2 where t1.cid=t2.cid and t1.score < t2.score) < 2
order by t1.cid,t1.score desc;

42.统计每门课程的学生选修人数(超过4人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

select cid,count(sid) from score group by cid having count(sid) > 4
order by count(sid) desc,cid asc;

43.检索至少选修两门课程的学生学号

select sid from score group by sid having count(cid) >=2;

44.查询全部学生都选修的课程的课程号和课程名

select t2.cid,t2.cname from score t1,course t2
where t1.cid=t2.cid group by t2.cid,t2.cname having count(t1.sid)=(select count(*) from student);

45.查询没学过“叶平”老师讲授的任一门课程的学生姓名

select t1.sname from student t1,score t2 where t1.sid=t2.sid and t2.cid in
(select t1.cid from course t1,teacher t2 where t1.tid=t2.tid and t2.tname='叶平');

46.查询两门以上不及格课程的同学的学号及其平均成绩

select sid,avg(ifnull(score,0)) from score where sid in (
select sid from score where score < 60 group by sid having count(cid) > 1) group by sid;

47.检索“004”课程分数小于60,按分数降序排列的同学学号

select sid from score where cid=004 and score < 60 order by score desc;

48.删除“002”同学的“001”课程的成绩

delete from score where sid=002 and cid=001;

以上代码是自己敲的,如有不合适或者更好的表达方式,麻烦留言,一起学习,谢谢!

笔试必备:48道SQL练习题(Oracle为主)相关推荐

  1. 50道mysql笔试题目及答案_50道SQL练习题及答案与详细分析

    网上流传较广的50道SQL训练,奋斗了不知道多久终于写完了.前18道题的难度依次递增,从19题开始的后半部分算是循环练习和额外function的附加练习,难度恢复到普通状态. 第9题非常难,我反正没有 ...

  2. 50道mysql笔试题目及答案_50道SQL练习题及答案与详细分析!!!

    数据表介绍 --1.学生表 Student(SId,Sname,Sage,Ssex) --SId 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别 --2.课程表 Course( ...

  3. 50道mysql笔试题目及答案_50道SQL练习题及答案(MySQL版)

    --1.查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数 SELECT st.*, class1, class2 FROM student s ...

  4. 来做做这 48 道 TypeScript 练习题

    下面所有题目来源于阿宝哥的 awesome-typescript 开源项目,可以点个star

  5. SQL与Oracle对比

    关键字: SQL,ORACLE 分类: 个人专区 密级: 公开 (评分:★★★★ , 回复: 16, 阅读: 641) »» 本人平时比较沉默,但朋友们都说我很疯狂-疯狂地学习.疯狂地工作.很久就 ...

  6. 50道数据库SQL练习题(深入理解各关键字的作用)

    目录 表结构 创建表 练习题 1.查询"001"课程比"002"课程成绩高的所有学生的学号 2.查询所有同学的学号.姓名.选课数.总成绩 3.查询平均成绩大于6 ...

  7. SQL练习题共50道附答案(MySQL)

    点击上方"框架师",选择"置顶公众号" 技术文章第一时间送达! 正文 引言: 最近真的超忙超忙,大家可以根据自己的查询需求更改数据,如果你sql很6那么当然最好 ...

  8. 入门Python必备100道练习题

    给大家整理了这份今天给大家分享100道Python练习题. 在此之前,先给大家推荐一个工具,是一个对 Python 运行原理进行可视化分析的工具,Python Tutor, 点击 Next 按钮就会根 ...

  9. 【SQL】Oracle SQL monitor

    [SQL]Oracle SQL monitor 第一章 被埋没的SQL优化利器--Oracle SQL monitor DBAplus社群 | 2015-11-26 07:00 转载声明:本文为DBA ...

  10. SQL Server,Oracle,DB2索引建立语句的对比

    http://database.51cto.com/art/201108/284540.htm SQL Server,Oracle,DB2索引建立语句的对比 2011-08-17 20:48 henr ...

最新文章

  1. Pig变飞机?AI为什么这么蠢 | Adversarial Attack
  2. 实现一个队列,使得push_rear(), pop_front() 和get_min()的时间复杂度为O(1)
  3. 电大计算机基础知识试题是什么样的,计算机应用基础知识(电大试题)
  4. Matlab 条形图实例
  5. 【nodejs笔记3】Express基本用法,和路由控制,和模板渲染ejs
  6. mysql 自己写数据库,自己写了一个简单的mysql数据库连接类
  7. mysql select 使用_mysql select简单用法
  8. 红外摄像机的功率究竟有多大
  9. 如何在SAP gateway系统配置路由到后台系统的OData服务路径
  10. Matlab——图像平移
  11. Python Pandas导出Hbase数据到dataframe
  12. 《朗读者》中那些让人受益终生的句子
  13. Android仿支付宝9.5芝麻信用分仪表盘
  14. GAN框架研究与思路整理
  15. 离线数仓建设及技术选型
  16. 一份黑椒牛肉饭引发的瞎想
  17. Android中textView自动识别电话号码,电子邮件,网址(自动加连接)
  18. wireshark看不到网口
  19. 打印A4纸图片需要多少像素和分辨率
  20. 12-06-微信小程序手写知识点

热门文章

  1. Redux:优点和缺点
  2. 债券基金的涨跌受什么影响
  3. 制作一份简单的网络地图(世博地图的配准和切割)
  4. Android手机应用开发之手机GPS定位
  5. 领导周末喊程序员修bug,程序员霸气回应:在下卖艺不是卖身!
  6. 为知笔记-艾宾浩斯遗忘曲线复习插件
  7. 高冷一字id_一个字网名 高冷一字id
  8. 实例化Servlet类异常404、500错误-解决方法
  9. Windows10系统封装
  10. 1008.判断闰年1.能被4整除而不能被100整除。(如2100年就不是闰年) 2.能被400整除。