SQL jobs

select * from emp;
--1. Calculate how many days an employee has
hired select ename,hiredate,(sysdate-hiredate)days from emp;
--2. Calculate how many months an employee has been
hiredselect ename,hiredate,months_between(sysdate,hiredate) from emp;
--3. Calculate the employee's year-end bonus
select sum(comm) from emp;
--4. Calculate the salary after the salary increase according to the employee's position
---a) If the position is Analyst (analyst), the salary increase is 10%
    select sal*1.1 from emp where job ='ANALYST';
--b) If the position is Programmer, salary increase by 5%
     select sal*1.05 from emp where job ='PROGRAMMER';
--c) If the position is clerk, salary increase 2%
      select sal*1.02 from emp where job ='CLERK';
--d) Other positions remain unchanged --5
 
. Calculate the salary of the employee comprehensively
select sum(sal) from emp;
--6. Calculate the number of employees, Salary comprehensive, average salary
select count(*), sum(sal), avg(sal) from emp;
--7. Calculate the maximum and minimum salary of employees
select max(sal), min(sal) from emp;
--8. Calculate the maximum and minimum salary of each department by department
select max(sal), min(sal ) from emp group by deptno;
--9. Calculate the comprehensive salary and average salary of each department
  select sum(sal),avg(sal) from emp group by deptno;
--10. Group by position, find the average salary of each position The highest and lowest salary and number of people
   select job,max(sal),min(sal),count(empno) from emp group by job;
--11. Calculate the department data with an average of more than 5,000 yuan, no department is not included
   select avg(sal),count(empno) from emp group by deptno
    having avg(sal)>5000 and count(empno) is not null;
--12. Count those positions with more than 2 people
    select job,count(job) from emp group by job having count(*)>2;
--13. Set the length of ename to 10. If it is not enough to the left, use * to fill in LPAD (splicing)
  select LPAD(ename,10,'*') from emp;
- -14. Find the number of the last day of the month
select last_day(sysdate) from dual;
--15. What positions are there in the R&D department?
select distinct(job) from emp where deptno in(select
deptno from dept where dname ='RESEARCH');
--16. Find who has the highest salary in each department
   select ename,sal,job,deptno from emp where(deptno ,sal)
   in(select deptno,max(sal)from emp where deptno is not
   null group by deptno);
--17. Which departments have higher average salary than department 20 average salary?
  select deptno from emp group by deptno having avg(sal)>
  (select avg(sal) from emp where deptno=20);
--18. List the names and positions of employees whose average salary is greater than 2500 in their department
   select eename ,e.job from empe,
(select avg(sal) as mysal,deptno from emp group
by deptno having avg(sal)>2500)s
where e.deptno =s.deptno;
--19. Which employees are paid less than the average salary of the department?
--Method 1
  select ename,sal,deptno from emp a where sal<(select
  avg(nvl(sal,0))from emp where deptno=a.deptno) --Method
2
  select ename,sal from emp e,(select avg(sal)mysal,deptno
from emp group by deptno )s where e.deptno =s.deptno and e.sal<s.mysal; --20
. who is the manager of others
select ename,DEPTNO from emp where job ='MANAGER';

select ename from emp e where e.empno in(
select distinct( mgr) from emp where mgr is not null);
--21. Which departments have no employees
select * from dept where deptno
not in (select deptno from emp group by deptno); --list


employee name and city  --method
(equi-join)
select ename,loc from emp,dept where emp.deptno=dept.deptno; --inner
join
select ename,loc from emp inner join dept on
emp.deptno=dept.deptno --list

the employee's name and his supervisor's name
select e.ename from emp e inner join emp a on e.mgr=a.empno; --List

the name of the employee and the department he is in, and check out the employees who do not have a department
    select e.ename,d.deptno, d.dname from emp e left join
dept d on e.deptno=d.deptno; --List

the name of the employee and the name of the department he is in, and check out the employees without a department
select e.ename,d.deptno ,d.dname from emp e right join
dept d on e.deptno=d.deptno;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326463868&siteId=291194637