oracle(scott用户)查询语句练习题与答案

–找出佣金高于薪金60%的雇员。
SELECT * FROM EMP WHERE COMM>(SAL*0.6);
–找出部门10中所有经理和部门20中所有办事员的详细资料。
SELECT *FROM EMP WHERE (DEPTNO=10 AND job=’MANAGER’) OR (DEPTNO=20 AND job=’CLERK’)
–找出部门10中所有经理,部门20中所有办事员以及既不是经理又不是办事员但其薪金大于或等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)
–找出收取佣金的雇员的不同工作。
SELECT DISTINCT job FROM EMP WHERE COMM IS NOT NULL
–找出不收取佣金或收取的佣金低于300的雇员
SELECT * FROM EMP WHERE COMM IS NULL OR COMM<300
–找出各月最后一天受雇的所有雇员。
SELECT * FROM EMP WHERE LAST_DAY(HIREDATE)=HIREDATE
–找出晚于26年之前受雇的雇员。
SELECT *FROM EMP WHERE SYSDATE>ADD_MONTHS(HIREDATE,26*12);
–显示只有首字母大写的的所有雇员的姓名
select ENAME from EMP where ASCII(SUBSTR(ENAME,1,1)) >= 65 and ASCII(SUBSTR(ENAME,1,1))<=90
–显示正好为5个字符的雇员的姓名。
select ENAME from EMP WHERE LENGTH(ENAME)=5
–显示不带有“R”的雇员姓名。
select ENAME from EMP WHERE ENAME NOT LIKE ‘%R%’;
–显示所有雇员的姓名的前三个字符。
SELECT SUBSTR(ename,1,3) FROM EMP;
–显所有雇员的姓名,用a替换所有“A”。
SELECT REPLACE(ename,’A’,’a’) from EMP;
–显示所有雇员的姓名以及满10年服务年限的日期
SELECT ename,hiredate FROM emp WHERE SYSDATE>ADD_MONTHS(HIREDATE, 12*10)
–显示雇员的详细资料,按姓名排序。
SELECT * FROM EMP ORDER BY ENAME;
–显示雇员姓名,根据其服务年限,将最老的雇员排在最前面。
SELECT ename,HIREDATE FROM EMP ORDER BY HIREDATE ASC;
–显示所有雇员的姓名、工作和薪金,按工作的降序排序,而工作按薪金排序
SELECT ename,job,sal from EMP ORDER BY job DESC,sal desc;
–显示所有雇员的姓名和加入公司的年份和月份,按雇员受雇日所在月排序,并将最早年份的项目排在最前面
SELECT ename,TO_CHAR(HIREDATE,’YYYY-mm’) FROM EMP ORDER BY TO_CHAR(hiredate,’mm’) ASC,TO_CHAR(HIREDATE,’YYYY’) ASC;
–显示在一个月为30天的情况下所有雇员的日薪金,取整
SELECT ROUND(sal/30, 0) FROM EMP;
–找出在(任何年份的)2月受聘的所有雇员
SELECT * from EMP WHERE TO_CHAR(HIREDATE,’mm’)=’02’
–对于每个雇员,显示其加入公司的天数
SELECT CEIL(SYSDATE-HIREDATE) FROM EMP;
–找出所有的普通员工
SELECT *FROM EMP WHERE EMPNO NOT IN ( SELECT MGR FROM EMP WHERE MGR is NOT NULL)
–求部门中哪些人的薪水最高
SELECT e.ename,e.SAL
FROM EMP e INNER JOIN
(select MAX(SAL) max_sal,DEPTNO from emp group by deptno) t
ON e.sal=t.max_sal AND e.deptno=t.deptno;
–求部门平均薪水的等级
Select t.deptno,t.avg_sal,s.GRADE
FROM
(select AVG(SAL) avg_sal,DEPTNO from EMP GROUP BY DEPTNO)t
join SALGRADE s ON t.avg_sal BETWEEN s.LOSAL AND s.HISAL;
–求部门平均的薪水等级
Select deptno,avg(grade)
from
(select deptno,ename,grade from emp
join salgrade s on (emp.sal between s.losal and s.hisal)) t group by deptno;
雇员中哪些人是经理人:
Select ename from emp where empno in (select distinct mgr from emp);

不准用组函数,求薪水的最高值
Select distinct sal from emp where sal not in (select distinct e1.sal from emp e1 join emp e2 on(e1.sal < e2.sal));

求平均薪水最高的部门的部门编号
Select deptno,avg_sal from (select avg(sal) avg_sal,deptno from emp group by deptno) where avg_sal = (select max(avg_sal) from (select avg(sal) avg_sal,deptno from emp group by deptno));

求平均薪水最高的部门的部门名字
select dname from dept where
deptno=(Select deptno from (select avg(sal)
avg_sal,deptno from emp group by deptno) where avg_sal = (select max(avg_sal) from (select avg(sal) avg_sal,deptno from emp group by deptno)))

求比普通员工的最高薪水还要高的经理人名称
Select ename from emp where empno in (select distinct mgr from emp where mgr is not null) and sal >(select max(sal) from emp where empno not in(select distinct mgr from emp where mgr is not null))

你可能感兴趣的:(Java,oracle)