题干:设有一数据库,包括四个表:学生表(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练习题的更多相关文章

  1. 【Java EE 学习 28 下】【Oracle面试题2道】【Oracle练习题3道】

    一.已知程序和数据 create table test1 (id int primary key, name ), money int); ,); ,); ,); ,); 要求根据下图写出相应的sql ...

  2. oracle练习题后15个

    31,32题更正: SQL> --31. 查询所有教师和同学的name.sex和birthday. SQL> select sname, ssex, sbirthday from stud ...

  3. Oracle练习题20~33

    20.查询score中选学多门课程的同学中分数为非最高分成绩的记录. 21. 查询成绩高于学号为“109”.课程号为“3-105”的成绩的所有记录. 22.查询和学号为108的同学同年出生的所有学生的 ...

  4. Oracle练习题(1~19)

    1. 查询Student表中的所有记录的Sname.Ssex和Class列. 2. 查询教师所有的单位即不重复的Depart列. 3. 查询Student表的所有记录. 4. 查询Score表中成绩在 ...

  5. Oracle.练习题

    2018-07-31 ---练习3 ---创建sporter表 create table sporter( sporterid ) constraint sport_id primary key, s ...

  6. oracle练习题 实验一

    实验一 练习1.请查询表DEPT中所有部门的情况. select * from dept; 练习2.查询表DEPT中的部门号.部门名称两个字段的所有信息. select deptno,dname fr ...

  7. SQL操作数据——SQL组成,查询基础语法,where,Oracle常用函数等

    SQL组成 DML数据操作语言 DCL数据控制语言 DQL数据查询语言 DDL数据定义语言 查询基础语法 记录筛选 where 子句 记录筛选 where 子句 实例练习 实例练习 Select语句中 ...

  8. 另一套Oracle SQL练习题,更新参考答案

    题干: create table student( sno ) primary key, sname ), sage ), ssex ) ); create table teacher( tno ) ...

  9. 一套oracle的练习题

    create table student( sno varchar2(10) primary key, sname varchar2(20), sage number(2), ssex varchar ...

随机推荐

  1. shell script 学习笔记-----shell变量

    1.在赋值语句name=value中不能存在空格,例如:name = value这样的形式会被认为是三个变量,因为本质上来说,脚本的内容就是传给shell程序的变量,而变量之间是通过空格区分的.如果想 ...

  2. 迭代加深搜索 codevs 2541 幂运算

    codevs 2541 幂运算  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description 从m开始,我们只需要6次运算就可以计算出 ...

  3. 合工大 OJ 1322 窗口

    窗口     Description 在某图形操作系统中,有N 个窗口,每个窗口都是一个两边与坐标轴分别平行的矩形区域.窗口的边界上的点也属于该窗口.窗口之间有层次的区别,在多于一个窗口重叠的区域里, ...

  4. HDU 4819 Mosaic --二维线段树(树套树)

    题意: 给一个矩阵,每次查询一个子矩阵内的最大最小值,然后更新子矩阵中心点为(Max+Min)/2. 解法: 由于是矩阵,且要求区间最大最小和更新单点,很容易想到二维的线段树,可是因为之前没写过二维的 ...

  5. EmberJS路由详解

    配置路由器,ember中是根据路径寻找与路由名相对应的模板和控制器的,比如当访问http://localhost:80/这个网址的时候,ember框架会自动寻找与路由名post相对应的PostTemp ...

  6. 【转】WCF与Web API 区别(应用场景)

    Web api  主要功能: 支持基于Http verb (GET, POST, PUT, DELETE)的CRUD (create, retrieve, update, delete)操作 请求的回 ...

  7. py变量

        1, python以数据为主 x=2,是给数据2开辟了个空间, X指向了2 y=x ,即y指向了2 x=5 ,x重新赋值 但是y依旧是原来的

  8. [tools]神器notepad++

    1,现象 notepad++编辑sh文件,放入linux后执行会有问题 2,解决: 2.1dos2unix转换文件 2,2 修改notepad++默认字符集 2,快捷键: ctrl+k 单行.多行注释 ...

  9. 关于那些难改的bug

    多年的测试经验中,经常发现有这么一种现象:总有些提了的bug不能顺利的被修复.这些bug往往有4个走向: 1.在被发现的版本中最终被解决,但中途花费较多周折. 2.有计划的在后续的版本中被解决. 3. ...

  10. MATLAB仿真总结

    MATLAB仿真过程中,编写MATLAB代码的时候犯了很多错误,做了很多蠢事.记录下自己犯错的点点滴滴,并引以为戒.使用MATLAB版本为2014a,以下内容如有不当还请指正. 1. 仿真开始前清理工 ...