--1.查询emp表,显示薪水大于2000,且工作类别是MANAGER的雇员信息

select * from emp
where sal > 2000
and job = 'MANAGER';

--2.查询emp表,显示年薪大于30000,工作类别不是MANAGER的雇员信息

select * from emp
where (sal+nvl(comm , 0 ))*12 > 30000
and job != 'MANAGER';

--3.查询emp表, 显示薪水在1500到3000之间,工作类别以“M”开头的雇员信息

select * from emp
where sal between 1500 and 3000
and job like 'M%';

--4.查询emp表,显示佣金为空并且部门号为20或30的雇员信息

select * from emp
where comm is null
and deptno in ( 20 , 30 );

--5.查询emp表,显示佣金不为空或者部门号为20的雇员信息,要求按照薪水降序排列

select * from emp
where comm is not null
or deptno = 20
order by sal desc ;

--6.查询emp表,显示年薪大于30000工作类别不是MANAGER,且部门号不是10或40的雇员信息,要求按照雇员姓名进行排列

select * from emp
where (sal+nvl(comm , 0 ))*12 > 30000
and job != 'MANAGER'
and deptno
not in ( 10 , 40 )
order by ename ;

--7.查询EMP、DEPT表,输出的列包含员工姓名、工资、部门编号、部门名称、部门地址.
select e.ename , e.sal , e.deptno , d.dname , d.loc
from emp e , dept d
where e.deptno = d.deptno ;

--8.使用自连接查询EMP表,输出的列包含员工姓名、主管姓名.
select e.ename employeename,m.ename managername
from emp e,emp m
where e.mgr = m.empno;

--9.在上一题的基础上,思考下为什么输出结果没有KING的信息? 如果要输出KING的信息,如何修改?
select e.ename employeename,m.ename managername
from emp e,emp m
where e.mgr = m.empno(+);

--10.使用左连接查询员工部门,包括没有员工的部门信息,输出列:部门编号、部门名称、位置。 --(左表为dept表,emp为右表)
select e.deptno , d.dname , d.loc
from dept d left join emp e
on e.deptno = d.deptno ;

--11.查询EMP表,输出每个部门的平均工资,并按部门编号降序排列.
select deptno , avg(sal)
from emp
group by deptno
order by deptno desc ;

--12.查询EMP表,输出每个职位的平均工资,按平均工资升序排列.
select job , avg(sal)
from emp
group by job
order by avg(sal) ;

--13.查询EMP表,输出每个部门的各个职位的平均工资,并按部门编号升序、平均工资降序排序。
select deptno , job , avg(sal)
from emp
group by deptno , job
order by deptno ,avg(sal) desc ;

--14.使用子查询,找出哪个部门下没有员工
select deptno
from dept
where deptno != all
(select deptno
from emp );

--15.使用子查询,找出那些工资低于所有部门的平均工资的员工
select ename , sal
from emp
where sal < all
(select avg(sal) de_sal
from emp
group by deptno) ;

--16.使用子查询,找出那些工资低于任意部门的平均工资的员工,比较一下与上一题输出的结果是否相同?
select ename , sal
from emp
where sal < any
(select avg(sal) de_sal
from emp
group by deptno) ;

--17.在EMP表中,增加一名员工,员工信息参照现有员工构造.
insert into emp
values (1111 , 'aa' , upper('salesman') , 7698 , sysdate , 8000 , 1000 , 40);

--18.员工SMITH部门调动到SALES部门,请编写SQL语句更新员工信息.
update emp
set deptno = '30'
where ename = 'SMITH';

--19.员工JAMES已经离职,请编写SQL语句更新数据库.
delete from emp
where ename = 'JAMES';

--20.用户执行delete from emp;语句删除了EMP表的记录,但没有提交,请问有办法恢复EMP原来的数据吗?
rollback

--21.得到平均工资大于2000的工作职种

select job , avg(sal)
from emp
group by job
having avg(sal)>2000;

