学生表 Student

字段值分别是 Sid ,Sname ,Sage ,Ssex

教师表 Teacher

字段值分别是 Tid ,Tname

课程表 Course

字段值分别是Cid ,Cname ,Tid

成绩表SC

字段值分别是 Sid ,Cid ,Score

--建表语句

drop table sc;

drop table course;

drop table teacher;

drop table student;

--创建学生表

create table student(

sid number(5) primary key,

sname varchar2(50),

sage date,

ssex varchar2(3)

);

--创建教师表

create table teacher(

tid number(5) primary key,

tname varchar2(50)

);

drop sequence t_seq;

create sequence t_seq;

--创建课程表

create table course(

cid number(5) primary key,

cname varchar2(50),

tid number(5) references teacher(tid)

);

--创建成绩表

create table sc(

sid number(5) references student(sid),

cid number(5) references course(cid),

score number(3)

);

--插入数据

insert into student values(1001,'张三',to_date('1990-10-10','yyyy-mm-dd'),'男');

insert into student values(1002,'李四',to_date('1981-10-10','yyyy-mm-dd'),'男');

insert into student values(1003,'王五',to_date('1981-11-10','yyyy-mm-dd'),'女');

insert into student values(1004,'赵六',to_date('1988-10-10','yyyy-mm-dd'),'男');

insert into student values(1005,'孙七',to_date('1989-01-10','yyyy-mm-dd'),'女');

insert into student values(1006,'周八',to_date('1990-10-10','yyyy-mm-dd'),'男');

insert into student values(1007,'张三',to_date('1990-06-10','yyyy-mm-dd'),'女');

insert into teacher values(t_seq.nextval,'叶平');

insert into teacher values(t_seq.nextval,'李老');

insert into teacher values(t_seq.nextval,'王老');

insert into teacher values(t_seq.nextval,'张老');

insert into teacher values(t_seq.nextval,'习老');

insert into course values(001,'企业管理',1);

insert into course values(002,'马克思',2);

insert into course values(003,'UML',3);

insert into course values(004,'数据库',4);

insert into course values(005,'英语',5);

insert into course values(006,'语文',1);

insert into course values(007,'数学',2);

insert into sc values(1001,006,80);

insert into sc values(1001,007,100);

insert into sc values(1001,001,50);

insert into sc values(1001,002,70);

insert into sc values(1001,003,80);

insert into sc values(1001,004,90);

insert into sc values(1001,005,80);

insert into sc values(1002,001,90);

insert into sc values(1002,004,100);

insert into sc values(1002,006,90);

insert into sc values(1002,007,80);

insert into sc values(1003,001,59);

insert into sc values(1003,002,70);

insert into sc values(1004,001,80);

insert into sc values(1004,002,70);

insert into sc values(1005,005,30);

insert into sc values(1005,001,50);

insert into sc values(1005,002,50);

insert into sc values(1005,003,100);

insert into sc values(1006,001,60);

insert into sc values(1007,001,100);

insert into sc values(1007,004,100);

insert into sc values(1007,006,90);

insert into sc values(1007,007,80);

一 单表条件查询

1、查询平均成绩大于60分的同学的学号和平均成绩;

select sid,avg(score) from sc group by sid having avg(score) >60;

2、查询姓“李”的老师的个数;

select count(*) as 个数 from teacher where tname like '李%';

3、查询男生、女生人数 ;

select ssex 性别,count(*) 人数 from student group by ssex

4、查询同名同姓学生名单,并统计同名人数 ;

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

5、1981年出生的学生名单(注:Student表中Sage列的类型是date) ;

select * from student where to_char(sage,'yyyy') = 1981;

6、查询姓“张”的学生名单 ;

select * from student where sname like '张%';

7、求选了课程的学生人数 ;

select count(*) from (select count(*) from sc group by sid);

8、检索至少选修两门课程的学生学号 ;

select sid from sc group by sid having count(*) >=2;

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

select sid from sc where cid =004 order by score desc;

10、删除“1002”同学的“001”课程的成绩 ;

delete from sc where sid = 1002 and cid = 001;

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

select cid as 课程ID,max(score) as 最高分,min(score) as 最低分 from sc group by cid;

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

select cid,count(*) from sc group by cid having count(*)>5 order by count(*) desc,cid

二 两表联查

13、查询“语文”比“数学”课程成绩高的所有学生的学号;

select a.sid from (select sc.sid,sc.score from course c,sc where

c.cid = sc.cid and c.cname='语文') a,(select sc.sid,sc.score from

course c,sc where c.cid = sc.cid and c.cname='数学') b

where a.score>b.score and a.sid = b.sid;

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

select s.sid,s.sname,count(*) as cnum,sum(sc.score) from student s

