oracle simple query and limited query

1. In SCOTT mode, use the "selection" and "projection" operators to realize the following query operations:

(1) Display the employee's name and date of entry;

select ename,hiredate from emp

(2) Display employee number, name, manager number;

select empno,ename,mgr from emp

(3) Query the number, name, date of employment, position and annual basic salary information of each employee;

select empno,ename,hiredate,job,sal*12 from emp

(4) Query whether the monthly bonus is 0 or empty employee number, name, bonus, department number;

select empno,ename,comm,deptno from emp where comm=0 or comm is null

(5) Query the name, date of entry, and department number of the employee whose entry date is 1981-2-20;

select ename,hiredate,deptno from emp where hiredate='20-2月-81'

(6) Display the details of all managers ('MANAGER') in department No. 10 and all employees ('CLERK') in department No. 20;

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

(7) Display the salary of each department manager ('MANAGER');

select empno,ename,deptno,sal from emp where job='MANAGER'

(8) Query the information of employees whose monthly bonus is higher than 60% of the monthly basic salary;

select * from emp where comm>(0.6*sal)

(9) Display the name and position of the employee whose name is 5 characters;

select ename,job from emp where length(ename)=5

(10) Display the name, monthly bonus and annual salary of employees whose names end with 'TH';

select ename,comm,sal*12 from emp where ename like'%TH'

(11) Display the number, name, and employment date of employees whose employee numbers start with '7' and end with '9' without '3' and '4' in the middle.

select empno,ename,hiredate from emp where regexp_like (empno,'^7[^3-4]{2}9$');--or
select empno,ename,hiredate from emp where regexp_like(empno,'^7[^3-4]{1,}9$');--or
select empno,ename,hiredate from emp where regexp_like(empno,'^7[^3-4]+9$');

2. In SCOTT mode, master various common limited query operators:

(1) Query employee information whose salary (sal) is not equal to 3000, 950 and 800 at the same time;

select * from emp where sal!=all(3000,950,800)

(2) Query the information of all employees hired in 1981 in the emp table;

select * from emp where hiredate between '01-1月-1981' and '31-12月-1981'

(3) Query the employee information in the emp table where the first character of the position (job) is 'S', the third character is 'L', and the fifth character is 'S';

select * from emp where job like'S_L_S%'

(4) Query the details of all managers in department 10, all clerks in department 20, and all employees who are neither managers nor clerks but whose monthly salary is greater than or equal to 2000;

select * from emp where (deptno=10 and job='manager') or (deptno=20 and job='clerk') or 
(job not in('manager','clerk') and (sal+comm)>=2000)

(5) Query the information of employees who have no monthly bonus or whose monthly bonus is less than 300;

select * from emp where (comm is null or comm<300)

(6) Query the employee number, name and position, sort according to the position, and arrange in descending order of the monthly basic salary for the same position;

select * from emp order by job,sal desc

(7) Query each position and the number of personnel.

select job,count(empno) from emp group by job

3. In SCOTT mode, master various common functions:

(1) Query the name and monthly salary of the employee ending with S

select ename,sal+nvl(comm,0) from emp where ename like'%S'

(2) Find the length of the ename column, query the employee number, name, and length (press smith to find the employee, display the number, name, and lowercase name)

select ename,empno,length(ename),lower(ename) from emp

(3) Query number, name, second character (query employees whose second character of ename is M, display number, name (substr))

select ename,empno,substr(ename,2,1) from emp

(4) Intercept the employee's name from M, query the employee's name and the intercepted name (instr and substr)

select ename,substr(ename,instr(ename,'M')) from emp

(5) After increasing the employee's salary by 500, calculate the percentage increase from the original salary, round to two decimal places, and display the employee number, name, salary, percentage (for example: 20%)

select empno,ename,sal,round(500/sal*100,2)||'%' from emp

(6) Query the employees who joined in the second half of 1981

select * from emp where hiredate between to_date(19810731,'YYYY-MM-DD')and to_date(19811231,'YYYY-MM-DD')

(7) If the salary is <1000, it will be displayed as 'Level 1', from 1001 to 2000 will be displayed as 'Level 2', from 2001 to 3000 will be displayed as 'Level 3', and others will be displayed as 'Level 4'

select sal,case when sal<1000 then '1级' when sal<2000 then '2级' when sal<3000 then '3级' else '4级' end case from emp

(8) Use case and decode respectively, if there is a commission, it will be displayed as 'with commission', if there is no commission, it will be displayed as 'no commission'

select comm,case when nvl(comm,0)>0 then '有提成' else '没有提成' end case from emp

(9) Replace the 4 digits in the middle of the phone number with the * number (dual form) (substr, replace)

select replace ('18320633333',substr('18320633333',4,4),'****') from dual

4. In SCOTT mode, master grouping and sorting:

(1) Query the employee number, name and length of service, and rank the oldest employee at the top according to his length of service;

select empno,ename,hiredate from emp order by hiredate

(2) Query all departments that have at least one employee;

select deptno,count(*) from emp group by deptno having count(*)>=1

(3) Query the number of employees who are engaged in the same job but belong to different departments;

Students who will meet can type it out in the comment area, I won’t┭┮﹏┭┮

(4) List the minimum salary of MANGER (manager) in each department;

select deptno,min(sal) from emp where job='MANAGER' group by deptno

(5) List the employees whose salary ranks fourth.

select empno,ename from (select rownum num,empno,ename from emp order by sal desc) where num=4

Guess you like

Origin blog.csdn.net/sinat_62012394/article/details/128960854