--22.分部门得到工资大于2000的所有员工的平均工资,并且平均工资还要大于2500

方法一:

create or replace view emp_sal
as select ename , sal ,deptno
from emp
where sal>2000;
select deptno,avg(sal) dept_sal
from emp_sal
group by deptno
having avg(sal)>2500;

方法二:

select deptno , avg(sal) deptno
from emp
where sal >2000
group by deptno
having avg(sal)>2500;

--23.得到每个月工资总数最少的那个部门的部门编号,部门名称,部门位置 
select *
from
(
select d.deptno , d.dname , d.loc ,sum(sal)
from dept d , emp e
where e.deptno = d.deptno
group by d.deptno , d.dname , d.loc
order by sum(sal)
)
where rownum = 1 ;

--24.分部门得到平均工资等级为2级(等级表)的部门编号

select * from salgrade;

select deptno , avg(sal) dsal
from emp
group by deptno
having avg(sal) between 1201 and 1400;

--25.查找出部门10和部门20中,工资最高第3名到工资第5名的员工的员工名字,部门名字,部门位置
select *
from
(select ename , dname , loc ,sal , rownum rn
from(
select e.ename , d.dname , d.loc ,e.sal
from emp e , dept d
where e.deptno!=(30) and e.deptno = d.deptno
order by sal desc))
where rn between 3 and 5

--26.查找出收入(工资加上奖金),下级比自己上级还高的员工编号,员工名字,员工收入*/

select empno , ename , shouru
from
(
select a.ename , a.empno , a.sal+nvl(a.comm,0) shouru , b.sal+nvl(b.comm,0) shouru_2
from emp a , emp b
where b.empno = a.mgr
)
where shouru >shouru_2

--27.查找出职位和'MARTIN' 或者'SMITH'一样的员工的平均工资 */

select avg(sal)
from emp
where job = (select job from emp where ename = upper('smith'))
or
job = (select job from emp where ename = upper('martin'));

--28.查找出不属于任何部门的员工

select ename
from emp
where deptno is null;

--29.按部门统计员工数,查出员工数最多的部门的第二名到第五名(列出部门名字,部门位置)

select dname ,loc
from (select *
from (select rownum rn , deptno
from(select deptno , count(*)
from emp
group by deptno
order by count(*) desc))
where rn between 2 and 5) a ,dept b
where a.deptno = b.deptno

--30.查询出king所在部门的部门号\部门名称\部门人数

select denum , a.deptno ,dname
from
(select count(deptno) denum ,deptno
from emp
where deptno =
(select deptno
from emp
where ename = 'KING')
group by deptno) a ,dept b
where a.deptno = b.deptno ;

--31.查询出king所在部门的工作年限最大的员工名字

select ename
from(
select *
from emp
where deptno = (
select deptno
from emp
where ename = 'KING')
order by hiredate )
where rownum = 1

--32.查询出工资成本最高的部门的部门号和部门名称

select a.deptno ,b.dname
from
(select sum(sal),deptno
from emp
group by deptno
order by sum(sal) desc) a , dept b
where a.deptno = b.deptno
and rownum = 1 ;

--33.显示所有员工的姓名、工作和薪金,按工作的降序排序,若工作相同则按薪金排序.

select ename , job , sal
from emp
order by job desc , sal desc;

--34.显示所有员工的姓名、加入公司的年份和月份,按受雇日期所在月排序,若月份相同则将最早年份的员工排在最前面.

select ename , to_char( hiredate , 'YYYY MM' )
from emp
order by to_char( hiredate , 'MM' ) ,to_char (hiredate , 'dd');

--35.显示在一个月为30天的情况所有员工的日薪金,忽略余数

select ename , trunc(sal/30)
from emp ;

--36.找出在(任何年份的)2月受聘的所有员工。

select ename
from emp
where to_char ( hiredate , 'mm' ) = 2 ;

--37.对于每个员工,显示其加入公司的天数.