inner join sc on s.sid = sc.sid group by s.sid,s.sname;

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

select s.sid,s.sname from student s,sc where s.sid=sc.sid and sc.cid='001' and s.sid in (select s.sid from student s,sc where s.sid=sc.sid and sc.cid='002')

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

select a.sid,a.sname from (select s.sid,sc.score,s.sname from student

s,sc where s.sid = sc.sid and sc.cid=001) a ,(select s.sid,sc.score

from student s,sc where s.sid = sc.sid and sc.cid=002) b where

a.score >b.score and a.sid = b.sid;

17、查询所有课程成绩小于60分的同学的学号、姓名;

select sid,sname from student where sid not in(

select sid from sc where sc.score >60);

18、查询没有学全所有课的同学的学号、姓名;

select s.sid,s.sname,count(*) from student s,sc where s.sid = sc.sid

group bys.sid,s.sname having count(*) < (select count(*) from course);

19、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名;

select distinct s.sid,s.sname from student s,sc where s.sid = sc.sid and sc.cid in (select cid from sc where sid =1001);

20、查询至少学过学号为“1001”同学所有一门课的其他同学学号和姓名;

select distinct s.sid,s.sname from student s,sc where s.sid = sc.sid and sc.cid in (select cid from sc where sid =1001) and s.sid !=1001;

21、查询每门课程被选修的学生数 ;

select c.cname,count(*) as 学生数 from course c,sc where c.cid = sc.cid group by c.cname;

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

select sid,sname from student where sid in (select sid from sc group by sid having count(*)=1)

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

select c.cname,c.cid,avg(sc.score) from course c,sc where c.cid = sc.cid

group by c.cname,c.cid order by avg(sc.score) desc,c.cid desc

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

select s.sid,s.sname,avg(score) from student s,sc where s.sid=sc.sid group by s.sid,s.sname having avg(score)>85

25、查询不及格的课程,并按课程号从大到小排列 ;

select c.cname,sc.score,sc.sid from sc,course c where score<60 order by sc.cid desc

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

select sid,sname from student where sid in (select sid from sc where cid=003 and score >80)

27、查询各个课程及相应的选修人数 ;

select c.cname,count(*) from sc,course c where sc.cid = c.cid group by c.cname;

28、查询不同课程成绩相同的学生的学号、课程号、学生成绩 ;

select distinct a.sid,a.cid,a.score from sc a,sc b where a.score = b.score and a.cid !=b.cid order by a.score

29、查询全部学生都选修的课程的课程号和课程名 ;

select cid,cname from course where cid in (select cid from sc group by cid having count(*) =(select count(*) from course));

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

select sid,avg(score) from sc where sid in (select sid from sc

where score <60 group by sid having count(*)>2) group by sid;

31、查询学生平均成绩及其名次 ;

select a.*,rownum 排名 from (select s.sname,avg(score) from student s,sc where s.sid = sc.sid group by s.sname order by avg(score)desc) a

三 多表查询(3~4)

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

select sid,sname from student where sid not in

(select sid from sc,course c,teacher t where sc.cid = c.cid

and c.tid=t.tid and t.tname='叶平');

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

select sid,sname from student where sid in

(select sid from sc,course c,teacher t where sc.cid = c.cid

and c.tid=t.tid and t.tname='叶平');

34、删除学习“叶平”老师课的SC表记录

delete from sc where cid in (select c.cid from course c,teacher t,sc where c.tid = t.tid and c.cid=sc.cid and t.tname = '叶平');

35、查询课程名称为“数据库”,且分数低于60的学生姓名和分数 ;

select s.sname,sc.score from student s,sc,course c where s.sid = sc.sid and sc.cid = c.cid and c.cname ='数据库' and sc.score<60;

36、查询所有学生的选课情况;

select s.sid,s.sname,c.cid,c.cname from student s,sc,course c where s.sid = sc.sid and sc.cid = c.cidorder by sid

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

select s.sname,c.cname,sc.score from student s,sc,course c where s.sid = sc.sid and sc.cid = c.cid and sc.score>70 order by s.sid

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

select sname from student where sid not in(select sc.sid from teacher t,course c,sc where t.tid = c.cid and c.cid = sc.cid and t.tname ='叶平');

39、把“SC”表中“叶平”老师教的各个课的成绩分别更改为对应课程的平均成绩;

update sc sc1 set score = (select avg(score) from sc inner join course c on sc.cid = c.cid inner join teacher t on c.tid = t.tid group by c.cid having c.cid = sc1.cid) where (sc1.sid, sc1.cid) in (select sc.sid,sc.cid from sc inner join course c on sc.cid = c.cid inner join teacher t on c.tid = t.tid where t.tname='叶平');

40、查询不同老师所教不同课程平均分从高到低显示 ;

