Basic exercises database

1, the query all the information department of all employees 30. 

select * from emp where deptno=30;

2, all queries clerks (CLERK) name, number and department number. 

select ename,empno,deptno from emp where job='CLERK';

3, all the information inquiry subsidies higher than the salaries of employees. 

select * from emp where nvl(comm,0) > sal;

4, all the information inquiry subsidy 60% higher than the salary of employees. 

select * from emp where nvl(comm,0) > (sal*0.6);

5, the query in all 10 departments manager (MANAGER) and all the clerks in department 20 (CLERK) for details. 

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


 

6, 10 in all departments query manager (MANAGER), all clerks in department 20 (CLERK), neither managers are not clerks but detailed records of all employees whose salary is greater than or equal to 2000. 

Understanding 1: 10 in all departments query manager (MANAGER), all clerks in department 20 (CLERK), and which is neither a manager in the company are not clerks but the salary is greater than or equal to detailed records of all employees in 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);


 

Understanding 2: 10 in all departments within the query manager (MANAGER), all clerks in department 20 (CLERK), and No. 10, 20 department managers are not neither a clerk but the salary is greater than or equal to detailed records of all employees in 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 And deptno In (10,20));


 

7, charge a different job inquiries grants employees. 

select ename,job,comm from emp where nvl(comm,0) > 0;


 

8, the query does not charge a commission or fee subsidy of less than 100 employees. 

select * from emp where nvl(comm,0) <100;


 

9, the query for all employees each month countdown day 3 employed. 

Select * From emp Where (last_day(hiredate) - hiredate) = 2;


 

10, early in the inquiry staff employed 12 years ago. 

Select * From emp Where to_char(Sysdate,'yyyy') - to_char(hiredate,'yyyy') >= 12;


 

11, the query capitalized displayed the names of all employees. 

Select ename,Initcap(ename) From emp;


 

12, employees of the query exactly five characters of the name. 

Select ename From emp Where Length(ename)=5;


 

13, the query without the employee's name of "R". 

做法1:Select ename From emp Where ename Not Like '%R%';

做法2:Select ename From emp Where Instr(ename,'R')=0; 


 

14、查询所有员工姓名的前三个字符。

Select ename, Substr(ename,1,3) From emp; 


 

15、查询所有员工的姓名,用“a”替换所有“A” 

Select ename,Replace(ename,'A','a') From emp;


 

16、查询满10年服务年限的员工的姓名和受雇日期。 

方法1:如果不考虑到闰年平年的话,10*365可以代表10年。

Select ename, hiredate From emp Where (Sysdate-hiredate)>=10*365;

方法2:如果考虑到闰年平年,10*365是不满足10年的要求。

如果服务年限满10年,那排除月日,sysdate和hiredate在年份上肯定也是>=10

Select ename, hiredate From emp Where (to_char(Sysdate,'yyyy') - to_char(hiredate,'yyyy')) >= 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 Desc; 


 

20、查询所有员工的姓名,加入公司的年份和月份,按受雇日期所在月份排序,若月份相同则将最早年份的员工排在最前面。 

Select ename,to_char(hiredate,'yyyy'),to_char(hiredate,'mm') From emp Order By to_char(hiredate,'mm'),to_char(hiredate,'yyyy');


 

21、查询在一个月尾30天的情况所有员工的日薪金,忽略余数。 

Select ename,Trunc(sal/30) From emp;


 

22、查询在(任何年份的)2月受聘的所有员工。 

Select * From emp Where to_char(hiredate,'mm')=2;


 

23、查询每个员工加入公司的天数。 

Select ename,(Sysdate-hiredate) From emp;


 

24、查询姓名字段的任何位置包含“A”的所有员工的姓名。 

Select ename From emp Where ename Like '%A%';


 

25、查询以年月日的方式显示所有员工的服务年限。(大概)

Select ename,sysdate,hiredate,Round(sysdate-hiredate) 天数,

Nvl(Trunc((sysdate-hiredate)/365),0)||'年'||

Nvl(Trunc((Mod((sysdate-hiredate),365))/30),0)||'月'||

Nvl(Round(Mod((Mod((sysdate-hiredate),365)),30)),0)||'日' 年月日 From emp;


 

作者:kerwin-chyl

文章链接:https:////www.cnblogs.com/kerwin-chyl

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

Guess you like

Origin www.cnblogs.com/kerwin-chyl/p/12366961.html