Sei sulla pagina 1di 2

1.

List the employees working in research department


select * from emp
where deptno=(select deptno from dept where dname='RESEARCH' );

2. List employees who are located in New York and Chicago


select * from emp
where deptno in (select deptno from dept where loc='NEW YORK' or LOC='CHICAGO');

3. Display the department name in which ANALYSTS are working

select * from dept


where deptno in (select deptno from emp
where job='ANALYST');

4.Display employees who are reporting to JONES


select * from emp
where mgr=(select empno from emp
where ename='JONES');

5. Display all the employees who are reporting to Jones Manager


select * from emp
where mgr=(select mgr from emp
where ename='JONES');

6. Display all the managers in SALES and ACCOUNTING department


select * from emp where deptno in (select deptno from dept where dname in
('SALES','ACCOUNTING'));

7. Display all the employee names in Research and Sales Department who are having
at least 1 person reporting to them

select empno, ename, job from emp where deptno in ( select deptno from dept where
dname in ('SALES','RESEARCH')) and
empno in (select mgr from emp);

8. Display all employees who do not have any reportees


select * from emp where empno not in (select mgr from emp where mgr is not null);

9. List employees who are having at least 2 reporting

select empno, ename, job,mgr from emp where empno in (select mgr from emp e group
by mgr
having count(*)>=2);

10. List the department names which are having more than 5 employees

select *from dept where deptno in (select deptno from emp group by deptno having
count(*)>5);

17. Write a query to get 2nd & 6th max salary from EMP table

select * from emp e


where 1=(select count(distinct(e1.sal)) from emp e1
where e.sal<=e1.sal) or 1=(select count(distinct(e1.sal)) from emp e1
where e.sal<=e1.sal);

21. List all the employees whose salaries are greater than their respective
departmental average salary.

select * from emp e where sal>(select avg(sal) from emp d where d.deptno=e.deptno);
*******or*********
select *from emp order by sal desc;
SELECT*
FROM emp e
WHERE e.sal > (SELECT AVG(e1.sal)
FROM emp e1
WHERE e1.deptno = e.deptno);
GROUP BY e.deptno;

11. List department name having at-least 3 salesman


select * from dept
where deptno in (select deptno from emp where job='SALESMAN' group by deptno
having count(*)>=3);

select * from emp;

12. List employees from research and accounting having at-least 2 reporting

select * from emp where deptno in (select deptno from dept where dname in
('RESEARCH','ACCOUNTING') and
empno in (select mgr from emp group by mgr having count(*)>=2 ));

Potrebbero piacerti anche