select t.tname,c.cname,round(avg(sc.score),2) avg from course c left join teacher t on c.tid = t.tid left join sc on c.cid= sc.cid group by c.cname,t.tname order by avg desc

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

select c.cid,c.cname,round(avg(sc.score),2) avg ,round((select count(*) from sc where cid = c.cid and score>=60)/(select count(*) from sc where cid = c.cid)*100,2)||'%' as path_rate from course c,sc where c.cid = sc.cid group by c.cname,c.cid order by avg, path_rate desc;

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

select s.sid,s.sname,sc.score from student s,sc,teacher t,course c where s.sid = sc.sid and sc.cid = c.cid and c.tid = t.tid and t.tname='叶平' and sc.score in (select max(sc.score) from student s,sc,teacher t,course c where s.sid = sc.sid and sc.cid = c.cid and c.tid = t.tid and t.tname='叶平');

四 exist

43、查询和“1002”号的同学学习的课程完全相同的其他同学学号和姓名;

select sid,sname from student s where sid !=1002 and

not exists(select * from sc where sc.sid = s.sid and cid not in

(select cid from sc where sid =1002))

and not exists(select * from sc where sid =1002 and cid not in

(select cid from sc where sc.sid=s.sid))

五 row_number() 函数

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

select * from (select c.cname,sc.score,s.*,row_number() over (partition by sc.cid order by score desc,s.sid) rn from sc,student s,course c where sc.sid = s.sid and sc.cid = c.cid) where rn <=3;

45、查询每门功成绩最好的前两名 ;

select * from (select c.cname,s.*,sc.score,row_number() over

(partition by sc.cid order by score desc,s.sid) rn from sc,student s,course c where sc.sid = s.sid and sc.cid = c.cid) where rn <=2;

六 case when

46、查询如下课程平均成绩和及格率的百分数(用"1行"显示): 企业管理(001),马克思(002),UML (003),数据库(004);

select

round(sum(case when cid =001 then score else 0 end)/sum(case when cid =001 then 1 else 0 end),2) as 企业管理平均分,

100*round(sum(case when cid =001 and score>=60 then 1 else 0 end)/sum(case when cid =001 then 1 else 0 end),4)||'%' as 企业管理及格率,

round(sum(case when cid =002 then score else 0 end)/sum(case when cid =002 then 1 else 0 end),2) as 马克思平均分,

100*round(sum(case when cid =002 and score>=60 then 1 else 0 end)/sum(case when cid =002 then 1 else 0 end),4)||'%' as 马克思及格率,

round(sum(case when cid =003 then score else 0 end)/sum(case when cid =003 then 1 else 0 end),2) as UML平均分,

100*round(sum(case when cid =003 and score>=60 then 1 else 0 end)/sum(case when cid =003 then 1 else 0 end),4)||'%' as UML及格率,

round(sum(case when cid =004 then score else 0 end)/sum(case when cid =004 then 1 else 0 end),2) as 数据库平均分,

100*round(sum(case when cid =004 and score>=60 then 1 else 0 end)/sum(case when cid =004 then 1 else 0 end),4)||'%' as 数据库及格率

from sc;

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

select c.cid as 课程ID,c.cname as 课程名,

sum(case when sc.score between 85 and 100 then 1 else 0 end) as "[100-85]",

sum(case when sc.score between 70 and 84 then 1 else 0 end) as "[84-70]",

sum(case when sc.score between 60 and 69 then 1 else 0 end) as "[69-60]",

sum(case when sc.score between 0 and 59 then 1 else 0 end) as "[<60]"

from course c,sc where c.cid = sc.cid group by c.cid,c.cname;

48、按成绩显示每个课程排名前三的学生姓名(按如下格式显示)

select cname 课程名,max(case when rn=1 then sname end) "1st",

max(case when rn=2 then sname end) "2st",max(case when rn=3 then sname end) "3st" from (select s.*,c.*,sc.*,row_number() over(partition by sc.cid order by sc.score) rn from student s

,sc,course c where s.sid = sc.sid and sc.cid = c.cid) group by cname

