Oracle的sql语句练习题含答案

合集下载

Oracle经典练习题及标准答案

Oracle经典练习题及标准答案

oracle经典练习sql/*1、选择在部门30中员工的所有信息*/select * from scott.emp where deptno = '30'/*2、列出职位为(MANAGER)的员工的编号,姓名*/select empno, ename from scott.emp where job = 'MANAGER'/*3、找出奖金高于工资的员工*/select * from scott.emp where comm > sal/*4、找出每个员工奖金和工资的总和*/select ename, sal + nvl(comm, 0) from scott.emp/*5、找出部门10中的经理(MANAGER)和部门20中的普通员工(CLERK) */select *from scott.empwhere deptno = '10'and job = 'MANAGER'unionselect *from scott.empwhere job = 'CLERK'and deptno = '20'/*6、找出部门10中既不是经理也不是普通员工,而且工资大于等于2000的员工*/ select *from scott.empwhere job != 'MANAGER'and job != 'CLERK'and sal > 2000/*7、找出有奖金的员工的不同工作*/select distinct(job) from scott.emp where comm is not null/*8、找出没有奖金或者奖金低于500的员工*/select *from scott.empwhere comm is not nulland comm > 500/*9、显示雇员姓名,根据其服务年限,将最老的雇员排在最前面*/select enamefrom scott.emporder by (months_between(sysdate, hiredate) / 12) descselect ename,hiredate from scott.emp order by hiredate/*10、找出每个月倒数第三天受雇的员工*/select * from scott.emp where hiredate = last_day(hiredate) - 2/*11、分别用case和decode函数列出员工所在的部门,deptno=10显示'部门10',deptno=20显示'部门20'deptno=30显示'部门30'deptno=40显示'部门40'否则为'其他部门'*/select ename,case deptnowhen 10 then'部门10'when 20 then'部门20'when 30 then'部门30'when 40 then'部门40'else'其他部门'end 工资情况from scott.empselect ename,decode(deptno,10,'部门10',20,'部门20',30,'部门30',40,'部门40','其他部门') 工资情况from scott.emp/*12、分组统计各部门下工资>500的员工的平均工资*/select avg(sal) from scott.emp where sal > 500 group by deptno/*13、统计各部门下平均工资大于500的部门*/select deptno from scott.emp group by deptno having avg(sal) > 500 /*14、算出部门30中得到最多奖金的员工奖金*/select max(comm) from scott.emp where deptno = 30/*15、算出部门30中得到最多奖金的员工姓名*/select enamefrom scott.empwhere deptno = 30and comm = (select max(comm) from scott.emp where deptno = 30) /*16、算出每个职位的员工数和最低工资*/select count(ename), min(sal), job from scott.emp group by job/*17、列出员工表中每个部门的员工数,和部门no */select count(ename), deptno from scott.emp group by deptno/*18、得到工资大于自己部门平均工资的员工信息*/select *from scott.emp ewhere sal > (select avg(sal) from scott.emp where e.deptno = deptno)select *from scott.emp e1,(select avg(sal) sals, deptno from scott.emp group by deptno) e2where sal > salsand e1.deptno = e2.deptno/*19、分组统计每个部门下,每种职位的平均奖金(也要算没奖金的人)和总工资(包括奖金) */select avg(nvl(comm,0)), sum(sal + nvl(comm, 0))from scott.empgroup by deptno,job/*20、笛卡尔集*/select * from scott.emp, scott.dept/*21、显示员工ID,名字,直属主管ID,名字*/select empno,ename,mgr,(select ename from scott.emp e1 where e1.empno = e2.mgr) 直属主管名字from scott.emp e2/*22、DEPT表按照部门跟EMP表左关联*/select *fromscott.dept, scott.empwherescott.dept.deptno = scott.emp.deptno(+)/*23、使用此语句重复的内容不再显示了*/select distinct (job) from scott.emp/*24、重复的内容依然显示*/select *from scott.empUNION ALLselect * from scott.emp/*23和24题和22题是一样的*//*25、只显示了两个表中彼此重复的记录。

Oracle经典练习题及标准答案

Oracle经典练习题及标准答案

Oracle经典练习题及标准答案oracle经典练习sql/*1、选择在部门30中员工的所有信息*/select * from scott.emp where deptno = '30'/*2、列出职位为(MANAGER)的员工的编号,姓名*/select empno, ename from scott.emp where job = 'MANAGER'/*3、找出奖金高于工资的员工*/select * from scott.emp where comm > sal/*4、找出每个员工奖金和工资的总和*/select ename, sal + nvl(comm, 0) from scott.emp/*5、找出部门10中的经理(MANAGER)和部门20中的普通员工(CLERK) */select *from scott.empwhere deptno = '10'and job = 'MANAGER'unionselect *from scott.empwhere job = 'CLERK'and deptno = '20'/*6、找出部门10中既不是经理也不是普通员工,而且工资大于等于2000的员工*/ select *from scott.empwhere job != 'MANAGER'and job != 'CLERK'and sal > 2000/*7、找出有奖金的员工的不同工作*/select distinct(job) from scott.emp where comm is not null /*8、找出没有奖金或者奖金低于500的员工*/select *from scott.empwhere comm is not nulland comm > 500/*9、显示雇员姓名,根据其服务年限,将最老的雇员排在最前面*/select enamefrom scott.emporder by (months_between(sysdate, hiredate) / 12) descselect ename,hiredate from scott.emp order by hiredate/*10、找出每个月倒数第三天受雇的员工*/select * from scott.emp where hiredate = last_day(hiredate) - 2/*11、分别用case和decode函数列出员工所在的部门,deptno=10显示'部门10',deptno=20显示'部门20'deptno=30显示'部门30'deptno=40显示'部门40'否则为'其他部门'*/select ename,case deptnowhen 10 then'部门10'when 20 then'部门20'when 30 then'部门30'when 40 then'部门40'else'其他部门'end 工资情况from scott.empselect ename,decode(deptno,10,'部门10',20,'部门20',30,'部门30',40,'部门40','其他部门') 工资情况from scott.emp/*12、分组统计各部门下工资>500的员工的平均工资*/select avg(sal) from scott.emp where sal > 500 group by deptno/*13、统计各部门下平均工资大于500的部门*/select deptno from scott.emp group by deptno having avg(sal) > 500 /*14、算出部门30中得到最多奖金的员工奖金*/ select max(comm) from scott.emp where deptno = 30/*15、算出部门30中得到最多奖金的员工姓名*/select enamefrom scott.empwhere deptno = 30and comm = (select max(comm) from scott.emp where deptno = 30) /*16、算出每个职位的员工数和最低工资*/select count(ename), min(sal), job from scott.emp group by job/*17、列出员工表中每个部门的员工数,和部门no */select count(ename), deptno from scott.emp group by deptno/*18、得到工资大于自己部门平均工资的员工信息*/select *from scott.emp ewhere sal > (select avg(sal) from scott.emp where e.deptno = deptno)select *from scott.emp e1,(select avg(sal) sals, deptno from scott.emp group by deptno) e2where sal > salsand e1.deptno = e2.deptno/*19、分组统计每个部门下,每种职位的平均奖金(也要算没奖金的人)和总工资(包括奖金) */select avg(nvl(comm,0)), sum(sal + nvl(comm, 0))from scott.empgroup by deptno,job/*20、笛卡尔集*/select * from scott.emp, scott.dept/*21、显示员工ID,名字,直属主管ID,名字*/select empno,ename,mgr,(select ename from scott.emp e1 where e1.empno = e2.mgr) 直属主管名字from scott.emp e2/*22、DEPT表按照部门跟EMP表左关联*/select *fromscott.dept, scott.empwherescott.dept.deptno = scott.emp.deptno(+)/*23、使用此语句重复的内容不再显示了*/select distinct (job) from scott.emp/*24、重复的内容依然显示*/select *from scott.empUNION ALLselect * from scott.emp/*23和24题和22题是一样的*//*25、只显示了两个表中彼此重复的记录。

oracle练习及答案

oracle练习及答案

oracle练习及答案测试⼀1.SQL*PLUS命令可以控制数据库(no)2.下⾯的语句是否可以执⾏成功(yes)select last_name , job_id , salary as salfrom employees;3.下⾯的语句是否可以执⾏成功(yes)select * from employees;4.找出下⾯语句中的错误select employee_id , last_namesal * 12 ANNUAL SALARYfrom employees;列于列之间未⽤逗号分隔别名应⽤引号括起来5.显⽰表departments的结构,并查询其中的全部数据desc departmentsselect * from departments;6.显⽰出表employees中的全部job_id(不能重复)select distinct job_idfrom employees;7.显⽰出表employees的全部列,各个列之间⽤逗号连接,列头显⽰成OUT_PUTselect EMPLOYEE_ID||','|| FIRST_NAME||','||LAST_NAME||','||EMAIL||','||PHONE_NUMBER||','||HIRE_DATE||','||JOB_ID||','||SALARY||','||COMMISSION_PCT||','||MANAGER_ID||','|| DEPARTMENT_ID as "OUT_PUT" from employees;测试⼆1.查询⼯资⼤于12000的员⼯姓名和⼯资SELECT FIRST_NAME, salaryFROM employeesWHERE salary > 12000;2.查询员⼯号为176的员⼯的姓名和部门号SELECT FIRST_NAME, department_idFROM employeesWHERE employee_id = 176;3.选择⼯资不在5000到12000的员⼯的姓名和⼯资SELECT FIRST_NAME, salaryFROM employeesWHERE salary NOT BETWEEN 5000 AND 12000;4.选择雇⽤时间在1998-02-01到1998-05-01之间的员⼯姓名,job_id和雇⽤时间SELECT FIRST_NAME, job_id, hire_dateFROM employeesWHERE hire_date BETWEEN '01-2⽉-98' AND '01-5⽉-98';5.选择在20和50号部门⼯作的员⼯姓名和部门号SELECT FIRST_NAME, department_idFROM employeesWHERE department_id IN (20, 50);6.选择在1994年雇⽤的员⼯的姓名和雇⽤时间SELECT FIRST_NAME, hire_dateFROM employeesWHERE hire_date LIKE '%94';7.选择公司中没有管理者的员⼯姓名及job_idSELECT FIRST_NAME, job_idFROM employeesWHERE manager_id IS NULL;8.选择公司中有奖⾦的员⼯姓名,⼯资和奖⾦SELECT FIRST_NAME, salary, commission_pctFROM employeesWHERE commission_pct IS NOT NULL;9.选择员⼯姓名的第三个字母是a的员⼯姓名SELECT FIRST_NAMEFROM employeesWHERE FIRST_NAME LIKE '__a%';10.选择姓名中有字母a和e的员⼯姓名SELECT FIRST_NAMEFROM employeesWHERE (FIRST_NAME LIKE '%e%a%' OR FIRST_NAME LIKE '%a%e%')测试三1.显⽰系统时间Select sysdate "Date" from dual2.查询员⼯号,姓名,⼯资,以及⼯资提⾼百分之20%后的结果(new salary)select empno,ename,sal, round(sal*1.20,0) as“new salary” from emp;3.将员⼯的姓名按⾸字母排序,并写出姓名的长度(length)select ename "Name" ,length(ename) "Length" from emp order by substr(ename,1,1);4.查询各员⼯的姓名,并显⽰出各员⼯在公司⼯作的⽉份数(worked_month)。

oracle sql语句练习题

oracle sql语句练习题
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);
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 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 emp) boss

oracle sql练习题

oracle sql练习题

oracle sql练习题1. 编写一个SQL查询,找出员工表中工资最高的员工的姓名和工资。

```SELECT ename, salFROM empWHERE sal = (SELECT MAX(sal) FROM emp);```2. 编写一个SQL查询,计算出每个部门的平均工资,并按照平均工资降序排列。

```SELECT deptno, AVG(sal) as avg_salaryFROM empGROUP BY deptnoORDER BY avg_salary DESC;```3. 编写一个SQL查询,找出没有任何员工的部门(即部门中没有员工记录的部门)。

```SELECT d.deptno, d.dnameFROM dept dLEFT JOIN emp e ON d.deptno = e.deptnoWHERE e.deptno IS NULL;```4. 编写一个SQL查询,找出在每个部门中薪资排名第二高的员工的姓名和工资。

```SELECT d.dname, e.ename, e.salFROM emp eINNER JOIN dept d ON e.deptno = d.deptnoWHERE e.sal = (SELECT DISTINCT salFROM empWHERE deptno = e.deptnoORDER BY sal DESCOFFSET 1 ROW FETCH FIRST 1 ROW ONLY);```5. 编写一个SQL查询,找出拥有部门管理权限(即至少管理一个部门)且工资不超过5000的员工的姓名。

```SELECT enameFROM empWHERE empno IN (SELECT DISTINCT mgrFROM empWHERE sal <= 5000);```6. 编写一个SQL查询,找出在工资表中有重复记录的员工姓名和工资。

```SELECT ename, salFROM empGROUP BY ename, salHAVING COUNT(*) > 1;```7. 编写一个SQL查询,找出至少在两个部门工作过的员工的姓名。

ORACLE数据库及SQL语言考试题一(含答案)

ORACLE数据库及SQL语言考试题一(含答案)

ORACLE 数据库及SQL 语言考试题及答案考试试题说明:试题包括三类,名词解释、ORACLE 数据库知识问答、SQL 语句编写,主要用于考察新同事ORACLE 数据库知识和SQL 语言掌握情况。

名词解释可以在回答中阐明名词的定义和你所了解的任何相关信息,没有字数限制,但避免长篇大论,简要描述即可。

ORACLE 数据库知识问答,重点在于切中要害,回答按点给分,每题2分。

SQL 语句编写检查大家的实际SQL 语句编写能力及掌握情况,注意格式规范,要清晰易读。

一、名词解释一、名词解释1. 数据库数据库是按照数据结构来组织、存储和管理数据的仓库。

2. 实例实例是一组Oracle 后台进程/线程以及一个共享内存区,这些内存由同一个计算机上运行的线程/进程所共享。

3. 表空间表空间是数据库的逻辑划分,用于存放数据库对象,主要是数据表,所以称作表空间。

ORACLE 自身存在一些表空间,如system 、user 和undo 表空间。

数据库用户也可以自己定义自己的表空间,并为每个表空间分配对应的数据文件。

4. 索引索引是对数据库表中一列或多列的值进行排序的一种结构,使用索引可快速访问数据库表中的特定信息。

索引不论逻辑上和物理上都与相关的表的数据无关,索引需要独立的存储空间,所以索引在创建之初就需要设置对应的表空间。

二、ORACLE 数据库知识问答1. 数据表Pirmary Key 和Unique Key 的作用和区别作用:Pirmary Key 和Unique Key 都是为数据表提供唯一性约束。

区别:Primary key 的1个或多个列必须为NOT NULL ,如果列为NULL ,在增加PRIMARY KEY 时,列自动更改为NOT NULL 。

而UNIQUE KEY 对列没有此要求。

一个表只能有一个PRIMARY KEY ,但可以有多个UNIQUE KEY 。

2. dos 模式下数据库用户备份、恢复命令数据库备份:exp 用户名/密码@连接标识符数据库恢复:imp 用户名/密码@连接标识符3. 列举五个常用的集合函数max,min,sum,avg,count4. round 与trunc 在处理数字方面的区别,并举例说明round 返回四舍五入后的值,而trunc 返回截取后的值,不进行四舍五入。

oracle的sql练习题

oracle的sql练习题

oracle的sql练习题1. 编写SQL查询语句,从员工表(EMPLOYEES)中选择工资(SALARY)大于5000的员工信息,按照工资的降序排列。

```sqlSELECT * FROM EMPLOYEES WHERE SALARY > 5000 ORDER BY SALARY DESC;```2. 编写SQL查询语句,从部门表(DEPARTMENTS)中选择部门名称(DEPARTMENT_NAME)、部门位置(LOCATION_ID)以及该部门员工的数量,按照员工数量的升序排列。

```sqlSELECT DEPARTMENT_NAME, LOCATION_ID, COUNT(*) AS EMPLOYEE_COUNTFROM DEPARTMENTSJOIN EMPLOYEES ON DEPARTMENTS.DEPARTMENT_ID = EMPLOYEES.DEPARTMENT_IDGROUP BY DEPARTMENT_NAME, LOCATION_IDORDER BY EMPLOYEE_COUNT ASC;```3. 编写SQL查询语句,从员工表(EMPLOYEES)中选择员工的姓名(FIRST_NAME)以及所属部门的名称(DEPARTMENT_NAME),要求只选择属于部门名称以"E"开头的员工信息。

```sqlSELECT FIRST_NAME, DEPARTMENT_NAMEFROM EMPLOYEESJOIN DEPARTMENTS ON EMPLOYEES.DEPARTMENT_ID = DEPARTMENTS.DEPARTMENT_IDWHERE DEPARTMENT_NAME LIKE 'E%';```4. 编写SQL查询语句,从员工表(EMPLOYEES)中选择员工的姓名(FIRST_NAME)以及所属部门的名称(DEPARTMENT_NAME),要求只选择属于部门名称以"A"结尾的员工信息,且员工的工资(SALARY)在3000到6000之间。

Oracle的sql语句练习题含答案

Oracle的sql语句练习题含答案

Oracle的sql语句练习题含答案--1、选择部门30中的雇员select * from emp where deptno=30;--2、列出所有办事员的姓名、编号和部门select ename,empno,dname from emp e inner join dept d on e.deptno = d.deptno where job=upper('clerk‟);--3、找出佣金高于薪金的雇员select * from emp where comm>sal;--4、找出佣金高于薪金60%的雇员select * from emp where comm>sal*0.6--5、找出部门10中所有经理和部门20中的所有办事员的详细资料select * from emp where (deptno=10 and job=upper('manager')) or (deptno=20 and job=upper('clerk '));--6、找出部门10中所有经理、部门20中所有办事员,既不是经理又不是办事员但其薪金>=2000的所有雇员的详细资料select * from emp where (deptno=10 and job=upper('manager')) or (deptno=20 and job=upper('clerk ')) or (job<>upper(…manager‟) and job<>upper(…clerk‟) and sal>=2000)--7、找出收取佣金的雇员的不同工作select distinct job from emp where comm>0;--8、找出不收取佣金或收取的佣金低于100的雇员select * from emp where nvl(comm,0)<100;--9、找出各月最后一天受雇的所有雇员select * from emp where hiredate= last_day(hiredate);--10、找出早于25年之前受雇的雇员select * from emp where months_between(sysdate,hiredate)/12>25;select * from emp where hiredate<ADD_MONTHS(SYSDATE,-12*25);< P>--11、显示只有首字母大写的所有雇员的姓名select ename from emp where ename=initcap(ename);--12、显示正好为6个字符的雇员姓名select ename from emp where length(ename)=6--13、显示不带有'R'的雇员姓名Select ename from emp where ename not like …%R%‟;Select ename from emp where instr(ename,‟R‟)=0;--14、显示所有雇员的姓名的前三个字符select substr(ename,1,3) from emp--15、显示所有雇员的姓名,用a替换所有'A'Select replace(ename,‟A‟,‟a‟) from emp--16、显示所有雇员的姓名以及满10年服务年限的日期Select ename,add_months(hiredate,12*10) …服务年限的日期‟ from e mp--17、显示雇员的详细资料,按姓名排序Select * from emp order by ename--18、显示雇员姓名,根据其服务年限,将最老的雇员排在最前面Select ename from emp order by hiredate--19、显示所有雇员的姓名、工作和薪金,按工作的降序顺序排序,而工作相同时按薪金升序Select ename,job,sal from emp order by job desc ,sal asc--20、显示所有雇员的姓名和加入公司的年份和月份,按雇员受雇日所在月排序,将最早年份的项目排在最前面select ename,to_char(hiredate,'yyyy'),to_char(hiredate,'mm') from emp order by hiredate asc--21、显示在一个月为30天的情况下所有雇员的日薪金select ename,sal/30 from emp;--22、找出在(任何年份的)2月受聘的所有雇员select * from emp where to_char(hiredate,'mm')='02';--23、对于每个雇员,显示其加入公司的天数select ename,sysdate-hiredate from emp--24、显示姓名字段的任何位置,包含"A" 的所有雇员的姓名select ename from emp where ename like '%A%';select ename from emp where instr(ename,‟A‟,1)>0;--25、以年、月和日显示所有雇员的服务年限Select months_between(sysdate,hiredate)/12 as “年”,months_between(sysdate,hiredate) as “月”, sysdate-hiredate as “日” from emp--1、列出至少有一个雇员的所有部门select distinct dname from dept where deptno in (select distinct deptno from emp);--2、列出薪金比"SMITH"多的所有雇员select ename,sal from emp where sal>(select sal from emp whereename=upper('smith'));--3、列出所有雇员的姓名及其直接上级的姓名select e.ename,m.ename from emp e,emp m where e.mgr=m.empno(+);--4、列出入职日期早于其直接上级的所有雇员select ename from emp e where hiredate<(select hiredate from emp where empno=e.mgr);--5、列出部门名称和这些部门的雇员,同时列出那些没有雇员的部门select dname,ename from dept d left join emp e on d.deptno=e.deptno;--6、列出所有“CLERK”(办事员)的姓名及其部门名称select ename,dname from emp e left join dept d on e.deptno=d.deptno wherejob=upper('clerk');--7、列出各种工作类别的最低薪金,显示最低薪金大于1500的记录select job,min(sal) from emp group by job having min(sal)>1500;--8、列出从事“SALES”(销售)工作的雇员的姓名,假定不知道销售部的部门编号select ename from emp where deptno = (select deptno from dept wheredname=uppder('SALES'))--9、列出薪金高于公司平均水平的所有雇员select ename from emp where sal>(select avg(sal) from emp);--10、列出与“SCOTT”从事相同工作的所有雇员select ename from emp where job=(select job from emp whereename=upper('scott'));--11、列出某些雇员的姓名和薪金,条件是他们的薪金等于部门30中任何一个雇员的薪金select ename,sal from emp where sal in (select sal from emp where deptno=30);--12、列出某些雇员的姓名和薪金,条件是他们的薪金高于部门30中所有雇员的薪金select ename ,sal from emp where sal>(select max(sal) from emp where deptno=30);--13、列出每个部门的信息以及该部门中雇员的数量select d.deptno,dname,count(ename) from dept d left join emp e on(d.deptno=e.deptno)group by d.deptno,dname--14、列出所有雇员的雇员名称、部门名称和薪金Select e.ename,d.dname,e.sal from emp e left join dept d on (d.deptno=e.deptno)--15、列出从事同一种工作但属于不同部门的雇员的不同组合Select tba.ename,tbb.ename,tba.job,tbb.job,tba.deptno,tba.deptnoFrom emp tba,emp tbbWhere tba.job=tbb.job and tba.deptno<>tbb.deptno--16、列出分配有雇员数量的所有部门的详细信息,即使是分配有0个雇员Select dept.deptno,dname,loc,count(empno)From dept,empWhere dept.deptno=emp.deptno(+)Group by dept.deptno,dname,loc--17、列出各种类别工作的最低工资Select min(sal) from emp group by job--18、列出各个部门的MANAGER(经理)的最低薪金Select deptno,min(sal) from emp where job=upper(…manager‟) group by deptno--19、列出按年薪排序的所有雇员的年薪select (sal+nvl(comm,0))*12 as avn from emp order by avn--20、列出薪金水平处于第四位的雇员Select * from (Select ename,sal, rank() over (order by sal desc) as grade from emp) where grade=4。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

--1、选择部门30中的雇员select * from emp where deptno=30;--2、列出所有办事员的姓名、编号和部门select ename,empno,dname from emp e inner join dept d on e.deptno = d.deptno where job=upper('clerk‟);--3、找出佣金高于薪金的雇员select * from emp where comm>sal;--4、找出佣金高于薪金60%的雇员select * from emp where comm>sal*0.6--5、找出部门10中所有经理和部门20中的所有办事员的详细资料select * from emp where (deptno=10 and job=upper('manager')) or (deptno=20 and job=upper('clerk '));--6、找出部门10中所有经理、部门20中所有办事员,既不是经理又不是办事员但其薪金>=2000的所有雇员的详细资料select * from emp where (deptno=10 and job=upper('manager')) or (deptno=20 and job=upper('clerk ')) or (job<>upper(…manager‟) and job<>upper(…clerk‟) and sal>=2000) --7、找出收取佣金的雇员的不同工作select distinct job from emp where comm>0;--8、找出不收取佣金或收取的佣金低于100的雇员select * from emp where nvl(comm,0)<100;--9、找出各月最后一天受雇的所有雇员select * from emp where hiredate= last_day(hiredate);--10、找出早于25年之前受雇的雇员select * from emp where months_between(sysdate,hiredate)/12>25;select * from emp where hiredate<add_months(sysdate,-12*25);--11、显示只有首字母大写的所有雇员的姓名select ename from emp where ename=initcap(ename);--12、显示正好为6个字符的雇员姓名select ename from emp where length(ename)=6--13、显示不带有'R'的雇员姓名Select ename from emp where ename not like …%R%‟;Select ename from emp where inst r(ename,‟R‟)=0;--14、显示所有雇员的姓名的前三个字符select substr(ename,1,3) from emp--15、显示所有雇员的姓名,用a替换所有'A'Select replace(ename,‟A‟,‟a‟) from emp--16、显示所有雇员的姓名以及满10年服务年限的日期Select ename,add_months(hiredate,12*10) …服务年限的日期‟ from emp--17、显示雇员的详细资料,按姓名排序Select * from emp order by ename--18、显示雇员姓名,根据其服务年限,将最老的雇员排在最前面Select ename from emp order by hiredate--19、显示所有雇员的姓名、工作和薪金,按工作的降序顺序排序,而工作相同时按薪金升序Select ename,job,sal from emp order by job desc ,sal asc--20、显示所有雇员的姓名和加入公司的年份和月份,按雇员受雇日所在月排序,将最早年份的项目排在最前面select ename,to_char(hiredate,'yyyy'),to_char(hiredate,'mm') from emp order by hiredate asc --21、显示在一个月为30天的情况下所有雇员的日薪金select ename,sal/30 from emp;--22、找出在(任何年份的)2月受聘的所有雇员select * from emp where to_char(hiredate,'mm')='02';--23、对于每个雇员,显示其加入公司的天数select ename,sysdate-hiredate from emp--24、显示姓名字段的任何位置,包含"A" 的所有雇员的姓名select ename from emp where ename like '%A%';select ename from emp where instr(ename,‟A‟,1)>0;--25、以年、月和日显示所有雇员的服务年限Select months_between(sysdate,h iredate)/12 as “年”, months_between(sysdate,hiredate) as “月”, sysdate-hiredate as “日” from emp--1、列出至少有一个雇员的所有部门select distinct dname from dept where deptno in (select distinct deptno from emp);--2、列出薪金比"SMITH"多的所有雇员select ename,sal from emp where sal>(select sal from emp where ename=upper('smith'));--3、列出所有雇员的姓名及其直接上级的姓名select e.ename,m.ename from emp e,emp m where e.mgr=m.empno(+);--4、列出入职日期早于其直接上级的所有雇员select ename from emp e where hiredate<(select hiredate from emp where empno=e.mgr);--5、列出部门名称和这些部门的雇员,同时列出那些没有雇员的部门select dname,ename from dept d left join emp e on d.deptno=e.deptno;--6、列出所有“CLERK”(办事员)的姓名及其部门名称select ename,dname from emp e left join dept d on e.deptno=d.deptno where job=upper('clerk');--7、列出各种工作类别的最低薪金,显示最低薪金大于1500的记录select job,min(sal) from emp group by job having min(sal)>1500;--8、列出从事“SALES”(销售)工作的雇员的姓名,假定不知道销售部的部门编号select ename from emp where deptno = (select deptno from dept where dname=uppder('SALES'))--9、列出薪金高于公司平均水平的所有雇员select ename from emp where sal>(select avg(sal) from emp);--10、列出与“SCOTT”从事相同工作的所有雇员select ename from emp where job=(select job from emp where ename=upper('scott'));--11、列出某些雇员的姓名和薪金,条件是他们的薪金等于部门30中任何一个雇员的薪金select ename,sal from emp where sal in (select sal from emp where deptno=30);--12、列出某些雇员的姓名和薪金,条件是他们的薪金高于部门30中所有雇员的薪金select ename ,sal from emp where sal>(select max(sal) from emp where deptno=30);--13、列出每个部门的信息以及该部门中雇员的数量select d.deptno,dname,count(ename) from dept d left join emp e on (d.deptno=e.deptno)group by d.deptno,dname--14、列出所有雇员的雇员名称、部门名称和薪金Select e.ename,d.dname,e.sal from emp e left join dept d on (d.deptno=e.deptno)--15、列出从事同一种工作但属于不同部门的雇员的不同组合Select tba.ename,tbb.ename,tba.job,tbb.job,tba.deptno,tba.deptnoFrom emp tba,emp tbbWhere tba.job=tbb.job and tba.deptno<>tbb.deptno--16、列出分配有雇员数量的所有部门的详细信息,即使是分配有0个雇员Select dept.deptno,dname,loc,count(empno)From dept,empWhere dept.deptno=emp.deptno(+)Group by dept.deptno,dname,loc--17、列出各种类别工作的最低工资Select min(sal) from emp group by job--18、列出各个部门的MANAGER(经理)的最低薪金Select deptno,min(sal) from emp where job=upper(…manager‟) group by deptno--19、列出按年薪排序的所有雇员的年薪select (sal+nvl(comm,0))*12 as avn from emp order by avn--20、列出薪金水平处于第四位的雇员Select * from (Select ename,sal, rank() over (order by sal desc) as grade from emp) where grade=4。

相关文档
最新文档