select ename , ceil(to_number (to_char(sysdate - hiredate))) hireday
from emp ;

--38.显示姓名字段的任何位置包含"A"的所有员工的姓名.

select ename
from emp
where ename like '%A%';

--39.以年月日的方式显示所有员工的服务年限.

--年
select ename , round(to_number (to_char(sysdate - hiredate))/365 ) year
from emp ;

--月
select ename , round(to_number (to_char(sysdate - hiredate))/30 ) month
from emp ;

--日
select ename , ceil(to_number (to_char(sysdate - hiredate)) ) month
from emp ;

--40.显示员工的姓名和受雇日期,根据其服务年限,将最老的员工排在最前面.

select ename , hiredate
from emp
order by hiredate

--41.

假设order_status2 表结构如下:
Name Type Nullable Default Comments
------------------------------- ------------------------- ------------ ------------ --------
ID INTEGER
STATUS CHAR(10) Y
LAST_MODIFIED DATE Y SYSDATE
MODIFIED_BY INTEGER Y
INITIALLY_CREATED DATE sysdate
TEST VARCHAR2(15) Y
TESTB VARCHAR2(10) Y 'testb'

--按表结构信息在数据中创建order_status2表

create table order_status3
(
ID INTEGER not null,
STATUS CHAR(10) ,
LAST_MODIFIED DATE default SYSDATE ,
MODIFIED_BY INTEGER ,
INITIALLY_CREATED DATE default sysdate not null ,
TEST VARCHAR2(15),
TESTB VARCHAR2(10) default 'testb');

--修改字段test, 使其不允许为空且给它赋予一个缺省的值testing;

alter table order_status2 modify(test default 'testing' not null);

--给order_status2表添加注释, 并为其每一个字段添加相应的注释.

comment on table order_status2 is '1';

comment on column order_status2.id is '1';
comment on column order_status2.status is '1';
comment on column order_status2.last_modified is '1';
comment on column order_status2.initially_created is '1';
comment on column order_status2.test is '1';
comment on column order_status2.testb is '1';
comment on column order_status2.MODIFIED_BY is '1';

--42.在41题创建的order_status2表的基础上完成以下练习:
--给order_status2表的status列添加一个check约束, 使其只允许输入Male和Female;

alter table order_status
add constraint order_status_check
check (status in ('male' , 'female'));

--为这个表的id列添加一个外键约束, 外键约束的表为employees, 对应的列为employee_id;

alter table order_status
add constraint order_status_f_id
foreign key (id) references employees(employee_id);

--43.对emp表中sal、comm进行加计算,并使用别名命令为员工的月总收入,同时展示出员工部门编号、员工姓名信息。

select ename , empno , sal+nvl(comm , 0) "月总收入"
from emp;

--44.使用连接符查询emp表中员工的姓名和工资,并以如下格式列出且字段名展示为 TOTAL INCOME:
--SMITH total income is XXXXX

select ename||' total income is'||sal as "TOTAL INCOME"
from emp ;

--45.使用distinct排重查询emp中的job类型

select distinct job from emp;

--46.从emp表中找出奖金高于 薪水60%的员工

select ename
from emp
where nvl(comm , 0 ) >nvl(comm , 0 )*0.6 ;

--47.找出部门10中所有经理(MANAGER)和部门20中所有办事员(CLERK)的详细资料。

select *
from emp
where deptno = 10 and job ='MANAGER' or deptno = 20 and job = 'CLERK';

--48.从emp和dept中联合查询,并将员工编号、姓名、职位、地址信息列出。

select e.empno , ename ,job , loc
from emp e , dept d
where e.deptno = d.deptno;

--49.统计各部门的薪水总和。

select sum(sal) ,deptno
from emp
group by deptno;

--50.找出部门10中所有理(MANAGER),部门20中所有办事员(CLERK)以及既不是经理又不是办事员但其薪水大于或等2000的所有员工的详细资料。

