SQL查询基本练习1:(作业)
使用scott/tiger用户下的emp表完成下列练习,表的结构说明如下
emp员工表 字段内容如下:
empno 员工号
ename 员工姓名
job 工作
mgr 上级编号
hiredate 受雇日期
sal 薪金
comm 佣金
deptno 部门编号
--创建表
CREATE TABLE emp
(
empno NUMBER(4),
ename VARCHAR2(10),
job VARCHAR2(10),
mgr NUMBER(4),
hiredate DATE,
sal NUMBER(4),
comm NUMBER(4),
deptno NUMBER(4)
);
INSERT INTO emp VALUES (1001,'zhang','MANAGER',1001,TO_DATE('28-JUL-1990','DD-MM-YYYY'),5000,3000,20);
INSERT INTO emp VALUES (1002,'zhang','CLERK',1001,TO_DATE('28-JUL-2000','DD-MM-YYYY'),3000,4500,20);
INSERT INTO emp VALUES (1003,'zhang','CLERK',1001,TO_DATE('28-JUL-2004','DD-MM-YYYY'),2000,NULL,20);
INSERT INTO emp VALUES (1004,'zhang','OTHER',1001,TO_DATE('28-JUL-2003','DD-MM-YYYY'),4000,500,20);
INSERT INTO emp VALUES (1005,'zhang','OTHER',1001,TO_DATE('28-JUL-2005','DD-MM-YYYY'),1000,NULL,20);
INSERT INTO emp VALUES (2001,'zhang','MANAGER',2001,TO_DATE('28-JUL-1990','DD-MM-YYYY'),5000,3000,10);
INSERT INTO emp VALUES (2002,'zhang','CLERK',2001,TO_DATE('28-JUL-1997','DD-MM-YYYY'),3000,8000,10);
INSERT INTO emp VALUES (2003,'zhang','CLERK',2001,TO_DATE('28-JUL-2005','DD-MM-YYYY'),2000,NULL,10);
INSERT INTO emp VALUES (2004,'zhang','OTHER',2001,TO_DATE('28-JUL-2007','DD-MM-YYYY'),4000,50,10);
INSERT INTO emp VALUES (2005,'zhang','OTHER',2001,TO_DATE('28-JUL-2006','DD-MM-YYYY'),1000,NULL,10);
INSERT INTO emp VALUES (3001,'zhang','MANAGER',3001,TO_DATE('28-JUL-1990','DD-MM-YYYY'),5000,3000,30);
INSERT INTO emp VALUES (3002,'zhang','CLERK',3001,TO_DATE('28-JUL-2008','DD-MM-YYYY'),3000,1500,30);
INSERT INTO emp VALUES (3003,'zhang','CLERK',3001,TO_DATE('28-JUL-2006','DD-MM-YYYY'),2000,NULL,30);
INSERT INTO emp VALUES (3004,'zhang','OTHER',3001,TO_DATE('28-JUL-1990','DD-MM-YYYY'),4000,40,30);
INSERT INTO emp VALUES (3005,'zhang','OTHER',3001,TO_DATE('28-JUL-2007','DD-MM-YYYY'),1000,NULL,30);
INSERT INTO emp VALUES (4001,'zhang','MANAGER',4001,TO_DATE('28-JUL-1990','DD-MM-YYYY'),5000,3000,40);
INSERT INTO emp VALUES (4002,'zhang','CLERK',4001,TO_DATE('28-JUL-1989','DD-MM-YYYY'),3000,6000,40);
INSERT INTO emp VALUES (4003,'zhang','CLERK',4001,TO_DATE('28-JUL-1996','DD-MM-YYYY'),2000,NULL,40);
INSERT INTO emp VALUES (4004,'zhang','OTHER',4001,TO_DATE('28-JUL-1991','DD-MM-YYYY'),4000,60,40);
INSERT INTO emp VALUES (4005,'zhang','OTHER',4001,TO_DATE('28-JUL-1993','DD-MM-YYYY'),1000,NULL,40);
INSERT INTO emp VALUES (5001,'zhang','MANAGER',5001,TO_DATE('28-JUL-1990','DD-MM-YYYY'),5000,3000,50);
INSERT INTO emp VALUES (5002,'zhang','CLERK',5001,TO_DATE('28-JUL-2004','DD-MM-YYYY'),3000,1500,50);
INSERT INTO emp VALUES (5003,'zhang','CLERK',5001,TO_DATE('28-JUL-2006','DD-MM-YYYY'),2000,NULL,50);
INSERT INTO emp VALUES (5004,'zhang','OTHER',5001,TO_DATE('28-JUL-1990','DD-MM-YYYY'),4000,3000,50);
INSERT INTO emp VALUES (5005,'zhang','OTHER',5001,TO_DATE('28-JUL-1990','DD-MM-YYYY'),1000,NULL,50);
1.选择部门30中的所有员工.
A:SELECT empno,ename,job,mgr,hiredate,sal,comm,deptno FROM emp WHERE deptno = 30;
2.列出所有办事员(CLERK)的姓名,编号和部门编号.
A:SELECT empno, ename,deptno FROM emp WHERE job='CLERK';
3.找出佣金高于薪金的员工.
A:SELECT empno ,ename FROM emp WHERE comm > sal;
4.找出佣金高于薪金的60%的员工.
A:SELECT empno ,ename FROM emp WHERE comm > sal*1.6;
5.找出部门10中所有经理(MANAGER)和部门20中所有办事员(CLERK)的详细资料.
A:SELECT empno,ename,job,mgr,hiredate,sal,comm,deptno
FROM emp
WHERE (deptno =10 AND job ='MANAGER') OR (deptno =20 AND job ='CLERK');
6.找出部门10中所有经理(MANAGER),部门20中所有办事员(CLERK),既不是经理又不是办事员但其薪金大于或等于2000的所有员工的详细资料.
A:SELECT empno,ename,job,mgr,hiredate,sal,comm,deptno
FROM emp
WHERE ((deptno =10 AND job ='MANAGER') OR (deptno =20 AND job ='CLERK'))
OR
(job <> 'MANAGER' AND job <> 'CLERK' AND sal >=2000)
7.找出收取佣金的员工的不同工作.
A:SELECT job FROM emp WHERE comm>0 GROUP BY job;
8.找出不收取佣金或收取的佣金低于100的员工.
A:SELECT empno,ename FROM emp
WHERE comm<100 --不收取佣金,内部表内是空值 还是0呢?此答题内容为0的方式答题
SELECT empno ,ename FROM emp
WHERE comm IS NULL OR comm<100 --这个方式是内部表为空值的情况
9.找出各月倒数第3天受雇的所有员工.
A:SELECT empno ,ename ,hiredate FROM emp
WHERE LAST_DAY(hiredate)-hiredate=3 --执行成功
10.找出早于12年前受雇的员工.
A:SELECT * from emp
where MONTHS_BETWEEN(SYSDATE,hiredate)>12*12; --执行成功
11.以首字母大写的方式显示所有员工的姓名.
A:SELECT INITCAP(ename) AS ename FROM emp; --执行成功
12.显示正好为5个字符的员工的姓名.
A:SELECT ename FROM emp WHERE LENGTH(ename)=5; --执行成功
13.显示不带有"R"的员工的姓名.
A:SELECT ename FROM emp WHERE ename NOT LIKE '%R%'; --执行成功
14.显示所有员工姓名的前三个字符.
A:SELECT SUBSTR(ename,1,3) AS ename FROM emp; --执行成功
15.显示所有员工的姓名,用a替换所有"A"
A:SELECT REPLACE(ename,'a','A') FROM emp ; --执行成功
16.显示满10年服务年限的员工的姓名和受雇日期.
A:SELECT ename,hiredate FROM emp
WHERE MONTHS_BETWEEN(sysdate,hiredate)>=10*12; --执行成功
17.显示员工的详细资料,按姓名排序.
A:SELECT empno,ename,job,mgr,hiredate,sal,comm,deptno --执行成功
FROM emp ORDER BY ename;
18.显示员工的姓名和受雇日期,根据其服务年限,将最老的员工排在最前面.
A:SELECT ename,hiredate, MONTHS_BETWEEN(sysdate,hiredate) AS Bmonths --执行成功
FROM emp ORDER BY Bmonths DESC;
19.显示所有员工的姓名、工作和薪金,按工作的降序排序,若工作相同则按薪金排序.
A:SELECT ename,job,sal
FROM emp
ORDER BY job DESC,sal ;
20.显示所有员工的姓名、加入公司的年份和月份,按受雇日期所在月排序,若月份相同则将最早年份的员工排在最前面.
A:SELECT ename,TO_NUMBER(TO_CHAR(hiredate,'YYYY')) AS year,TO_CHAR(hiredate,'MM') AS month
FROM emp
ORDER BY month,year ;--执行成功,这里存在一个问题!就是月份的 高低排序的问题,我用的是字符串进行排序的,没有NUMBER看起来舒服。
21.显示在一个月为30天的情况所有员工的日薪金,忽略余数.
A:SELECT TRUNC(sal/30) AS DaySAL FROM emp
22.找出在(任何年份的)2月受聘的所有员工。
A:SELECT empno,ename FROM emp WHERE TO_CHAR(hiredate,'MM')='02';
23.对于每个员工,显示其加入公司的天数.
A:SELECT empno,ename,(SYSDATE-hiredate) AS days
FROM emp; --根据题目的意思肯定是这么写,但是情况有点复杂就是DAYS有小数存在,那么我们就可以使用ROUND 或者TRUNC函数进行取舍。具体情况具体对待
ROUND:SELECT empno,ename,ROUND(SYSDATE-hiredate) AS days FROM emp;
TRUNC:SELECT empno,ename,TRUNC(SYSDATE-hiredate) AS days FROM emp;
24.显示姓名字段的任何位置包含"A"的所有员工的姓名.
A:SELECT empno ,ename FROM emp WHERE empno LIKE '%A%'; --执行成功
25.以年月日的方式显示所有员工的服务年限. (大概)
A:
MONTH:
SELECT empno,ename,MONTHS_BETWEEN(SYSDATE,hiredate) AS Bmonths FROM emp ;-- 详细
ROUND:SELECT empno,ename,ROUND(MONTHS_BETWEEN(SYSDATE,hiredate)) AS Bmonths FROM emp ;--round 大概
TRUNC:SELECT empno,ename,TRUNC(MONTHS_BETWEEN(SYSDATE,hiredate)) AS Bmonths FROM emp ;--trunc 大概
DAY:
SELECT empno,ename,(SYSDATE-hiredate) ASBdays FROM emp; -- 详细
ROUND:SELECT empno,ename,ROUND(SYSDATE-hiredate) AS Bdays FROM emp; --round大概
TRUNC:SELECT empno,ename,TRUNC(SYSDATE-hiredate) AS Bdays FROM emp; --trunc大概
YEAR:
SELECT empno,ename,(MONTHS_BETWEEN(SYSDATE,hiredate))/12AS Byears FROM emp;-- 详细
ROUND:SELECT empno,ename,ROUND(MONTHS_BETWEEN(SYSDATE,hiredate)/12) AS Bmonths FROM emp ; --round大概
TRUNC:SELECT empno,ename,TRUNC(MONTHS_BETWEEN(SYSDATE,hiredate)/12) AS Bmonths FROM emp ; --trunc大概
emp员工表 字段内容如下:
empno 员工号
ename 员工姓名
job 工作
mgr 上级编号
hiredate 受雇日期
sal 薪金
comm 佣金
deptno 部门编号
--创建表
CREATE TABLE emp
(
empno NUMBER(4),
ename VARCHAR2(10),
job VARCHAR2(10),
mgr NUMBER(4),
hiredate DATE,
sal NUMBER(4),
comm NUMBER(4),
deptno NUMBER(4)
);
INSERT INTO emp VALUES (1001,'zhang','MANAGER',1001,TO_DATE('28-JUL-1990','DD-MM-YYYY'),5000,3000,20);
INSERT INTO emp VALUES (1002,'zhang','CLERK',1001,TO_DATE('28-JUL-2000','DD-MM-YYYY'),3000,4500,20);
INSERT INTO emp VALUES (1003,'zhang','CLERK',1001,TO_DATE('28-JUL-2004','DD-MM-YYYY'),2000,NULL,20);
INSERT INTO emp VALUES (1004,'zhang','OTHER',1001,TO_DATE('28-JUL-2003','DD-MM-YYYY'),4000,500,20);
INSERT INTO emp VALUES (1005,'zhang','OTHER',1001,TO_DATE('28-JUL-2005','DD-MM-YYYY'),1000,NULL,20);
INSERT INTO emp VALUES (2001,'zhang','MANAGER',2001,TO_DATE('28-JUL-1990','DD-MM-YYYY'),5000,3000,10);
INSERT INTO emp VALUES (2002,'zhang','CLERK',2001,TO_DATE('28-JUL-1997','DD-MM-YYYY'),3000,8000,10);
INSERT INTO emp VALUES (2003,'zhang','CLERK',2001,TO_DATE('28-JUL-2005','DD-MM-YYYY'),2000,NULL,10);
INSERT INTO emp VALUES (2004,'zhang','OTHER',2001,TO_DATE('28-JUL-2007','DD-MM-YYYY'),4000,50,10);
INSERT INTO emp VALUES (2005,'zhang','OTHER',2001,TO_DATE('28-JUL-2006','DD-MM-YYYY'),1000,NULL,10);
INSERT INTO emp VALUES (3001,'zhang','MANAGER',3001,TO_DATE('28-JUL-1990','DD-MM-YYYY'),5000,3000,30);
INSERT INTO emp VALUES (3002,'zhang','CLERK',3001,TO_DATE('28-JUL-2008','DD-MM-YYYY'),3000,1500,30);
INSERT INTO emp VALUES (3003,'zhang','CLERK',3001,TO_DATE('28-JUL-2006','DD-MM-YYYY'),2000,NULL,30);
INSERT INTO emp VALUES (3004,'zhang','OTHER',3001,TO_DATE('28-JUL-1990','DD-MM-YYYY'),4000,40,30);
INSERT INTO emp VALUES (3005,'zhang','OTHER',3001,TO_DATE('28-JUL-2007','DD-MM-YYYY'),1000,NULL,30);
INSERT INTO emp VALUES (4001,'zhang','MANAGER',4001,TO_DATE('28-JUL-1990','DD-MM-YYYY'),5000,3000,40);
INSERT INTO emp VALUES (4002,'zhang','CLERK',4001,TO_DATE('28-JUL-1989','DD-MM-YYYY'),3000,6000,40);
INSERT INTO emp VALUES (4003,'zhang','CLERK',4001,TO_DATE('28-JUL-1996','DD-MM-YYYY'),2000,NULL,40);
INSERT INTO emp VALUES (4004,'zhang','OTHER',4001,TO_DATE('28-JUL-1991','DD-MM-YYYY'),4000,60,40);
INSERT INTO emp VALUES (4005,'zhang','OTHER',4001,TO_DATE('28-JUL-1993','DD-MM-YYYY'),1000,NULL,40);
INSERT INTO emp VALUES (5001,'zhang','MANAGER',5001,TO_DATE('28-JUL-1990','DD-MM-YYYY'),5000,3000,50);
INSERT INTO emp VALUES (5002,'zhang','CLERK',5001,TO_DATE('28-JUL-2004','DD-MM-YYYY'),3000,1500,50);
INSERT INTO emp VALUES (5003,'zhang','CLERK',5001,TO_DATE('28-JUL-2006','DD-MM-YYYY'),2000,NULL,50);
INSERT INTO emp VALUES (5004,'zhang','OTHER',5001,TO_DATE('28-JUL-1990','DD-MM-YYYY'),4000,3000,50);
INSERT INTO emp VALUES (5005,'zhang','OTHER',5001,TO_DATE('28-JUL-1990','DD-MM-YYYY'),1000,NULL,50);
1.选择部门30中的所有员工.
A:SELECT empno,ename,job,mgr,hiredate,sal,comm,deptno FROM emp WHERE deptno = 30;
2.列出所有办事员(CLERK)的姓名,编号和部门编号.
A:SELECT empno, ename,deptno FROM emp WHERE job='CLERK';
3.找出佣金高于薪金的员工.
A:SELECT empno ,ename FROM emp WHERE comm > sal;
4.找出佣金高于薪金的60%的员工.
A:SELECT empno ,ename FROM emp WHERE comm > sal*1.6;
5.找出部门10中所有经理(MANAGER)和部门20中所有办事员(CLERK)的详细资料.
A:SELECT empno,ename,job,mgr,hiredate,sal,comm,deptno
FROM emp
WHERE (deptno =10 AND job ='MANAGER') OR (deptno =20 AND job ='CLERK');
6.找出部门10中所有经理(MANAGER),部门20中所有办事员(CLERK),既不是经理又不是办事员但其薪金大于或等于2000的所有员工的详细资料.
A:SELECT empno,ename,job,mgr,hiredate,sal,comm,deptno
FROM emp
WHERE ((deptno =10 AND job ='MANAGER') OR (deptno =20 AND job ='CLERK'))
OR
(job <> 'MANAGER' AND job <> 'CLERK' AND sal >=2000)
7.找出收取佣金的员工的不同工作.
A:SELECT job FROM emp WHERE comm>0 GROUP BY job;
8.找出不收取佣金或收取的佣金低于100的员工.
A:SELECT empno,ename FROM emp
WHERE comm<100 --不收取佣金,内部表内是空值 还是0呢?此答题内容为0的方式答题
SELECT empno ,ename FROM emp
WHERE comm IS NULL OR comm<100 --这个方式是内部表为空值的情况
9.找出各月倒数第3天受雇的所有员工.
A:SELECT empno ,ename ,hiredate FROM emp
WHERE LAST_DAY(hiredate)-hiredate=3 --执行成功
10.找出早于12年前受雇的员工.
A:SELECT * from emp
where MONTHS_BETWEEN(SYSDATE,hiredate)>12*12; --执行成功
11.以首字母大写的方式显示所有员工的姓名.
A:SELECT INITCAP(ename) AS ename FROM emp; --执行成功
12.显示正好为5个字符的员工的姓名.
A:SELECT ename FROM emp WHERE LENGTH(ename)=5; --执行成功
13.显示不带有"R"的员工的姓名.
A:SELECT ename FROM emp WHERE ename NOT LIKE '%R%'; --执行成功
14.显示所有员工姓名的前三个字符.
A:SELECT SUBSTR(ename,1,3) AS ename FROM emp; --执行成功
15.显示所有员工的姓名,用a替换所有"A"
A:SELECT REPLACE(ename,'a','A') FROM emp ; --执行成功
16.显示满10年服务年限的员工的姓名和受雇日期.
A:SELECT ename,hiredate FROM emp
WHERE MONTHS_BETWEEN(sysdate,hiredate)>=10*12; --执行成功
17.显示员工的详细资料,按姓名排序.
A:SELECT empno,ename,job,mgr,hiredate,sal,comm,deptno --执行成功
FROM emp ORDER BY ename;
18.显示员工的姓名和受雇日期,根据其服务年限,将最老的员工排在最前面.
A:SELECT ename,hiredate, MONTHS_BETWEEN(sysdate,hiredate) AS Bmonths --执行成功
FROM emp ORDER BY Bmonths DESC;
19.显示所有员工的姓名、工作和薪金,按工作的降序排序,若工作相同则按薪金排序.
A:SELECT ename,job,sal
FROM emp
ORDER BY job DESC,sal ;
20.显示所有员工的姓名、加入公司的年份和月份,按受雇日期所在月排序,若月份相同则将最早年份的员工排在最前面.
A:SELECT ename,TO_NUMBER(TO_CHAR(hiredate,'YYYY')) AS year,TO_CHAR(hiredate,'MM') AS month
FROM emp
ORDER BY month,year ;--执行成功,这里存在一个问题!就是月份的 高低排序的问题,我用的是字符串进行排序的,没有NUMBER看起来舒服。
21.显示在一个月为30天的情况所有员工的日薪金,忽略余数.
A:SELECT TRUNC(sal/30) AS DaySAL FROM emp
22.找出在(任何年份的)2月受聘的所有员工。
A:SELECT empno,ename FROM emp WHERE TO_CHAR(hiredate,'MM')='02';
23.对于每个员工,显示其加入公司的天数.
A:SELECT empno,ename,(SYSDATE-hiredate) AS days
FROM emp; --根据题目的意思肯定是这么写,但是情况有点复杂就是DAYS有小数存在,那么我们就可以使用ROUND 或者TRUNC函数进行取舍。具体情况具体对待
ROUND:SELECT empno,ename,ROUND(SYSDATE-hiredate) AS days FROM emp;
TRUNC:SELECT empno,ename,TRUNC(SYSDATE-hiredate) AS days FROM emp;
24.显示姓名字段的任何位置包含"A"的所有员工的姓名.
A:SELECT empno ,ename FROM emp WHERE empno LIKE '%A%'; --执行成功
25.以年月日的方式显示所有员工的服务年限. (大概)
A:
MONTH:
SELECT empno,ename,MONTHS_BETWEEN(SYSDATE,hiredate) AS Bmonths FROM emp ;-- 详细
ROUND:SELECT empno,ename,ROUND(MONTHS_BETWEEN(SYSDATE,hiredate)) AS Bmonths FROM emp ;--round 大概
TRUNC:SELECT empno,ename,TRUNC(MONTHS_BETWEEN(SYSDATE,hiredate)) AS Bmonths FROM emp ;--trunc 大概
DAY:
SELECT empno,ename,(SYSDATE-hiredate) ASBdays FROM emp; -- 详细
ROUND:SELECT empno,ename,ROUND(SYSDATE-hiredate) AS Bdays FROM emp; --round大概
TRUNC:SELECT empno,ename,TRUNC(SYSDATE-hiredate) AS Bdays FROM emp; --trunc大概
YEAR:
SELECT empno,ename,(MONTHS_BETWEEN(SYSDATE,hiredate))/12AS Byears FROM emp;-- 详细
ROUND:SELECT empno,ename,ROUND(MONTHS_BETWEEN(SYSDATE,hiredate)/12) AS Bmonths FROM emp ; --round大概
TRUNC:SELECT empno,ename,TRUNC(MONTHS_BETWEEN(SYSDATE,hiredate)/12) AS Bmonths FROM emp ; --trunc大概