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安全--XSS现代WAF规则探测及绕过技术
XSS现代WAF规则探测及绕过技术初始测试 1.使用无害的payload,类似<b>,<i>,<u>观察响应,判断应用程序是否被HTML编码,是否标签被过滤,是否过 ...
- 传统高斯模糊与优化算法(附完整C++代码)
高斯模糊(英语:Gaussian Blur),也叫高斯平滑,是在Adobe Photoshop.GIMP以及Paint.NET等图像处理软件中广泛使用的处理效果,通常用它来减少图像噪声以及降低细节层次 ...
- android源码在线查看
http://grepcode.com/project/repository.grepcode.com/java/ext/com.google.android/android/
- 用于json的 .ashx 小细节
public void ProcessRequest(HttpContext context) { switch (action) { case "attribute_field_valid ...
- 层叠上下文(The stacking context)
MDNThe stacking context 层叠上下文是HTML元素的三维概念,这些HTML元素在一条假想的相对于面向(电脑屏幕的)视窗或者网页的用户的z轴上延伸,HTML元素依据其自身属性按照优 ...
- DWZ集成的xhEditor编辑器浏览本地图片上传的设置
有关xhEditor的文件上传配置官方文档链接:http://i.hdu.edu.cn/dcp/dcp/comm/xheditor/demos/demo08.html 一.xhEditor图片上传的配 ...
- C# 与 Unity 同名函数
1,Random,直接使用Random会报错,要么使用UnityEngine.Random,要么使用System.Random
- 一款Android开源的下拉刷新动画
无意间在GitHub看到的,就Down了下来.但是作者是用AndroidStudio开发的,这边移动Eclipse供小伙伴们下载使用. 截图 这么好的东西因为字数不够不让分享,得了,贴段代码吧 pac ...
- 九度oj-1003-Java
题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开. 现在请计算A+B的结果,并以正常形式输出. 输入: 输入包含多组数据数据,每组数据占一行,由两 ...
- iOS中UIMenuController的使用
不知你有没有发现,在你的微信朋友中,长按一段文字(正文或者评论),会弹出这样子的玩意: 要想在你的view或者viewController中实现长按弹出菜单栏你必须要调用becomeFirstResp ...