select *
from emp
where deptno = 10 and job ='MANAGER'
or deptno = 20 and job = 'CLERK'
or job <> all('MANAGER' , 'CLERK') and sal >= 2000;

--51.列出各种工作的最低工资。

select job , min(sal)
from emp
group by job;

--52.列出各个部门的MANAGER(经理)的最低薪水。

select min(sal)
from
(select *
from
(select sal, job , deptno
from emp)
where job = 'MANAGER')
group by deptno

--53.列出有奖金的员工的不同工作。
select ename , job , comm
from
(select *
from emp
where comm is not null);

--54.找出无奖金或奖金低于300的员工。

select * from emp where comm is null or nvl(comm , 0)<300;

--55.显示所有员工的姓名,并使姓名首字母大写。

select initcap(ename) from emp;

--56.显示正好为5个字符的员工的姓名。

select ename from emp where length(ename) = 5;

--57.显示不带有“R”的员工姓名。

select ename from emp where ename not like '%R%';

--58.列出薪水高于在部门30工作的所有员工的薪水的员工姓名和薪水。

select ename ,sal
from emp
where sal >all (select sal from emp where deptno = 30 ) ;

--59.列出在每个部门工作的员工数量、平均工资和平均服务期限。

select count(*) "员工人数" , avg(sal) "平均工资" ,avg (trunc(sysdate - hiredate)) "平均服务年限"
from emp
group by deptno ;

--60.列出从事同一种工作但属于不同部门的员工的一种组合。

select a.ename , b.ename
from emp a , emp b
where a.job = b. job and a.deptno <> b.deptno ;

--61.列出薪水比“SMITH”多的所有员工。
select ename
from emp
where sal > (select sal from emp where ename = 'SMITH');

--62.列出至少有一个员工的所有部门。

select distinct deptno
from emp
where deptno in (select deptno from emp );

--63.对于每个员工,显示其加入公司的天数、月数、年数。

select ename , trunc(sysdate - hiredate) "天数" ,
trunc(sysdate - hiredate)/30 "月数" , trunc(sysdate - hiredate)/365 "年"
from emp;

--64.对21中的天数、月数、年数取整显示。

select ename , trunc(sysdate - hiredate) "天数" ,
round(trunc(sysdate - hiredate)/30) "月数" , round(trunc(sysdate - hiredate)/365) "年"
from emp;

--65.找出在每年5月受聘的所有员工。

select ename
from emp
where to_char(hiredate,'mm') = 5 ;

--66.显示在一个月为30天的情况下所有员工的日薪水,取整。

select ename , round(sal/30) daysal
from emp;

--67.显示所有员工的姓名和加入公司的年份和月份,并将员工入职年月从低到高排序。

select ename , to_char(hiredate,'yyyy mm')
from emp
order by hiredate ;

--68.请查SMITH领导的薪水
select sal
from emp
where empno = (select mgr
from emp
where ename = 'SMITH');

--69.请查SMITH领导的薪水和所在的部门地址

select sal ,loc
from emp e , dept d
where empno = (select mgr
from emp
where ename = 'SMITH')
and e.deptno = d.deptno;

--70.请查SMITH领导的薪水和所在的部门地址 以及领导的薪水等级

select a.* , grade
from
(
select sal , loc
from emp ,dept
where empno=(select mgr
from emp
where ename = 'SMITH') and dept.deptno=
(select deptno
from emp
where ename = 'SMITH')) a ,salgrade
where a.sal
between losal and hisal
;
--或
select a.* , grade ,loc
from
(select b.sal
from emp a,emp b
where a.mgr=b.empno and a.ename ='SMITH') a ,
salgrade ,dept
where a.sal between losal and hisal
and deptno=(select b.deptno
from emp a,emp b
where a.mgr=b.empno and a.ename ='SMITH');

--71.查出SMITH的薪水等级

select grade
from salgrade ,emp
where ename ='SMITH'
and sal between losal and hisal;

