版权声明:JAVA https://blog.csdn.net/weixin_43190126/article/details/85257276
将表导入数据库
/*
Navicat Oracle Data Transfer
Oracle Client Version : 10.2.0.5.0
Source Server : _1521_ORCL
Source Server Version : 100200
Source Host : :1521
Source Schema : SCOTT
Target Server Type : ORACLE
Target Server Version : 100200
File Encoding : 65001
Date: 2018-12-24 14:01:42
*/
-- ----------------------------
-- Table structure for BONUS
-- ----------------------------
DROP TABLE "SCOTT"."BONUS";
CREATE TABLE "SCOTT"."BONUS" (
"ENAME" VARCHAR2(10 BYTE) NULL ,
"JOB" VARCHAR2(9 BYTE) NULL ,
"SAL" NUMBER NULL ,
"COMM" NUMBER NULL
)
LOGGING
NOCOMPRESS
NOCACHE
;
-- ----------------------------
-- Records of BONUS
-- ----------------------------
/*
Navicat Oracle Data Transfer
Oracle Client Version : 10.2.0.5.0
Source Server : _1521_ORCL
Source Server Version : 100200
Source Host : :1521
Source Schema : SCOTT
Target Server Type : ORACLE
Target Server Version : 100200
File Encoding : 65001
Date: 2018-12-24 14:01:50
*/
-- ----------------------------
-- Table structure for DEPT
-- ----------------------------
DROP TABLE "SCOTT"."DEPT";
CREATE TABLE "SCOTT"."DEPT" (
"DEPTNO" NUMBER(2) NOT NULL ,
"DNAME" VARCHAR2(14 BYTE) NULL ,
"LOC" VARCHAR2(13 BYTE) NULL
)
LOGGING
NOCOMPRESS
NOCACHE
;
-- ----------------------------
-- Records of DEPT
-- ----------------------------
INSERT INTO "SCOTT"."DEPT" VALUES ('10', 'ACCOUNTING', 'NEW YORK');
INSERT INTO "SCOTT"."DEPT" VALUES ('20', 'RESEARCH', 'DALLAS');
INSERT INTO "SCOTT"."DEPT" VALUES ('30', 'SALES', 'CHICAGO');
INSERT INTO "SCOTT"."DEPT" VALUES ('40', 'OPERATIONS', 'BOSTON');
-- ----------------------------
-- Indexes structure for table DEPT
-- ----------------------------
-- ----------------------------
-- Primary Key structure for table DEPT
-- ----------------------------
ALTER TABLE "SCOTT"."DEPT" ADD PRIMARY KEY ("DEPTNO");
/*
Navicat Oracle Data Transfer
Oracle Client Version : 10.2.0.5.0
Source Server : _1521_ORCL
Source Server Version : 100200
Source Host : :1521
Source Schema : SCOTT
Target Server Type : ORACLE
Target Server Version : 100200
File Encoding : 65001
Date: 2018-12-24 14:01:56
*/
-- ----------------------------
-- Table structure for EMP
-- ----------------------------
DROP TABLE "SCOTT"."EMP";
CREATE TABLE "SCOTT"."EMP" (
"EMPNO" NUMBER(4) NOT NULL ,
"ENAME" VARCHAR2(10 BYTE) NULL ,
"JOB" VARCHAR2(9 BYTE) NULL ,
"MGR" NUMBER(4) NULL ,
"HIREDATE" DATE NULL ,
"SAL" NUMBER(7,2) NULL ,
"COMM" NUMBER(7,2) NULL ,
"DEPTNO" NUMBER(2) NULL
)
LOGGING
NOCOMPRESS
NOCACHE
;
-- ----------------------------
-- Records of EMP
-- ----------------------------
INSERT INTO "SCOTT"."EMP" VALUES ('7369', 'SMITH', 'CLERK', '7902', TO_DATE('1980-12-17 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '800', null, '20');
INSERT INTO "SCOTT"."EMP" VALUES ('7499', 'ALLEN', 'SALESMAN', '7698', TO_DATE('1981-02-20 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '1600', '300', '30');
INSERT INTO "SCOTT"."EMP" VALUES ('7521', 'WARD', 'SALESMAN', '7698', TO_DATE('1981-02-22 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '1250', '500', '30');
INSERT INTO "SCOTT"."EMP" VALUES ('7566', 'JONES', 'MANAGER', '7839', TO_DATE('1981-04-02 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '2975', null, '20');
INSERT INTO "SCOTT"."EMP" VALUES ('7654', 'MARTIN', 'SALESMAN', '7698', TO_DATE('1981-09-28 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '1250', '1400', '30');
INSERT INTO "SCOTT"."EMP" VALUES ('7698', 'BLAKE', 'MANAGER', '7839', TO_DATE('1981-05-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '2850', null, '30');
INSERT INTO "SCOTT"."EMP" VALUES ('7782', 'CLARK', 'MANAGER', '7839', TO_DATE('1981-06-09 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '2450', null, '10');
INSERT INTO "SCOTT"."EMP" VALUES ('7788', 'SCOTT', 'ANALYST', '7566', TO_DATE('1987-04-19 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '3000', null, '20');
INSERT INTO "SCOTT"."EMP" VALUES ('7839', 'KING', 'PRESIDENT', null, TO_DATE('1981-11-17 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '5000', null, '10');
INSERT INTO "SCOTT"."EMP" VALUES ('7844', 'TURNER', 'SALESMAN', '7698', TO_DATE('1981-09-08 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '1500', '0', '30');
INSERT INTO "SCOTT"."EMP" VALUES ('7876', 'ADAMS', 'CLERK', '7788', TO_DATE('1987-05-23 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '1100', null, '20');
INSERT INTO "SCOTT"."EMP" VALUES ('7900', 'JAMES', 'CLERK', '7698', TO_DATE('1981-12-03 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '950', null, '30');
INSERT INTO "SCOTT"."EMP" VALUES ('7902', 'FORD', 'ANALYST', '7566', TO_DATE('1981-12-03 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '3000', null, '20');
INSERT INTO "SCOTT"."EMP" VALUES ('7934', 'MILLER', 'CLERK', '7782', TO_DATE('1982-01-23 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '1300', null, '10');
-- ----------------------------
-- Indexes structure for table EMP
-- ----------------------------
-- ----------------------------
-- Checks structure for table EMP
-- ----------------------------
ALTER TABLE "SCOTT"."EMP" ADD CHECK ("EMPNO" IS NOT NULL);
-- ----------------------------
-- Primary Key structure for table EMP
-- ----------------------------
ALTER TABLE "SCOTT"."EMP" ADD PRIMARY KEY ("EMPNO");
-- ----------------------------
-- Foreign Key structure for table "SCOTT"."EMP"
-- ----------------------------
ALTER TABLE "SCOTT"."EMP" ADD FOREIGN KEY ("DEPTNO") REFERENCES "SCOTT"."DEPT" ("DEPTNO");
/*
Navicat Oracle Data Transfer
Oracle Client Version : 10.2.0.5.0
Source Server : _1521_ORCL
Source Server Version : 100200
Source Host : :1521
Source Schema : SCOTT
Target Server Type : ORACLE
Target Server Version : 100200
File Encoding : 65001
Date: 2018-12-24 14:02:01
*/
-- ----------------------------
-- Table structure for SALGRADE
-- ----------------------------
DROP TABLE "SCOTT"."SALGRADE";
CREATE TABLE "SCOTT"."SALGRADE" (
"GRADE" NUMBER NULL ,
"LOSAL" NUMBER NULL ,
"HISAL" NUMBER NULL
)
LOGGING
NOCOMPRESS
NOCACHE
;
-- ----------------------------
-- Records of SALGRADE
-- ----------------------------
INSERT INTO "SCOTT"."SALGRADE" VALUES ('1', '700', '1200');
INSERT INTO "SCOTT"."SALGRADE" VALUES ('2', '1201', '1400');
INSERT INTO "SCOTT"."SALGRADE" VALUES ('3', '1401', '2000');
INSERT INTO "SCOTT"."SALGRADE" VALUES ('4', '2001', '3000');
INSERT INTO "SCOTT"."SALGRADE" VALUES ('5', '3001', '9999');
下面是一些练习
conn scott/tiger;
dept表
DEPTNO NOT NULL NUMBER(2) 部门编号
DNAME VARCHAR2(14) 部门名称
LOC VARCHAR2(13) 部门所在位置
emp表
EMPNO NOT NULL NUMBER(4) 雇员编号
ENAME VARCHAR2(10) 雇员姓名
JOB VARCHAR2(9) 雇员职位
MGR NUMBER(4) 对应领导编号
HIREDATE DATE 雇用日期
SAL NUMBER(7,2) 基本工资
COMM NUMBER(7,2) 奖金,佣金
DEPTNO NUMBER(2) 部门编号
salgrade表
GRADE NUMBER 工资等级
LOSAL NUMBER 此等级最高工资
HISAL NUMBER 此等级最高工资
bonus表
ENAME VARCHAR2(10) 雇员姓名
JOB VARCHAR2(9) 雇员职位
SAL NUMBER 雇员工资
COMM NUMBER 雇员奖金
select * from dept;
select * from emp;
select * from salgrade;
select * from bonus;
---1、显示所有部门名
select dname as 部门名
from dept;
--2、显示所有雇员名及其全年收入(工资+补助),并指定列别名“年收入”
--如果 eExpression1 的计算结果为 null 值,则 NVL( ) 返回 eExpression2。如果 eExpression1 的计算结果不是 null 值,
--则返回 eExpression1。eExpression1 和 eExpression2 可以是任意一种数据类型。如果 eExpression1 与 eExpression2 的结果皆为 null 值,
--则 NVL( ) 返回 .NULL.。
select ename,sal+nvl(comm,0) 年收入
from emp;
--3、显示存在雇员的所有部门号
select deptno
from emp
group by deptno;
--4、显示工资超过2850的雇员名和工资
select ename,sal
from emp
where sal>2850;
--5、显示工资不在1500到2850之间的所有雇员名及工资
select ename,sal
from emp
where sal not between 1500 and 2850;
--6、显示雇员代码为7566的雇员名及所在部门号
select ename,deptno
from emp
where empno='7566' ;
--7、显示部门代码为10和30中工资超过1500的雇员名及工资
select ename,sal,deptno
from emp
where sal > 1500
and deptno in(10,30);
--8、显示无管理者的雇员名及岗位
select ename,job
from emp
where mgr is null;
--9、显示所有雇员的平均工资、总计工资、最高工资、最低工资
select avg(sal) as "平均工资",sum(sal) as "总计工资",
max(sal) as "最高工资", min(sal) as "最低工资"
from emp;
--10.显示每种岗位的雇员总数、平均工资
select job,count(empno) as "雇员总数",
avg(sal) as "平均工资"
from emp
group by job;
--11.显示雇员总数,以及获得补助的雇员数
select count(*)as "雇员总数", count(comm) as "补助的雇员数"
from emp ;
--12.显示管理者的总人数
select count(distinct mgr)
from emp;
--13.显示雇员工资的最大差额
select max(sal)-min(sal) as "工资最大差额"
from emp;
--14.显示部门代码为20的部门号,以及该部门的所有雇员名、雇员工资及岗位
select ename,sal,job,deptno
from emp
where deptno='20';
--15.显示获得补助的所有雇员名、补助额以及所在部门号
select ename,comm,deptno
from emp
where comm is not null;
--16.显示所有雇员的姓名、部门编号、工资,并且列名要显示为中文
select ename as "姓名",deptno as "部门编号",sal as "工资"
from emp;
--17.显示每个部门每个岗位的平均工资、每个部门的平均工资、每个岗位的平均工资
--显示每个部门每个岗位的平均工资
select deptno,job,avg(sal)
from emp
group by deptno, job
order by avg(sal) desc;
--每个部门的平均工资
select avg(sal)
from emp
group by deptno
order by avg(sal) desc;
--每个岗位的平均工资
select avg(sal)
from emp
group by job
order by avg(sal) desc;
select count(empno),avg(sal),deptno,job
from emp
group by deptno,job
--18.显示工资大于1500的雇员名和工资,并且按照工资的降序排列
select ename,sal
from emp
where sal>1500
order by sal desc;
--19.显示雇员部门编号为10或20的信息(要求使用IN关键字)
select *
from dept
where deptno in(10,20);
--20.显示雇员名的第二个字母为A的信息
select *
from emp
where ename like '_A%';
--21.显示没有发放补助的雇员信息
select *
from emp
where comm is null;
--22.显示雇员表中记录总数
select count(distinct empno)
from emp ;
--1,张无忌一年的总收入是多少
select salary * 12
from employees
where first_name = '张'
and last_name = '无忌';
--2,查询得到输出结果:某某's的月薪是:xx
select first_name || last_name || '''s的月薪是:' || salary
from employees;
--3,查询employees表中的不重复job_id
select distinct job_id
from employees
where job_id is not null;
--4,查询工资在4000-7000之间的员工信息
select *
from employees
where salary between 4000 and 7000;
--5,查询工资为3000或6000或10000的员工
select *
from employees
where salary in (10000, 6000, 3000);
--6,查询公司last_name中第二个字符是'尚'的员工
select *
from employees
where last_name like '_尚';
--7,查询公司的老板
select distinct m.*
from employees e, employees m
where e.manager_id = m.employee_id
and m.manager_id is null;
--8,查询雇佣时间在2010-2-1到2011-5-1之间的员工姓名和雇佣时间
select *
from employees
where hire_date between to_date('2010-02-01', 'yyyy-mm-dd')
and to_date('2011-5-1', 'yyyy-mm-dd');
--9,选择在2010年雇佣的员工的姓名和雇佣时间
select *
from employees
where to_char(hire_date,'yyyy')='2010';
--10,选择姓名中含有张或者敏的员工姓名
select *
from employees
where concat(first_name,last_name) like '%张%'
or concat(first_name,last_name) like '%敏%';
--11,隐藏部分员工姓名,例如布袋和尚取前1位布加最后1位尚,中间补5位'*',得到布*****尚
select substr(first_name,1,1)||'*****'||substr(last_name,length(last_name))
from employees;
select substr(first_name,1,1)||'*****'||substr(last_name,-1)
from employees;
--12,姓名右对齐
select LPAD(first_name||last_name,10,' ') 姓名
from employees;
--13,以本地格式打印员工工资
select salary,to_char(salary,'L99,999.99')
from employees;
--14,打印出"2010-5月-4 8:50:23"格式的日期和时间
select * from V$NLS_PARAMETERS;
alter session set nls_language='SIMPLIFIED CHINESE';
alter session set NLS_DATE_FORMAT='RR-MON-DD';--必须更改默认的日期格式的格式
select to_date('2010-5月-4 8:50:23','YYYY-MON-DD HH:MI:SS') from dual;--只能这样格式化
select to_date('2010年5月4日 8:50:23','YYYY年MON月DD日 HH:MI:SS') from dual;--不能实现
--15,计算员工年薪
select salary * 12 年薪,e.*
from employees e;
--16,打印公司员工工资等级:5000以下D;5000-10000C;10000-15000B;15000以上A
select salary,
case
when salary<5000 then 'D'
when salary>=5000 and salary<10000 then 'C'
when salary>=10000 and salary<15000 then 'B'
when salary>=15000 then 'A'
end
from employees;
--17,计算公司平均奖金点
--默认不计算null值
select avg(commission_pct)
from employees;
--包含空值
select avg(nvl(commission_pct,0))
from employees;
--18,查询平均工资高于4000的部门和其平均工资
select avg(salary),department_name
from employees e,departments d
where e.department_id=d.department_id
group by department_name
having avg(salary)>4000;
--19,查询职位是员工最高工资和最低工资差距
select max(salary)-min(salary)
from employees
where job_id='员工';
--20,查询各个管理人员下员工的平均工资,其中最低工资不能低于4000,不计算老板(manager_id is null)
select avg(e.salary),e.manager_id
from employees e, employees m
where e.manager_id = m.employee_id
and e.salary>4000
and m.manager_id is not null
group by e.manager_id;
--21,查询公司总人数,以及在2010,2011,2012年各进公司多少人
总人数 2010 2011 2012
10 2 3 5
select count(*) "总人数",
sum(decode(to_char(hire_date,'yyyy'),'2010',1,0)) "2010",
sum(decode(to_char(hire_date,'yyyy'),'2011',1,0)) "2011",
sum(decode(to_char(hire_date,'yyyy'),'2012',1,0)) "2012"
from employees;
--22,查询出公司员工的last_name,department_name,city
select e.employee_id,e.first_name||e.last_name,d.department_name,l.location_id,l.city
from employees e,departments d,locations l
where e.department_id=d.department_id
and l.location_id=d.location_id;
--23,查询last_name为'敏' 的管理员(manager_id is not null)信息
select distinct m.*
from employees e,employees m
where e.manager_id=m.employee_id
and m.manager_id is not null--manager_id is null表示老板
and m.last_name='敏';
--24,查询所有有奖金的员工的姓名,部门,地址,城市
select e.employee_id,e.first_name||e.last_name,d.department_name,l.location_id,l.city
from employees e,departments d,locations l
where e.department_id=d.department_id
and l.location_id=d.location_id
and e.commission_pct is not null;
--25,查询姓张的部门同事姓名
select * from employees e
where e.department_id in
(
select distinct department_id
from employees
where first_name='张'
and department_id is not null
)
and first_name!='张';
--26,查询工资比无忌高的员工姓名和工资
select e.*
from employees e
where e.salary>
(
select salary
from employees
where last_name='无忌'
);
--27,查询公司工资最低的员工信息
select first_name,last_name,salary
from employees
where salary =
(
select min(salary) from employees
);
--28,查询平均工资最高的部门名称
select department_name
from departments
where department_id =
(
select department_id
from employees
group by department_id
having avg(salary)=
(
select max(avg(salary))
from employees
group by department_id
)
);
select d.department_name
from departments d
join employees e
on d.department_id = e.department_id
group by d.department_id,d.department_name
having avg(e.salary)=
(
select max(avg(salary))
from employees
group by department_id
);
--29,查询平均工资高于公司平均工资的部门信息
select department_name
from departments
where department_id IN
(
select department_id
from employees
group by department_id
having avg(salary)>
(
select avg(salary) from employees
)
);
--30,查询公司所有管理员的信息
select first_name,last_name
from employees
where employee_id IN
(
select distinct(manager_id)
from employees
where manager_id is not null
);
--31,查询公司中平均工资最低的部门信息
select department_name
from departments
where department_id =
(
select department_id
from employees
group by department_id
having avg(salary)=
(
select min(avg(salary))
from employees
group by department_id
)
);
--32,查询和姓名中包含阿的员工的同部门同事信息
select e.first_name,e.last_name
from employees e
where e.department_id IN
(
select distinct(department_id)
from employees
where first_name||last_name LIKE '%阿%'
)
and e.first_name||e.last_name NOT LIKE '%阿%';
--33,查询last_name=敏管理的员工
select last_name,first_name
from employees
where manager_id IN
(
select employee_id
from employees
where last_name = '敏'
);
select e.*
from employees e,employees m
where e.manager_id=m.employee_id
and m.last_name='敏';
--34,查询各个部门中最高工资中最低的那个部门的最低工资(先删除没有部门的员工)
select min(salary) from employees
group by department_id
having department_id IN
(
select department_id
from employees
group by department_id
having max(salary)=
(
select min(max(salary)) from employees
group by department_id
)
);
--35,查询平均工资最高的部门的manager的信息
select distinct m.first_name||m.last_name
from employees e, employees m
where e.manager_id=m.employee_id
and m.department_id=
(
select department_id
from employees
group by department_id
having avg(salary)=
(
select max(avg(e.salary))
from employees e,departments d
where e.department_id=d.department_id
group by d.department_id
)
);
--36,查询2010年来公司的所有员工的最高工资的员工
select first_name,last_name
from employees
where salary=
(
select max(salary)
from employees
where to_char(hire_date,'YYYY')='2010'
)
and to_char(hire_date,'YYYY')='2010';
select rownum ,a.*
from
(
select first_name,last_name
from employees
where to_char(hire_date,'YYYY')='2010'
order by salary desc
) a
where rownum <=1;
--37,查询2010,2011,2012年各个部门新进员工数
部门id 2010 2011 2012
10 2 2 1
20 0 1 4
select department_id,
sum(decode(to_char(hire_date,'yyyy'),'2010',1,0)) "2010",
sum(decode(to_char(hire_date,'yyyy'),'2011',1,0)) "2011",
sum(decode(to_char(hire_date,'yyyy'),'2012',1,0)) "2012"
from employees
where hire_date is not null and department_id is not null
group by department_id
部门名称 2010 2011 2012
部门1 2 0 5
部门2 1 2 3
select d.department_name,
sum(decode(to_char(e.hire_date,'yyyy'),'2010',1,0)) "2010",
sum(decode(to_char(e.hire_date,'yyyy'),'2011',1,0)) "2011",
sum(decode(to_char(e.hire_date,'yyyy'),'2012',1,0)) "2012"
from employees e,departments d
where e.department_id=d.department_id
and hire_date is not null
group by d.department_name
select d.department_name,
sum(decode(t.years,'2010',t.coun,0)) "2010",
sum(decode(t.years,'2011',t.coun,0)) "2011",
sum(decode(t.years,'2012',t.coun,0)) "2012"
from departments d JOIN
(select department_id did,count(employee_id) coun,to_char(hire_date,'YYYY') years
from employees
where to_char(hire_date,'YYYY') IN('2010','2011','2012')
group by department_id,to_char(hire_date,'YYYY')) t ON d.department_id = t.did
group by d.department_name;
1.选择部门30中的所有员工.
select ename,job,mgr,hiredate,sal,comm ,deptno
from emp
where deptno=30;
2.列出职位为办事员(CLERK)的员工姓名,编号和部门编号.
select ename,empno,deptno
from emp
where job='CLERK';
3.找出佣金高于薪金的员工.
select *
from emp
where nvl(comm,0)>sal;
4.找出佣金高于薪金的60%的员工.
select *
from emp
where nvl(comm,0)>sal*0.6;
5.找出部门10中所有经理(MANAGER)和部门20中所有办事员(CLERK)的详细资料.
select empno ,ename, job,hiredate,sal,comm,deptno
from emp
where( job='MANAGER' and deptno=10)
or( job='CLERK' and deptno=20);
6.找出部门10中所有经理(MANAGER),部门20中所有办事员(CLERK),
既不是经理又不是办事员但其薪金大于或等于2000的所有员工的详细资料.
select empno,ename,job, hiredate,sal, deptno
from emp
where ( job='MANAGER' and deptno=10)
or( job='CLERK' and deptno=20)
or((job <>'MANAGER' and job<>'CLERK') and sal>=2000);
select *
from emp
where (deptno=10 and job='MANAGER')
or (deptno=20 and job='CLERK')
or (job not in ('MANAGER','CLERK') and sal>=2000);
7.找出收取佣金的员工的不同工作.
select distinct ename,job,comm
from emp
where NVL(comm,0)>0;
注意如果像上面这样写,就 job ename comm 3个同时不同的都列出来
正确的是:
select distinct job
from emp
where NVL(comm,0)>0;
8.找出不收取佣金或收取的佣金低于100的员工.
select ename,comm
from emp
where NVL(comm,0)=0
or NVL(comm,0)<100;
如果按上面的写法就判断就重复了,直接可以写成
select ename,comm
from emp
where NVL(comm,0)<100;
没使用函数,直接写
select ename,comm
from emp
where comm is null
or comm < 100;
9.找出各月倒数第3天受雇的所有员工.
select ename, job,hiredate
from emp
where hiredate=last_day(hiredate)-2;
10.找出早于12年前受雇的员工.
select ename,job
from emp
where months_between(sysdate,hiredate)/12>12
select * from emp where hiredate < add_months(sysdate,-12*12);
11.以首字母大写的方式显示所有员工的姓名.
select initcap(ename) name ,job, hiredate
from emp;
12.显示正好为5个字符的员工的姓名.
select ename,job,hiredate
from emp
where length(ename)=5;
13.显示不带有"R"的员工的姓名.
select ename, job
from emp
where ename not like '%R%';
14.显示所有员工姓名的前三个字符.
select substr(ename, 1,3) name,job,hiredate
from emp;
15.显示所有员工的姓名,用a替换所有"A"
select replace(ename, 'A','a') name,job,hiredate
from emp;
16.显示满10年服务年限的员工的姓名和受雇日期.
select ename, job, hiredate
from emp
where months_between(sysdate,hiredate)/12>=10;
17.显示员工的详细资料,按姓名排序.
select *
from emp
order by ename;
18.显示员工的姓名和受雇日期,根据其服务年限,将最老的员工排在最前面.
select ename, hiredate
from emp
order by hiredate desc;
19.显示所有员工的姓名、工作和薪金,按工作的降序排序,若工作相同则按薪金排序.
select ename,job, sal
from emp
order by job desc, sal asc;
20.显示所有员工的姓名、加入公司的年份和月份,按受雇日期所在月排序,
若月份相同则将最早年份的员工排在最前面.
select ename,
to_char(Hiredate,'yyyy"年"mm"月"') as 年月,
hiredate
from emp
order by to_char(Hiredate,'mm'),to_char(Hiredate,'yyyy');
21.显示在一个月为30天的情况所有员工的日薪金,忽略余数.
select ename,trunc(sal/30) daysal
from emp;
22.找出在(任何年份的)2月受聘的所有员工。
select ename, hiredate
from emp
where to_char(hiredate,'mm')='02';
23.对于每个员工,显示其加入公司的天数.
select ename,
sysdate-hiredate,
TRUNC(sysdate-hiredate) totdays
from emp;
24.显示姓名字段的任何位置包含"A"的所有员工的姓名.
select ename
from emp
where instr(ename,'A',1)>0;
25.以年月日的方式显示所有员工的服务年限.(大概)
select empno,ename,
months_between(sysdate,hiredate)/12,
('在职'||trunc((months_between(sysdate,hiredate))/12)||'年'
||trunc(mod((months_between(sysdate,hiredate)),12))||'个月'
||trunc(sysdate-(add_months(hiredate,months_between(sysdate,hiredate))))||'天') tot
from emp;
select months_between(sysdate,hiredate)/12,
'在职'||to_char(to_date('0001-01-01','yyyy-mm-dd')+(sysdate-hiredate)-366-31,'yy:mm:dd') as tot
from emp;
1.列出员工表中每个部门的员工数,和部门no
select deptno,count(*)
from emp
group by deptno;
2.列出员工表中每个部门的员工数(员工数必须大于3),和部门名称
select d.dname,t.cou
from
dept d,
(
select deptno,count(*) cou
from emp
group by deptno
having count(*)>3
) t
where
d.deptno = t.deptno;
3.找出工资比jones多的员工
select *
from emp
where sal>
(
select sal
from emp
where lower(ename)='jones'
);
select e.*
from emp e,
(
select *
from emp
where lower(ename)='jones'
) t
where e.sal>t.sal;
4.列出所有员工的姓名和其上级的姓名
select xd.ename ,boss.ename boss_name
from emp xd,emp boss
where xd.mgr=boss.empno;
5.以职位分组,找出平均工资最高的两种职位
select t.*
from
(
select job,avg(sal)
from emp
group by job
order by avg(sal) desc
) t
where rownum<=2;
6.查找出不在部门20,且比部门20中任何一个人工资都高的员工姓名、部门名称
select ename,dname
from
dept,
(
select ename,deptno
from emp
where deptno!=20
and sal>all
(
select sal
from emp
where deptno=20
)
)t
where
t.deptno=dept.deptno;
7.得到平均工资大于2000的工作职种
select job
from emp
group by job
having avg(sal)>2000;
8.分部门得到工资大于2000的所有员工的平均工资,并且平均工资还要大于2500
select deptno,avg(sal)
from emp
where sal>2000
group by deptno
having avg(sal)>2500;
9.得到每个月工资总数最少的那个部门的部门编号,部门名称,部门位置
select d.*
from
dept d,
(
select * from
(
select deptno,sum(sal) as sum_sal
from emp
group by deptno
order by sum(sal)
)
where rownum=1
) t
where
d.deptno = t.deptno;
10.分部门得到平均工资等级为4级(等级表)的部门编号
select t.deptno
from
salgrade s,
(
select deptno,avg(sal) avg_sal
from emp
group by deptno
) t
where
t.avg_sal between s.losal and s.hisal
and s.grade = 4;
select e1.deptno
from
(
select deptno,avg(sal) avg_sal
from emp
group by deptno
) e1,
(
select hisal,losal
from salgrade
where grade=4
) g1
where
e1.avg_sal between g1.losal and g1.hisal;
select deptno
from emp
group by deptno
having avg(sal)
between
(
select losal
from salgrade
where grade=4
)
and
(
select hisal
from salgrade
where grade=4
)
11.查找出部门10和部门20中,工资最高第3名到工资第5名的员工的员工名字部门名字部门位置
select t2.ename,d.dname,d.loc
from
dept d,
(
select *
from
(
select rownum no,t.*
from
(
select *
from emp
where deptno in (10,20)
order by sal desc
) t
) t1
where
t1.no>=3 and t1.no<=5
) t2
where
d.deptno = t2.deptno;
12.查找出收入(工资加上奖金),下级比自己上级还高的员工编号,员工名字,员工收入
select e.empno,e.ename,e.sal+nvl(e.comm,0)
from
emp e,
emp boss
where
e.mgr = boss.empno and
e.sal+nvl(e.comm,0)>boss.sal+nvl(boss.comm,0);
select emp.empno,emp.ename,emp.sal+nvl(emp.comm,0) as income
from emp,
(
select empno,sal+nvl(comm,0) as incm
from emp
) boss
where emp.sal+nvl(emp.comm,0) > boss.incm
and emp.mgr=boss.empno;
13.查找出工资等级不为4级的员工的员工名字,部门名字,部门位置
select
e.ename,d.dname,d.loc
from
emp e,
salgrade s,
dept d
where
e.sal between s.losal and s.hisal
and e.deptno = d.deptno
and s.grade!=4;
14.查找出职位和'MARTIN' 或者'SMITH'一样的员工的平均工资
select avg(sal)
from emp
where job in
(
select distinct job
from emp
where ename in('MARTIN','SMITH')
);
15.查找出不属于任何部门的员工
select *
from emp
where deptno not in
(
select distinct deptno
from dept
);
16.按部门统计员工数,查处员工数最多的部门的第二名和第三名(列出部门名字,部门位置)
select *
from
(
select t.*,rownum r
from
(
select dept.deptno,dept.loc,count(emp.empno) cnum
from emp
join dept
on emp.deptno = dept.deptno
group by dept.deptno,dept.loc
order by cnum
)t
where rownum <=3
)
where r>=2
17.查询出king所在部门的部门号\部门名称\部门人数
select
d.deptno,
d.dname,
(
select count(*)
from emp
where deptno = t.deptno
)
from
dept d,
(
select deptno
from emp
where ename = 'KING'
) t
where
d.deptno = t.deptno;