数据库习题(oracle)的更多相关文章

  1. sqlserver 2008R2数据库迁移oracle

    x项目需要,将以前的sqlserver数据库迁移的oracle数据库中,由于以前对oracle只是在DML语句的步骤,所以总结一下这次遇到的问题以及具体步骤 1,oracle新建数据库 新建Oracl ...

  2. 初识 数据库及Oracle数据库

    一.数据库基本概念二.数据库举例三.Oracle特点四.Oracle版本五.安装Oracle注意事项六.SQL简介七.Select语句 一.数据库基本概念 数据库(Database,DB)数据库管理系 ...

  3. oracle监听,数据库,oracle服务器的启动和停用

    一.oracle监听,数据库,oracle服务器的启动和停用 使用root用户登录:切换用户su – root/root操作系统,一般只有root用户才有权限去操作系统中安装的oracle数据库服务器 ...

  4. 数据库之Oracle的介绍与使用20180620

    /*******************************************************************************************/ 一.orac ...

  5. Microsoft-PetSop4.0(宠物商店)-数据库设计-Oracle

    ylbtech-DatabaseDesgin:Microsoft-PetSop4.0(宠物商店)-数据库设计-Oracle DatabaseName:PetShop(宠物商店) Model:宠物商店网 ...

  6. 数据库之Oracle

    数据库之Oracle 一. 用户的管理 1. 用户就是好比公司的某个人,而权限是这个人能在公司做什么,他的角色就是说明他的职位. 2. 用户的权限分为: 系统权限:对别的用户的管理操作. 对象权限:对 ...

  7. Oracle数据库之Oracle的下载与安装

    二.Oracle 的下载与安装 2.1.Oracle 简介 Oracle 公司是全球最大的信息管理软件及服务供应商,成立于 1977 年,主要的业务是推动电子商务平台的搭建.Oracle 公司有自己的 ...

  8. Linux下安装Oracle后重启无法登录数据库ORA-01034:ORACLE not available

    Linux下安装了数据库,安装完成后可以用,今天启动就不能用了,提示Oracle not available,后来查找资料,据说是oracle服务没有打开.如下方式可以解决问题. [root@root ...

  9. 数据库之Oracle优化技巧(一)

    数据库之Oracle优化技巧(一) 1.where子句中的连接顺序 在Oracle数据库中,where子句的执行顺序是自下而上进行解析,根据这个原理,表之间的连接必须写在其他where条件之前,那些可 ...

  10. Python操作数据库类 Oracle、Sqlserver、PostgreSQL

    我在工作中经常使用Python,特点很明显,轻量,效率还不错,尤其在维护或者自动化方面. 下面是我使用到的访问数据库(Oracle.Sqlserver.PostgreSQL)的公共类. 一.Oracl ...

随机推荐

  1. Javascript事件绑定及深入

    由于开学后的编程实验课,接触了海量字符换搜索的实验,所以好几天没有学习JS课程了,今天继续学习事件绑定. 传统事件绑定存在一些问题,如:同名事件函数都执行,第二个函数会覆盖第一个. 下面我们以事件切换 ...

  2. C语言程序的三种基本结构

    1.程序结构:在C语言程序中,一共有三种程序结构:顺序结构.选择结构(分支结构).循环结构: 顺序结构:从头到尾一句接着一句的执行下来,直到执行完最后一句: 选择结构:到某个节点后,会根据一次判断的结 ...

  3. Ionic2中集成腾讯Bugly之自定义插件

    Ionic2混合开发,入坑系列:Ionic2中集成腾讯Bugly之自定义插件 1.编写Bugly.js代码 var exec = require('cordova/exec'); module.exp ...

  4. 每天一个linux命令(53)--ps命令

    要毁掉一天,从早上开始. Linux中的ps命令是 process status 的缩写.ps 命令用来列出系统中当前运行的那些进程.ps 命令列出的是当前那些进程的快照,就是执行ps 命令的那个时刻 ...

  5. 细谈position属性:static、fixed、relative与absolute

    学习WEB有些时日了,对DOM中的定位概念有些模糊,特地花了一个下午的时间搜资料.整理写下这篇随笔. 首先,我们要清楚一个概念:文档流. 简单的讲,就是窗体自上而下分成一行一行,并在每行中按照从左到右 ...

  6. Servlet的监听

    Servlet监听 在<Servlet和Jsp>中我们使用了ServletConfig获取Servlet的初始配置,用ServletContext来获取整个Web应用的初始配置,但如果需要 ...

  7. enum类型的本质(转)

    原地址:http://www.cppblog.com/chemz/archive/2007/06/05/25578.html 至从C语言开始enum类型就被作为用户自定义分类有限集合常量的方法被引入到 ...

  8. Python第五章__模块介绍,常用内置模块

    Python第五章__模块介绍,常用内置模块 欢迎加入Linux_Python学习群  群号:478616847 目录: 模块与导入介绍 包的介绍 time &datetime模块 rando ...

  9. Dollar Dayz poj3181

    http://poj.org/problem?id=3181 这个题目一开始就能看出来是个dp问题,但是我并没有一开始就看出来是一个完全背包为题,只是想着根据以前的方法,这个问题应该是可以找到规律的, ...

  10. jquery.zclip.js 不起作用

    最近有用到复制的这个功能,选用jQuery的插件,正常使用的时候,没有任何问题: $('#copy_mobile_watch_address').zclip({ path: '/dist/plugin ...