--72.请查出SIMIH的薪水等级和他所在部门所在地

select grade , loc
from dept d, salgrade ,emp e
where ename ='SMITH' and e.deptno = d.deptno
and sal between losal and hisal;

--73.按照职位分组,求出每个职位的最大薪水

select max(sal)
from emp
group by job;

--74.
--I)求出每个部门中的每个职位的最大薪水

select max(sal),deptno , job
from emp
group by deptno ,job ;

--II)在薪水大于1000,并且职位不是MANAGER的员工中,求职哪个职位的平均薪水大于2000
select job
from emp
where sal>1000 and job <> 'MANAGER'
group by job
having avg(sal)>2000;

--75.列出SMITH的薪水和职位

select sal , job
from emp
where ename = 'SMITH';

--76.列出SMITH的部门地址和补贴和薪水等级(等级表salgrade)

select ename , loc , comm , grade
from emp e , dept d ,salgrade
where ename = 'SMITH' and e.deptno = d.deptno
and e.sal between losal and hisal

--77.列出薪金比"SMITH"多的所有员工
select ename
from emp
where sal>(
select sal
from emp
where ename = 'SMITH');

--78.列出所有员工的姓名及其直接上级的姓名

方法一:

select a.ename "直接上级", b.ename "员工姓名"
from emp a , emp b
where a.mgr = b.empno(+);

方法二:

select a.ename "直接上级", b.ename "员工姓名"
from emp a left outer join emp b
on a.mgr = b.empno(+);

--79.列出部门不是10,职位不是C开头的,薪资比公司平均薪资都高的员工名字

select ename
from emp
where deptno != 10 and job not like 'C%'
and sal>(select avg(sal) from emp );

--80.哪个部门下面没有员工

select deptno
from dept
where deptno <>all
(select deptno
from emp);

--81.谁的薪水比SMITH多,同时部门又是和SCOTT的部门相同

select ename
from emp
where sal > (select sal from emp where ename = 'SMITH')
and deptno = (select deptno from emp where ename ='SCOTT');

--82.列出薪资比每个部门每个职位的平均薪资还要高的员工

select ename
from emp
where sal >all(select avg(sal) from emp
group by deptno, job );

--83.列出至少有一个员工的所有部门

select distinct deptno
from emp;

--84.新员工入职,请新增一个用户

insert into emp(empno , ename , hiredate)
values(1111 , 'aaaa' , sysdate);

--85.SMITH的职位变更为SCOTT的职位

update emp
set job = (select job from emp where ename = 'SCOTT')
where ename = 'SMITH';

--86.SMITH离职,请删除该用户

delete from emp
where ename = 'SMITH';

