oracle-sql入门练习及答案
1,通过命令行方式打开sqlplus
2,执行登录命令
sqlplus scott/scott@192.168.248.129/orcl
3进行sqlplus命令测试
Set time on
Set pagesize 数字
Set linesizes 数字
Set pause on/off
Describe命令 查看表或视图结构 也可以desc
Show 查看相应参数 如:show parameter,show user,show error等
Get命令 把一个sql脚本文件内容放入缓冲区
用系统编辑程序编辑命令
在SQL*PLUS中运行操作系统缺省的文本编辑程序(EDIT)
命令为:edit或者ed
将缓存中的sql语句保存到文件中:SAVE 文件名【具体路径】
查看缓存sql:list
执行文件中的sql语句:
START my.sql
@ my.sql
清空缓冲区:clear buffer
格式化命令:column
column sal format $999,999.00
如:
SQL> select ename, sal from emp
2
SQL> /
ENAME SAL
---------- ------------
SMITH $800.00
ALLEN $1,600.00
WARD $1,250.00
JONES $2,975.00
MARTIN $1,250.00
BLAKE $2,850.00
CLARK $2,450.00
重置为默认值:clear columns;
将屏幕上的内容写入到文件中
spool fileName
结束写入
spool off
3.Oracle查询
1) 查询EMP表,显示部门号为10的所有雇员的NAME 、JOB、SALARY和 DEPTNO,并以岗位降序、工资升序进行排序。
select ename,job,sal,deptno from emp where deptno=10 order by job desc , sal asc
2) 从EMP表中查询所有雇员的NAME和补助(SALARY+COMM),并处理null行。
select ename,(sal+nvl(comm,0)) from emp
3) 统计30号部门的总人数、人均工资、最高工资和最低工资。
select count(*) totle,avg(sal),max(sal),min(sal) from emp where deptno=30
4) 查询各个部门各个工种的平均工资、每个部门的平均工资和所有员工的平均工资。
select deptno,job,avg(sal) from emp group by rollup(deptno,job);
5) 查询各个部门中各个工种的平均工资、每个部门的平均工资、每个工种的平均工资和所有员工的平均工资。
select deptno,job,avg(sal) from emp group by cube(deptno,job);
6) 查询EMP,显示1999年之后参加工作人员的NAME、JOB和HIREDATE,并以工作日期的升序进行排序。
select * from emp where hiredate>'1-JAN-99';
7) 查询名字以“S”开始的所有职工:
select *from emp where ename like 'S%';
8) 让员工姓名右对齐显示
col ename jus left
9) 显示姓名只有5个字母组成的员工信息
select *from emp where ename like '_____'
或者
select *from emp where length(ename)=5
10) 查询在当月倒数第三天入职的员工信息
select *from emp where hiredate = last_day(hiredate)-2
2) 对各表中的数据进行不同条件的查询;
a)查询全体学生的学号和姓名
select sno,sname from student;
b)查询全体学生的详细记录
select *from student;
c)查询所有选修过课程的学生学号
select distinct sno from sc;
或者
select sno from student where sno in (select sno from sc);
d)查询考试有不及格的学生学号
select sno from sc where nvl(grade,0) <60;
e)查询不是信息系(IS)、计算机系(CS)的学生性别、年龄、系别
select ssex,sage,sdept from student where sdept not in('is','cs');
f)查询选修了4号课的学生学号和成绩,结果按成绩降序排列
select sc1.sno,sc1.grade from sc sc1,sc sc2 where sc2.cno=4 and sc1.sno=sc2.sno;
g)查询每个课程号和相应的选课人数
select cno,count(sno) from sc group by cno;
h)查询计算机系(CS)的学生姓名、年龄、系别
select sname,sage,sdept from student where sdept='cs'
i)查询年龄18-20岁的学生学号、姓名、系别、年龄;
select sno,sname,sage,sdept from student where sage between 18 and 20
j)查询姓刘的学生情况
select *from student where sname like '刘%'
k)查询既选修1号课程,又选修2号课程的学生学号和成绩
select sno,grade from sc where cno = 1 and cno in(select sno from sc where cno=2);
l)查询学生的姓名和出生年份(今年2003年)
select sname,2017-sage born from student;
m)查询没有成绩的学生学号和课程号
select s.sno,c.cno from student s,course c where (s.sno,c.cno) not in(select sno,cno from sc);
n)查询总成绩大于200分的学生学号
select sno from sc group by sno having sum(grade)>200;
o)查询每门课程不及格学生人数
select cno ,count(sno) from sc where nvl(grade,0)<60 group by cno【不及格分组】
p)查询不及格课程超过3门的学生学号
select sno from (select sno,cno from sc where nvl(grade,0)<60) r group by sno having count(r.cno)>3
q)查询年龄在10到19岁之间的学生信息
select *from student where sage>=10 and sage<=19;
select *from student where sage between 10 and 19;【between and 包含两头】
r)查询全体学生情况,按所在系升序排列,同一个系的学生按年龄降序排列
select *from student order by sdept asc,sage desc;
s)查询选了1号课程的学生平均成绩
select cno, avg(grade) avgGrade from sc where cno=1 group by cno;
t)查询选了3号课程的学生的最高分
select max(grade) from sc where cno=3;
u)查询每个同学的总成绩
select sno, count(grade) from sc group by sno;
复杂查询
内容和主要步骤:
1)实验一中的数据为基础
2) 对各表中的数据进行不同条件的连接查询和嵌套查询;
(1)?查询每个学生及其选课情况;
select * from sc;
(2)?查询每门课的间接先修课
select a.cno,b.cpno from course a,course b where a.cpno=b.cno;
(3)?将STUDENT,SC进行右连接
select *from student right join sc on student.sno=sc.sno;
(4)?查询有不及格的学生姓名和所在系
select sname,sdept from student where sno in (select distinct sno from sc where nvl(grade,0)<60);
(5)?查询所有成绩为优秀(大于90分)的学生姓名
select sname from student where sno in (select distinct sno from sc where nvl(grade,0)>90);
(6)?查询既选修了2号课程又选修了3号课程的学生姓名、学号;
select sname,sno from student where sno in (select sno from sc where cno = 3 and cno in(select sno from sc where cno=2));
(7)?查询和刘晨同一年龄的学生
select *from student where sname<>'刘晨' and sage in (select sage from student where sname='刘晨');
(8)?选修了课程名为“数据库”的学生姓名和年龄
select sname,sage from student where sno in(select sno from sc where cno in(select cno from course where cname='数据库'))
(9)?查询其他系比IS系任一学生年龄小的学生名单
select sname from student where sdept<>'is' and sage < (select max(sage) from student where sdept='is')
(10)?查询其他系中比IS系所有学生年龄都小的学生名单
select sname from student where sdept<>'is' and sage < (select min(sage) from student where sdept='is')
(11)?查询选修了全部课程的学生姓名
select sname from student where sno in(select sno from sc group by sno having count(cno) = (select count(cno)from sc))
(12)?查询计算机系学生及其性别是男的学生
select *from student where sdept='cs' and ssex='男'
(13)?查询选修课程1的学生集合和选修2号课程学生集合的差集
select *from sc where cno='1' and sno not in(select sno from sc where cno='2')
(14)?查询李丽同学不学的课程的课程号
select cno from course where cno not in(select cno from sc where sno in (select sno from student where sname='李丽'))
(15)?查询选修了3号课程的学生平均年龄
select avg(sage) from student where sno in(select sno from sc where cno=3)
(16)?求每门课程学生的平均成绩
select cno ,avg(grade) from sc group by cno;
(17)?统计每门课程的学生选修人数(超过3人的才统计)。
要求输出课程号和选修人数,结果按人数降序排列,若人数相同,按课程号升序排列
select cno ,count(sno) from sc group by cno having count(sno)>3 order by count(sno) desc,cno asc;
(18)?查询学号比刘晨大,而年龄比他小的学生姓名。
select a.sname from student a,(select sno,sname,sage from student where sname='刘晨') b where a.sno>all b.sno and a.sage<all b.sage;
(19)?求年龄大于女同学平均年龄的男同学姓名和年龄
select sname,sage from student where sage > (select avg(sage) from student where ssex='女');
(20)?求年龄大于所有女同学年龄的男同学姓名和年龄
select sname,sage from student where sage >all (select sage from student where ssex='女');
(21)?查询至少选修了95002选修的全部课程的学生号码
思想:首先查找学号为95002的同学的课程号集合,然后查找sc中所有学号在该集合中的选课信息,【此时该选课信息均为95002选过的课程信息】记为 msg
然后从msg中以学号进行分组,然后选出cno个数和95002选课个数相同的学号,该学号就是至少选修了95002选修的全部课程的学生学号
select sno from sc where sno<>95002
and
cno in (select cno from sc where sno=95002)
group by sno
having
count(cno)=(select count(cno) from sc where sno=95002)
(22)?查询95001和95002两个学生都选修的课程的信息
select cno from sc where sno =95001 and cno in (select cno from sc where sno=95002);
更新查询题目:
1) 应用INSERT,UPDATE,DELETE语句进行更新操作;
a) 插入如下学生记录(学号:95030,姓名:李莉,年龄:18)
insert into student (sno,sname,sage) values (95030,'李莉',18)
b) 插入如下选课记录(95030,1)
insert into sc (sno,cno) values(95030,1)
c) 计算机系学生年龄改成20
update student set sage=20 where sdept='cs';
d) 数学系所有学生成绩改成0
update sc set grade = 0
where sno in (select sno from student where sdept='ma')
e) 把低于总平均成绩的女同学成绩提高5分
update sc set grade = grade+5 where sno in (select sno from student where ssex='女' and grade < (select avg(grade) from sc))
f) 修改2号课程的成绩,若成绩小于75分提高5%,成绩大于75时提高%1
update sc set grade = grade+grade*0.05 where grade<75
update sc set grade=grade+grade*0.01 where grade>75
4)(两个语句实现,注意顺序)
g) 删除95030学生信息
delete student where sno=95030
h) 删除SC表中无成绩的记录
delete sc where grade is null;
i) 删除张娜的选课记录
delete sc where sno in (select sno from student where sname='张娜')
j) 删除数学系所有学生选课记录
delete sc where sno in (select sno from student where sdept='ma')
k) 删除不及格的学生选课记录
delete sc where grade <60
l) 查询每一门课程成绩都大于等于80分的学生学号、姓名和性别,把值送往另一个已经存在的基本表STU(SNO,SNAME,SSEX)中
create table stu as (select sno,sname,ssex from student where sno in(select sno from sc where sno not in(select distinct sno from sc where grade<80)))
m) 把所有学生学号和课程号连接追加到新表中
create table newTable as select * from student,sc;
n) 所有学生年龄增1
update student set sage=sage+1;
o) 统计3门以上课程不及格的学生把相应的学生姓名、系别追加到另外一个表中
create table failT as select sname,sdept from student where sno in(select sno from sc where grade<60 group by sno having count(cno)>=3)
oracle-sql入门练习及答案的更多相关文章
- 另一套Oracle SQL练习题,更新参考答案
题干: create table student( sno ) primary key, sname ), sage ), ssex ) ); create table teacher( tno ) ...
- 7.25实习培训日志-Oracle SQL(一)
Oracle SQL(一) 重点 尽量避免select *,影响性能,不直观. 慎用Distinct,会排序,影响性能,用exists 排序尽量利用索引,索引有序 索引列不要加函数,会使索引失效 外连 ...
- Oracle PL/SQL入门之慨述
Oracle PL/SQL入门之慨述 一.PL/SQL出现的目的 结构化查询语言(Structured Query Language,简称SQL)是用来访问关系型数据库一种通用语言,它属于第四代语言( ...
- Oracle编程入门经典 第12章 事务处理和并发控制
目录 12.1 什么是事务处理... 1 12.2 事务处理控制语句... 1 12.2.1 COMMIT处理... 2 12.2.2 RO ...
- [转]oracle学习入门系列之五内存结构、数据库结构、进程
原文地址:http://www.2cto.com/database/201505/399285.html 1 Oracle数据库结构 关于这个话题,网上一搜绝对一大把,更别提书籍上出现的了,还有很多大 ...
- oracle数据库入门
oracle 数据库入门. 1.数据 2.数据存储的地方:变量 数组 容器 (内存中),文件,数据库(文件) 3.数据库系统:sqlserver 2000 2005 2008 mysql 5 ...
- Oracle从入门到精通----学习笔记
书名:<Oracle从入门到精通:视频实战版>秦靖.刘存勇等编著 第4章 SQL基础 1.SQL语言分类 数据定义语言 --- DDL,Data Definition Language 数 ...
- 第87节:Java中的Bootstrap基础与SQL入门
第87节:Java中的Bootstrap基础与SQL入门 前言复习 什么是JQ? : write less do more 写更少的代码,做更多的事 找出所有兄弟: $("div" ...
- oracle sql 优化大全
转自: http://panshaobinsb.iteye.com/blog/1718233 http://yulimeander.blog.sohu.com/115850824.html 最近遇到了 ...
- Oracle从入门到精通 限定查询和排序查询的问题
视频课程:李兴华 Oracle从入门到精通视频课程 学习者:阳光罗诺 视频来源:51CTO学院 知识点 SQL语句的执行顺序 限定符号的使用. 具体内容: 如果想要对所选择的数据进行控制,就可以使 ...
随机推荐
- 每天一个JS 小demo之留言板。主要知识点:DOM方法的理解和运用
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"& ...
- Layered Windows窗口的半透明效果
介绍: Layered Windows是windows窗口中的一类,提供类似半透明的效果(阿尔法混合).半透明效果是字面上有能看出来的,但实际上根据MSND,该类型的窗口还能更好的支持非矩形的窗口,使 ...
- Java虚拟机14:Java对象大小、对象内存布局及锁状态变化
一个对象占多少字节? 关于对象的大小,对于C/C++来说,都是有sizeof函数可以直接获取的,但是Java似乎没有这样的方法.不过还好,在JDK1.5之后引入了Instrumentation类,这个 ...
- [转] .NET领域驱动设计—实践(穿过迷雾走向光明)
阅读目录 开篇介绍 1.1示例介绍 (OnlineExamination在线考试系统介绍) 1.2分析.建模 (对真实业务进行分析.模型化) 1.2.1 用例分析 (提取系统的所有功能需求) 1.3系 ...
- js 动态 key value
问题描述: 如果var mykey = 'a'; 我们试图 a = {mykey: 'b'} 来达到 a = { 'a': 'b'}的效果,但是mykey直接被作为键值,无法达到要求 解决办法: a ...
- angular js 和 dajango 标签{{}} 冲突
问题描述: 如果在django的模板中使用{{ }},不会被angularjs 识别. 解决办法: >1.5 的django中,将需要angularjs解释的{{expression}}放在 v ...
- js中的数组对象排序(方法sort()详细介绍)
定义和用法 sort() 方法用于对数组的元素进行排序. 语法 arrayObject.sort(sortby) 参数sortby:可选.规定排序顺序.必须是函数. 返回值 对数组的引用.请注意 ...
- 学容器必须懂 bridge 网络 - 每天5分钟玩转 Docker 容器技术(32)
上一节我们讨论了 none 和 host 类型的容器网络,本节学习应用最广泛也是默认的 bridge 网络. Docker 安装时会创建一个 命名为 docker0 的 linux bridge.如果 ...
- 关于MATLAB处理大数据坐标文件2017530
今天使用了所有特征并且用SVM测试数据 理由:SVM可以使用特征将测试集划分区域,比较单调.死板 结果:成绩很不理想,无疑又一次说明随机森林更适合大数据处理 第二次提交数据 用MATLAB运行11次运 ...
- 【Android Developers Training】 9. 覆盖于布局之上的Action Bar
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...