oracle练习题
题干:设有一数据库,包括四个表:学生表(Student)、课程表(Course)、成绩表(Score)以及教师信息表(Teacher)。
建表后数据如下:
SQL> select * from student;
SNO SNAME SSEX SBIRTHDAY CLASS
--- -------- ---- ----------- -----
108 曾华 男 1977/9/1 95033
105 匡明 男 1975/10/2 95031
107 王丽 女 1976/1/23 95033
101 李军 男 1976/2/20 95033
109 王芳 女 1975/2/10 95031
103 陆君 男 1974/6/3 95031
6 rows selected SQL> select * from course;
CNO CNAME TNO
----- ---------- ---
3-105 计算机导论 825
3-245 操作系统 804
6-166 数字电路 856
9-888 高等数学 831 SQL> select * from score;
SNO CNO DEGREE
--- ----- ------
103 3-245 86.0
105 3-245 75.0
109 3-245 68.0
103 3-105 92.0
105 3-105 88.0
109 3-105 76.0
101 3-105 64.0
107 3-105 91.0
108 3-105 78.0
101 6-166 85.0
107 6-166 79.0
108 6-166 81.0
12 rows selected SQL> select * from course;
CNO CNAME TNO
----- ---------- ---
3-105 计算机导论 825
3-245 操作系统 804
6-166 数字电路 856
9-888 高等数学 831
以下为题目及解答:
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0
Connected as TEST1@ORCL SQL> --1、 查询Student表中的所有记录的Sname、Ssex和Class列。
SQL> select sname, ssex, class from student;
SNAME SSEX CLASS
-------- ---- -----
曾华 男 95033
匡明 男 95031
王丽 女 95033
李军 男 95033
王芳 女 95031
陆君 男 95031
6 rows selected SQL> --2、 查询教师所有的单位即不重复的Depart列。
SQL> select distinct depart from teacher;
DEPART
----------
电子工程系
计算机系 SQL> --3、 查询Student表的所有记录。
SQL> select * from student;
SNO SNAME SSEX SBIRTHDAY CLASS
--- -------- ---- ----------- -----
108 曾华 男 1977/9/1 95033
105 匡明 男 1975/10/2 95031
107 王丽 女 1976/1/23 95033
101 李军 男 1976/2/20 95033
109 王芳 女 1975/2/10 95031
103 陆君 男 1974/6/3 95031
6 rows selected SQL> --4、 查询Score表中成绩在60到80之间的所有记录。
SQL> select * from score;
SNO CNO DEGREE
--- ----- ------
103 3-245 86.0
105 3-245 75.0
109 3-245 68.0
103 3-105 92.0
105 3-105 88.0
109 3-105 76.0
101 3-105 64.0
107 3-105 91.0
108 3-105 78.0
101 6-166 85.0
107 6-166 79.0
108 6-166 81.0
12 rows selected SQL> select * from score where degree between 60 and 80;
SNO CNO DEGREE
--- ----- ------
105 3-245 75.0
109 3-245 68.0
109 3-105 76.0
101 3-105 64.0
108 3-105 78.0
107 6-166 79.0
6 rows selected SQL> --5、 查询Score表中成绩为85,86或88的记录。
SQL> select * from score where in (85, 86, 88);
select * from score where in (85, 86, 88)
ORA-00936: 缺失表达式 SQL> select * from score where degree in (85, 86, 88);
SNO CNO DEGREE
--- ----- ------
103 3-245 86.0
105 3-105 88.0
101 6-166 85.0 SQL> --6、 查询Student表中“95031”班或性别为“女”的同学记录。
SQL> select * from student where class = '' or ssex = '女';
SNO SNAME SSEX SBIRTHDAY CLASS
--- -------- ---- ----------- -----
105 匡明 男 1975/10/2 95031
107 王丽 女 1976/1/23 95033
109 王芳 女 1975/2/10 95031
103 陆君 男 1974/6/3 95031 SQL> --7、 以Class降序查询Student表的所有记录。
SQL> select * from student where class dest;
select * from student where class dest
ORA-00920: 无效的关系运算符 SQL> select * from student where class desc;
select * from student where class desc
ORA-00920: 无效的关系运算符 SQL> select * from student order by class desc;
SNO SNAME SSEX SBIRTHDAY CLASS
--- -------- ---- ----------- -----
108 曾华 男 1977/9/1 95033
107 王丽 女 1976/1/23 95033
101 李军 男 1976/2/20 95033
109 王芳 女 1975/2/10 95031
103 陆君 男 1974/6/3 95031
105 匡明 男 1975/10/2 95031
6 rows selected SQL> --8、 以Cno升序、Degree降序查询Score表的所有记录。
SQL> select * from score order by cno asc, degree desc;
SNO CNO DEGREE
--- ----- ------
103 3-105 92.0
107 3-105 91.0
105 3-105 88.0
108 3-105 78.0
109 3-105 76.0
101 3-105 64.0
103 3-245 86.0
105 3-245 75.0
109 3-245 68.0
101 6-166 85.0
108 6-166 81.0
107 6-166 79.0
12 rows selected SQL> --9、 查询“95031”班的学生人数。
SQL> select count(*) from student where class = '';
COUNT(*)
----------
3 SQL> --10、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)
SQL> select sno, cno from score where degree = max(degree);
select sno, cno from score where degree = max(degree)
ORA-00934: 此处不允许使用分组函数 SQL> select sno, cno from score where degree = max(select degree from score);
select sno, cno from score where degree = max(select degree from score)
ORA-00934: 此处不允许使用分组函数 SQL> select sno, cno from score where degree = (select max(degree) from score);
SNO CNO
--- -----
103 3-105 SQL> --11、 查询每门课的平均成绩。
SQL> select avg(all degree) from score group by cno;
AVG(ALLDEGREE)
--------------
76.33333333333
81.66666666666
81.5 SQL> select avg(all degree), cno from score group by cno;
AVG(ALLDEGREE) CNO
-------------- -----
76.33333333333 3-245
81.66666666666 6-166
81.5 3-105 SQL> --12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
SQL> select cno, avg(degree) from score where cno in (select cno from score group by cno having count(1) >= 5) and cno like '3%' group by cno;
CNO AVG(DEGREE)
----- -----------
3-105 81.5 SQL> --13、查询分数大于70,小于90的Sno列。
SQL> select sno degree > 70 and degree < 90;
select sno degree > 70 and degree < 90
ORA-00923: 未找到要求的 FROM 关键字 SQL> select sno from score where degree > 70 and degree < 90;
SNO
---
103
105
105
109
108
101
107
108
8 rows selected SQL> --14、查询所有学生的Sname、Cno和Degree列。
SQL> select st.sname, sc.cno, sc.degree from student st, score sc where st.sno = sc.sno;
SNAME CNO DEGREE
-------- ----- ------
陆君 3-245 86.0
匡明 3-245 75.0
王芳 3-245 68.0
陆君 3-105 92.0
匡明 3-105 88.0
王芳 3-105 76.0
李军 3-105 64.0
王丽 3-105 91.0
曾华 3-105 78.0
李军 6-166 85.0
王丽 6-166 79.0
曾华 6-166 81.0
12 rows selected SQL> --15、查询所有学生的Sno、Cname和Degree列。
SQL> select sc.sno, co.cname, sc.degree from course co, score sc where co.cno = sc.cno;
SNO CNAME DEGREE
--- ---------- ------
103 操作系统 86.0
105 操作系统 75.0
109 操作系统 68.0
103 计算机导论 92.0
105 计算机导论 88.0
109 计算机导论 76.0
101 计算机导论 64.0
107 计算机导论 91.0
108 计算机导论 78.0
101 数字电路 85.0
107 数字电路 79.0
108 数字电路 81.0
12 rows selected SQL> --16、查询所有学生的Sname、Cname和Degree列。
SQL> select st.sname, co.cname, sc.degree from student st, course co, score sc where st.sno = sc.sno, sc.cno = co.cno;
select st.sname, co.cname, sc.degree from student st, course co, score sc where st.sno = sc.sno, sc.cno = co.cno
ORA-00933: SQL 命令未正确结束 SQL> select st.sname, co.cname, sc.degree from student st, course co, score sc where st.sno = sc.sno and sc.cno = co.cno;
SNAME CNAME DEGREE
-------- ---------- ------
陆君 操作系统 86.0
匡明 操作系统 75.0
王芳 操作系统 68.0
陆君 计算机导论 92.0
匡明 计算机导论 88.0
王芳 计算机导论 76.0
李军 计算机导论 64.0
王丽 计算机导论 91.0
曾华 计算机导论 78.0
李军 数字电路 85.0
王丽 数字电路 79.0
曾华 数字电路 81.0
12 rows selected SQL> --17、 查询“95033”班学生的平均分。
SQL> select avg(sc.degree) from student st, score sc where st.sno = sc.sno and st.class = '';
AVG(SC.DEGREE)
--------------
79.66666666666 SQL> --18,现查询所有同学的Sno、Cno和rank列。
SQL> select sc.sno, sc.cno, gr.rank from score sc, grade gr where sc.degree between gr.low and gr.up;
SNO CNO RANK
--- ----- ----
101 3-105 D
109 3-245 D
105 3-245 C
109 3-105 C
108 3-105 C
107 6-166 C
108 6-166 B
101 6-166 B
103 3-245 B
105 3-105 B
107 3-105 A
103 3-105 A
12 rows selected SQL> --19、 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
SQL> select st.* from student st, score sc where (sc.sno = st.sno) and degree > (select degree from score where sno = '');
select st.* from student st, score sc where (sc.sno = st.sno) and degree > (select degree from score where sno = '')
ORA-01427: 单行子查询返回多个行 SQL> select st.* from student st, score sc where (sc.sno = st.sno) and (cno = '3-105') and degree > (select degree from score where sno = '');
select st.* from student st, score sc where (sc.sno = st.sno) and (cno = '3-105') and degree > (select degree from score where sno = '')
ORA-01427: 单行子查询返回多个行 SQL> select st.* from student st, score sc where (sc.sno = st.sno) and (cno = '3-105') and degree > all (select degree from score where sno = '');
SNO SNAME SSEX SBIRTHDAY CLASS
--- -------- ---- ----------- -----
108 曾华 男 1977/9/1 95033
105 匡明 男 1975/10/2 95031
107 王丽 女 1976/1/23 95033
103 陆君 男 1974/6/3 95031 SQL> --20、查询score中选学多门课程的同学中分数为非最高分成绩的记录。
SQL> selet * from score where (select sno from score group by sno having count(1)) > 1;
SQL>
SQL> --21、 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
SQL> select * from score where degree > (select degree from score where sno = '' and cno = '3-105');
SNO CNO DEGREE
--- ----- ------
103 3-245 86.0
103 3-105 92.0
105 3-105 88.0
107 3-105 91.0
108 3-105 78.0
101 6-166 85.0
107 6-166 79.0
108 6-166 81.0
8 rows selected SQL> --22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
SQL> select sno, sname sbirthday from student where extract(year from sbirthday) = (select extract(year from sbirthday) from student where sno = 108 );
SNO SBIRTHDAY
--- ---------
108 曾华 SQL> select sno, sname, sbirthday from student where extract(year from sbirthday) = (select extract(year from sbirthday) from student where sno = 108 );
SNO SNAME SBIRTHDAY
--- -------- -----------
108 曾华 1977/9/1 SQL> --23、查询“张旭“教师任课的学生成绩。
SQL> select * from score sc, teacher te, course co where sc.cno = co.cno and co.tno = te.tno and te.tname = '张旭';
SNO CNO DEGREE TNO TNAME TSEX TBIRTHDAY PROF DEPART CNO CNAME TNO
--- ----- ------ --- ----- ---- ----------- ------ ---------- ----- ---------- ---
101 6-166 85.0 856 张旭 男 1969/3/12 讲师 电子工程系 6-166 数字电路 856
107 6-166 79.0 856 张旭 男 1969/3/12 讲师 电子工程系 6-166 数字电路 856
108 6-166 81.0 856 张旭 男 1969/3/12 讲师 电子工程系 6-166 数字电路 856 SQL> select sc.* from score sc, teacher te, course co where sc.cno = co.cno and co.tno = te.tno and te.tname = '张旭';
SNO CNO DEGREE
--- ----- ------
101 6-166 85.0
107 6-166 79.0
108 6-166 81.0 SQL> select degree from score sc, teacher te, course co where sc.cno = co.cno and co.tno = te.tno and te.tname = '张旭';
DEGREE
------
85.0
79.0
81.0 SQL> --24、查询选修某课程的同学人数多于5人的教师姓名。
SQL> select tname from score sc, teacher te, course co where sc.cno = co.cno and co.tno = te.tno and (select sno from score group by sno having count(1)) > 5;
SQL> select tname from score sc, teacher te, course co where sc.cno = co.cno and co.tno = te.tno and te.cno = (select cno from score group by cno having count(1) > 5);
select tname from score sc, teacher te, course co where sc.cno = co.cno and co.tno = te.tno and te.cno = (select cno from score group by cno having count(1) > 5)
ORA-00904: "TE"."CNO": 标识符无效 SQL> select tname from score sc, teacher te, course co where sc.cno = co.cno and co.tno = te.tno and co.cno = (select cno from score group by cno having count(1) > 5);
TNAME
-----
王萍
王萍
王萍
王萍
王萍
王萍
6 rows selected SQL> select distinct tname from score sc, teacher te, course co where sc.cno = co.cno and co.tno = te.tno and co.cno = (select cno from score group by cno having count(1) > 5);
TNAME
-----
王萍 SQL> --25、查询95033班和95031班全体学生的记录。
SQL> select * from student where class in ('', '');
SNO SNAME SSEX SBIRTHDAY CLASS
--- -------- ---- ----------- -----
108 曾华 男 1977/9/1 95033
105 匡明 男 1975/10/2 95031
107 王丽 女 1976/1/23 95033
101 李军 男 1976/2/20 95033
109 王芳 女 1975/2/10 95031
103 陆君 男 1974/6/3 95031
6 rows selected SQL> --26、 查询存在有85分以上成绩的课程Cno.
SQL> select distinct cno from score where degree > 85;
CNO
-----
3-245
3-105 SQL> --27、查询出“计算机系“教师所教课程的成绩表。
SQL> select degree from score sc, teacher te, course co where sc.cno = co.cno and co.tno = te.tno and te.depart = '计算机系 ';
DEGREE
------
86.0
75.0
68.0
92.0
88.0
76.0
64.0
91.0
78.0
9 rows selected
SQL> --28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
SQL> select prof from teacher where depart = '计算机系' and prof not in (select prof from teacher where depart = '电子工 程系');
PROF
------
副教授 SQL> --29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序 。
SQL> --select
SQL> select cno, sno, degree from score where cno = '3-105' and degree > any (select degree from score where cno = '3-245') and order by degree;
select cno, sno, degree from score where cno = '3-105' and degree > any (select degree from score where cno = '3-245') and order by degree
ORA-00936: 缺失表达式 SQL> select cno, sno, degree from score where cno = '3-105' and degree > any (select degree from score where cno = '3-245') order by degree;
CNO SNO DEGREE
----- --- ------
3-105 109 76.0
3-105 108 78.0
3-105 105 88.0
3-105 107 91.0
3-105 103 92.0 SQL> --30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
SQL> select cno, sno, degree from score where cno = '3-105' and degree > all (select degree from score where cno = '3- ');
CNO SNO DEGREE
----- --- ------
3-105 105 88.0
3-105 107 91.0
3-105 103 92.0
oracle练习题的更多相关文章
- 【Java EE 学习 28 下】【Oracle面试题2道】【Oracle练习题3道】
一.已知程序和数据 create table test1 (id int primary key, name ), money int); ,); ,); ,); ,); 要求根据下图写出相应的sql ...
- oracle练习题后15个
31,32题更正: SQL> --31. 查询所有教师和同学的name.sex和birthday. SQL> select sname, ssex, sbirthday from stud ...
- Oracle练习题20~33
20.查询score中选学多门课程的同学中分数为非最高分成绩的记录. 21. 查询成绩高于学号为“109”.课程号为“3-105”的成绩的所有记录. 22.查询和学号为108的同学同年出生的所有学生的 ...
- Oracle练习题(1~19)
1. 查询Student表中的所有记录的Sname.Ssex和Class列. 2. 查询教师所有的单位即不重复的Depart列. 3. 查询Student表的所有记录. 4. 查询Score表中成绩在 ...
- Oracle.练习题
2018-07-31 ---练习3 ---创建sporter表 create table sporter( sporterid ) constraint sport_id primary key, s ...
- oracle练习题 实验一
实验一 练习1.请查询表DEPT中所有部门的情况. select * from dept; 练习2.查询表DEPT中的部门号.部门名称两个字段的所有信息. select deptno,dname fr ...
- SQL操作数据——SQL组成,查询基础语法,where,Oracle常用函数等
SQL组成 DML数据操作语言 DCL数据控制语言 DQL数据查询语言 DDL数据定义语言 查询基础语法 记录筛选 where 子句 记录筛选 where 子句 实例练习 实例练习 Select语句中 ...
- 另一套Oracle SQL练习题,更新参考答案
题干: create table student( sno ) primary key, sname ), sage ), ssex ) ); create table teacher( tno ) ...
- 一套oracle的练习题
create table student( sno varchar2(10) primary key, sname varchar2(20), sage number(2), ssex varchar ...
随机推荐
- WEB安全--CSRF防御
CSRF漏洞防御主要可以从三个层面进行,即服务端的防御.用户端的防御和安全设备的防御. 服务端的防御 目前服务器端防御CSRF攻击主要有5种策略(我知道的就这么多):验证HTTP Referer字段, ...
- jemter的使用(三)
前面的文章已经把接口请求.响应等前序工作做好,那么如何施加压力呢,看下面 1.点击线程组,设置线程属性,其中:线程数即并发用户数,ramp-up period是多长时间初始化上面的并发用户数,循环次数 ...
- SSM ( Spring 、 SpringMVC 和 Mybatis )配置详解
使用 SSM ( Spring . SpringMVC 和 Mybatis )已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没 ...
- YOU ARE MY SUNSHINE
/*you are sunshine, my only sunshine, you make me happy when skies are grey. you'll never know dear ...
- (转载)java多态(2)-------Java转型(向上或向下转型)
5.13.1 向上转型 我们在现实中常常这样说:这个人会唱歌.在这里,我们并不关心这个人是黑人还是白人,是成人还是小孩,也就是说我们更倾向于使用抽象概念“人”.再例如,麻雀是鸟类的一种(鸟类的子类), ...
- 创建服务factory和service方法的区别
factory方法返回的是对象,json或数组,也可以返回字符串类型的数据,但service方法只能返回数据或对象 创建服务有3种方法 $provide.provider('服务名',function ...
- AMAP
ViewController.m #import "ViewController.h" //地图显示需要的头文件 #import <MAMapKit/MAMapKit.h&g ...
- 【C#】WM 消息大全
消息名 消息值 说明 WM_CREATE 0x0001 应用程序创建一个窗口 WM_DESTROY 0x0002 一个窗口被销毁 WM_MOVE 0x0003 移动一个窗口 WM_SIZE 0x000 ...
- [转]VMware虚拟机上网络连接(network type)的三种模式--bridged、host-only、NAT
转自:http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/03/15/1985084.html VMWare提供了三种工作模式,它们是brid ...
- C语言 读取文件中特定数据
//读取文件数据 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> struct jia ...