oracle练习题(二)

1、列出所有办事员的姓名、编号和部门

select ename,empno,dname from dept,emp where emp.deptno=dept.deptno

2、找出部门10中所有经理和部门20中的所有办事员的详细资料

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

3、找出部门10中所有经理、部门20中所有办事员,既不是经理又不是办事员但其薪金>=2000的所有雇员的详细资料

select * from emp where deptno =10 and job='MANAGER'
UNION
select * from emp where deptno =
20 and job='CLERK'
UNION
SELECT * FROM EMP WHERE JOB NOT IN(
'MANAGER' ,'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 )

4、找出早于25年之前受雇的雇员

SELECT * FROM EMP where (SYSDATE-HIREDATE)/365>25

扫描二维码关注公众号,回复: 2767331 查看本文章

5、显示所有雇员的姓名和加入公司的年份和月份,按雇员受雇日所在月排序,将最早年份的项目排最前面

select ename,to_char(hiredate,'yyyy'),to_char(hiredate,'MM') FROM EMP ORDER BY
to_char(hiredate,
'yyyy'),to_char(hiredate,'MM')

 

如果这么写 则按月份大小排序

select ename,to_char(hiredate,'yyyy'),to_char(hiredate,'MM') FROM EMP
 ORDER BY
to_char(hiredate,
'MM'),to_char(hiredate,'yyyy')排序前后和orderby关系

 

第三种方法:

select ename,to_char(hiredate,'yyyy'),to_char(hiredate,'MM') FROM EMP
 ORDER BY
 hiredate asc

6、以年、月和日显示所有雇员的服务年限

SELECT trunc((SYSDATE-HIREDATE)/365)||'年',
trunc(mod((SYSDATE-HIREDATE),
365)/30)||'月',
trunc(mod(mod((SYSDATE-HIREDATE),
365),30))||'日'
FROM EMP

 

select trunc((sysdate-hiredate)/365) as Äê,
 trunc(mod((sysdate-hiredate),
365)/30) as ÔÂ,
 trunc(mod(mod((sysdate-hiredate),
365),30)) as ÈÕ
 from emp

7、列出从事同一种工作但属于不同部门的雇员的

select * from emp t1,emp t2 where t1.job=t2.job and t1.deptno!=t2.deptno

8、列出分配有雇员数量的所有部门的详细信息,

select * from dept,(
select deptno from emp group by deptno) t
where dept.deptno=t.deptno

9列出每个部门的信息以及该部门中雇员的数量

select * from dept,(
select deptno,count(*) num from emp group by deptno
)t where dept.deptno=t.deptno

10列出至少有一个员工的所有部门

select * from dept,(
select deptno from emp group by deptno) t
where dept.deptno=t.deptno

11列出各个部门的MANAGER(经理)的最低薪金

select MIN(SAL) from emp where job='MANAGER' GROUP BY DEPTNO

12、复制emp表结构及数据(备分表名称为empback);

create table empback
as
select * from emp

13、创建一张与emp表结构一样的表(不需要emp表数据)、

create table empback
as
select * from emp where null=null

14、查询emp表中第31条到40条之间的数据(分页)

select * from (
select rownum n,t.* from (
       select * from emp
) t where rownum <=
20) where n>=10

 

 

select * from emp limit 30,10 mysql数据库分页

15 已知表tabA结构如下,删除表tabA中属性t_name值重复的行

Create table  tabA(

 t_id number,

t_name varchar2(20)

)

Insert into tabA (‘1’,’a);

Insert into tabA (‘2’,’a);

Insert into tabA (‘3’,’b);

Insert into tabA (‘4’,’b);

Insert into tabA (‘5’,’c);

delete from tabA where t_id not in(
select min(t_id) from tabA group by t_name having count(*)>=
2
) and t_name in(
select t_name from tabA group by t_name having count(*)>=
2
)

16、显示雇员表中雇员的姓名,职务,薪水,部门表中的部门名称、地址以及工资等级表中的工资等级

select * from emp e,dept d,salgrade s where e.deptno=d.deptno
and e.sal between losal and hisal

猜你喜欢

转载自blog.csdn.net/qq_39404258/article/details/81303805