Oracle练习详解的更多相关文章

  1. oracle 数据类型详解---日期型(转载)

    oracle 数据类型详解---日期型 oracle数据类型看起来非常简单,但用起来会发现有许多知识点,本文是我对ORACLE日期数据类型的一些整理,都是开发入门资料,与大家分享: 注:由于INTER ...

  2. oracle 序列 详解

    序列: 是oacle提供的用于产生一系列唯一数字的数据库对象. l  自动提供唯一的数值 l  共享对象 l  主要用于提供主键值 l  将序列值装入内存可以提高访问效率 创建序列: 1.  要有创建 ...

  3. oracle checkpoint 详解

    Oracle checkpoint详解 topcheckpoint扫盲 top什么是checkpoint 在数据库系统中,写日志和写数据文件是数据库中IO消耗最大的两种操作,在这两种操作中写数据文件属 ...

  4. Oracle数据字典详解

    学习笔记:oracle数据字典详解 --- 本文为TTT学习笔记,首先介绍数据字典及查看方法,然后分类总结各类数据字典的表和视图.然后列出一些附例.   数据字典系统表,保存在system表空间中. ...

  5. oracle rowid 详解

    oracle rowid详解 今天是2013-09-15,存储在数据库中的每一行数据都有一个地址,oracle使用rowid数据类型在存储地址.rowid有如下类别: 1)physical rowid ...

  6. Oracle索引详解

    Oracle索引详解(二) --索引分类   Oracle 提供了大量索引选项.知道在给定条件下使用哪个选项对于一个程序的性能来说非常重要.一个错误的选择可能会引发死锁,并导致数据库性能急剧下降或进程 ...

  7. Oracle内存详解之 Library cache 库缓冲

    Oracle内存详解之 Library cache 库缓冲 2017年11月09日 11:38:39 阅读数:410更多 个人分类: 体系结构 Library cache是Shared pool的一部 ...

  8. Oracle date 详解

    oracle 数据类型详解---日期型 oracle数据类型看起来非常简单,但用起来会发现有许多知识点,本文是我对ORACLE日期数据类型的一些整理,都是开发入门资料,与大家分享:注:由于INTERV ...

  9. 【Oracle】详解ORACLE中的trigger(触发器)

    本篇主要内容如下: 8.1 触发器类型 8.1.1 DML触发器 8.1.2 替代触发器 8.1.3 系统触发器 8.2 创建触发器 8.2.1 触发器触发次序 8.2.2 创建DML触发器 8.2. ...

  10. 【Oracle】详解Oracle中的序列

    序列: 是oacle提供的用于产生一系列唯一数字的数据库对象. 自动提供唯一的数值 共享对象 主要用于提供主键值 将序列值装入内存可以提高访问效率 创建序列: 1.  要有创建序列的权限 create ...

随机推荐

  1. 【ExtJS】FormPanel 布局(一)

    准备工作,布置一个最简单的Form,共5个组件,都为textfield. Ext.onReady(function(){ Ext.create('Ext.form.Panel', { width: 5 ...

  2. 想熟悉PostgreSQL?这篇就够了

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由angel_郁 发表于云+社区专栏 什么是PostgreSQL? PostgreSQL是自由的对象-关系型数据库服务器,在灵活的BSD ...

  3. Hibernate实体类编写规则和主键策略

    一.实体类的编写规则 1.属性要是私有的. 2.要有公开的setter和getter方法供外界访问和修改. 3.每一个实体类要有一个属性作为唯一值(一般都是使用对于数据表的主键). 4.建议数据类型不 ...

  4. 了解下C#异常时的输出

    Sample Code: try { string re = "1.1".Substring(1,4); } catch (Exception ex) { logger.Error ...

  5. Memcached 查询stats及各项状态解释

    一.两个最常用状态查询(掌握第一个就完全OK了) 1)查看状态:printf “stats\r\n” |nc 127.0.0.1 11211      2)模拟top命令查看状态:watch “ech ...

  6. bitbucket 源代码托管

    5个人以下可以免费使用,不限制仓库的数量; 国外的注册需要开启蓝灯FQ; 1.注册账号 maanshancss w1-g1@qq.com;创建仓库; 然后拷贝现有项目 然后提交 然后push; 2.写 ...

  7. Spring.Net IOC基本应用和在MVC4中的应用

    1.Spring.Net的IOC简单应用 新建一个解决方案添加一个控制台应用程序和一个业务层一个业务层的接口层,通过配置,让控制台应用程序调业务层的方法 1)新建如下图所示,BLL为业务层,通过Spr ...

  8. .netcore2.0 发布CentOS7

    1.一般在windows pc上使用vscode 开发好.netcore 程序如果需要发布到其他平台需要注意一些事情 首先需要明白2个概念:FDD(Framework-dependent deploy ...

  9. Rabbit的事务

    加入事务的方法: txSelect()  txCommit()  txRollback() 生产者: package com.kf.queueDemo.transactions; import jav ...

  10. Maven --- <distributionManagement>标签

    1.<distributionManagement>的作用: 负责管理构件的发布.这是一个环境变量    <downloadUrl> URL </downloadUrl& ...