
Oracle之人生何处欢(没得欢)练习题3(多表)
发布日期:2021-05-16 13:53:20
浏览次数:21
分类:精选文章
本文共 2627 字,大约阅读时间需要 8 分钟。
数据库学习指南:SQL Developer 实战
在学习 SQL Developer 时,实践是关键。以下是一些常见的查询练习,帮助您熟悉数据库操作。
表结构
我们创建了以下表结构:
-
学生表 (
student
):create table student(sno varchar2(10) primary key, sname varchar2(20), sage number(2), ssex varchar2(5));
- 插入数据:
insert into student values ('s001','张三',23,'男');
- 插入数据:
-
教师表 (
teacher
):create table teacher(tno varchar2(10) primary key, tname varchar2(20));
- 插入数据:
insert into teacher values ('t001', '刘阳');
- 插入数据:
-
课程表 (
course
):create table course(cno varchar2(10), cname varchar2(20), tno varchar2(20), constraint pk_course primary key (cno, tno));
- 插入数据:
insert into course values ('c001','J2SE','t002');
- 插入数据:
-
选课表 (
sc
):create table sc(sno varchar2(10), cno varchar2(10), score number(4,2), constraint pk_sc primary key (sno, cno));
- 插入数据:
insert into sc values ('s001','c001',78.9);
- 插入数据:
** 常见查询问题解答
查询c001课程比c002课程成绩高的所有学生的学号
select x.sno from sc xjoin sc y on x.sno = y.snowhere x.cno = 'c001'and y.cno = 'c002'and x.score > y.score;
查询平均成绩大于60分的同学的学号和平均成绩
select sno, avg(score) from scgroup by snohaving avg(score) > 60;
查询所有同学的学号、姓名、选课数、总成绩
select s.sno, s.sname, count(*), sum(score)from student sjoin sc on s.sno = sc.sno;
查询姓“刘”的老师的个数
select count(*) from teacher where tname like '刘%';
查询没学过“谌燕”老师课的同学的学号和姓名
select sno, sname from student swhere sno not in ( select distinct sno from sc where cno in ( select cno from course join teacher t on c.tno = t.tno where tname = '谌燕' ));
查询学过c001且c002课程的同学的学号和姓名
select s.sno, s.sname from ( select x.sno from sc x join sc y on x.sno = y.sno where x.cno = 'c001' and y.cno = 'c002' ) ejoin student s on e.sno = s.sno;
查询学过谌燕老师所教的所有课的同学的学号和姓名
select s.sno, s.sname from scjoin course c on sc.cno = c.cnojoin teacher t on c.tno = t.tnowhere t.tname = '谌燕'group by s.sno, s.snamehaving count(*) = (select count(*) from teacher t join course c on t.tno = c.tno where t.tname = '谌燕');
查询c002课程成绩比c001低的所有同学的学号和姓名
select sno, sname from sc ajoin sc b on a.sno = b.snowhere a.cno = 'c002'and b.cno = 'c001'and a.score < b.score;
查询所有课程成绩小于60的同学的学号和姓名
select sno, sname from studentwhere sno in ( select distinct sno from sc where sc.score < 60);
查询没有学全所有课的同学的学号和姓名
select sno, sname from studentwhere sno in ( select sno from sc group by sno having count(cno) < ( select count(distinct(cno)) from sc ));
** 验证与提交答案**
通过以上练习,您可以逐步掌握 SQL 查询技巧。在实际操作中,建议逐步测试每个查询,确保语法正确并得到预期结果。
发表评论
最新留言
第一次来,支持一个
[***.219.124.196]2025年05月11日 05时37分33秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
LibTorch框架学习
2023-01-31
libtorch组成讲解之ATen、c10、at、csrc
2023-01-31
libvirt TLS
2023-01-31
libvirtd tcp 方式远程连接配置步骤
2023-01-31
libvirt报错处理及解决
2023-01-31
License Server上找不到指定版本的XenApp License
2023-01-31
License授权
2023-01-31
LifecycleException: Protocol handler start failed & BindException: Address already in use: bind
2023-01-31
liferay 去掉 portlet:actionUrl 跳转时的message
2023-01-31
Liferay7 BPM门户开发之21: 理解消息总线(Message Bus)体系
2023-01-31
Light OJ 1005
2023-01-31
LightningChart 图表控件图库
2023-01-31
Lightning接口详解-ChatGPT4o作答
2023-01-31
LightOJ - 1074 Extended Traffic (SPFA+负环)
2023-01-31
LightOJ - 1077 How Many Points
2023-01-31
LightOJ - 1151概率dp+高斯消元
2023-01-31
lightoj 1102 - Problem Makes Problem
2023-01-31
LightOJ 1284 Lights inside 3D Grid(概率)
2023-01-31
LightOJ 1336 Sigma Function
2023-01-31
LightOJ1214 - Large Division(高精度取模 + 模板)
2023-01-31