1)select employee name,id and deptname. (http://www.csee.umbc.edu/help/oracle8/s erver.
815/a67779/ch4l.htm) (http://information.utep.edu/mmahmood/cis4365/slides/CHAP08.ppt#264,4,Natural Jo in Example) http://www.doc.mmu.ac.uk/STAFF/P.Quick/sql3.ppt Sol: ---select ename,emp.deptno,dname from emp,dept where (emp.deptno,dname) in (select deptno,dname from dept) 2)display the employee details who earns more salary than 'JONES' salary from em p table. sol: --select ename,sal from emp where sal > (select sal from emp where empno=7566) 3)Display the names of all employees who earn less than the average salary in th e company. sol: --select ename,sal,job from emp where sal < (select avg(sal) from emp); 4)Display the details of the employees who are managed by the same manager and w ork in the same department as the employees with EMPLOYEEID 7654 and 7902. (pairwise comparision subquery) sol: --select ename,empno,job,mgr,deptno from emp where (mgr,deptno) in (select mgr,deptno from emp where empno in (7654,7902)) and empno not in (7654,7902) 5)Display the details of the employees who are managed by the same manager as th e employees with EMPLOYEEID 7654 or 7902 and work in the same department as the employees with EMPLOYEEID 76 54 or 7902. sol: ---select ename,empno,job,mgr,deptno from emp where mgr in (select mgr from emp where empno in (7654,7844)) and deptno in( select deptno from emp where empno in (7654,7844)) and empno not in (7654,7844);
6)selects the name, job, salary and department number of all employees except sa
les people from department number 30. sol: --SELECT ename, job, sal, deptno FROM emp WHERE NOT (job = 'SALESMAN' AND deptno = 30); 7)Display the employee details who works in 'BLAKE''s department. sol: ---SELECT ename, deptno FROM emp WHERE deptno = (SELECT deptno FROM emp WHERE ename = 'BLAKE') and ename not like 'BLAKE'; 8)display the employee last names,salaries, department numbers, and average sal aries for all the employees who earn more than the average salary in their department sol: ---select a.ename,a.sal,a.deptno,b.avgsal from emp a,(select deptno,avg(sal) avgsal from emp group by deptno) b where a.deptno=b.deptno and a.sal >b.avgsal; 9)find all emp records whose department location is DALLAS sol: ---SELECT emp.ename, emp.JOB, emp.deptno FROM emp WHERE EXISTS (SELECT 'x' FROM dept d WHERE d.DEPTNO = emp.DEPTNO AND d.LOC = 'DALLAS') 10)Display ename,job,deptno,dname,dloc for all the employees. (A scalar sub-query returns only a single column and a single row) sol: ---SELECT emp.ename, emp.JOB, emp.deptno, (select dname from dept where dept.deptno = emp.deptno) dname, (select loc from dept where dept.deptno = emp.deptno) dloc FROM emp; (or) SELECT emp.ename, emp.JOB,
emp.deptno, dnames.dname, dloc.loc FROM emp JOIN (select dname,deptno from dept ) dnames ON emp.deptno = dnames.deptno JOIN (select loc,deptno from dept) dloc on emp.deptno=dloc.deptno 11)display the department name in which employees are not working. sol: ---select dname from dept where deptno not in (select deptno from emp where deptno is not null); (or) SELECT dname FROM dept WHERE NOT EXISTS (SELECT deptno FROM emp WHERE emp.deptno = dept.deptno)
12)Display the department names along with number of employees working in each d epartment; Sol: ---SELECT d.deptno, d.dname, (SELECT count(*) FROM emp e WHERE e.deptno = d.deptno) AS "Num Dept" FROM dept d; 13)display all the manager details by making use of 'exists' operator. sol: ---select empno,ename,deptno,job from emp outer where exists(select 'x' from emp where mgr=outer.empno); 14)Find all departments that do not have any employees. Sol: --select deptno,dname from dept d where not exists(select 'x' from emp where deptno=d.deptno); 15)Using the WITH clause, write a query to display the department name and total salaries for those departments whose total salary is greater than the average salary across departm ents. sol: ----
with dept_costs AS ( select dname,sum(e.sal) as dept_total from emp e,dept d where e.deptno=d.deptno group by d.dname), avg_costs AS ( select sum(dept_total)/count(*) as dept_avg from dept_costs) select * from dept_costs where dept_total > ( select dept_avg from avg_costs) order by dname; 16)diplay highest and lowest paid employee name and sal details: --------------------------------------------------------------select ename,sal from emp where sal =(select max(sal) from emp) or sal =(select min(sal) from emp); ( or) select ename,sal from emp where sal in (select max(sal) from emp union select min(sal) from emp) 17)What are the names, jobs and salaries of employees with the same job and salary as FORD? SELECT ENAME, JOB, SAL FROM EMP WHERE (JOB, SAL) IN ( SELECT JOB, SAL FROM EMP WHERE ENAME = 'FORD' ); 18)Display the highest paid employee. SELECT JOB, ENAME FROM EMP WHERE SAL >= ALL (SELECT SAL FROM EMP); JOB ENAME PRESIDENT KING 19)Display employees whose salary is greater than all employees in department 30: SELECT ENAME, SAL FROM EMP WHERE SAL > ALL (SELECT SAL FROM EMP WHERE DEPTNO = 30); 20)List the employees who were not managers: select ename from emp where empno not in (select mgr from emp where mgr is not null)
Note: whenever we use subqueries with "not in" operator we need to ensure that t he subquery should not return the null in to the result 21)List the employees who are managers: select ename from emp e where exists (select 'x' from emp where e.empno=mgr); 22)Display the names of all employees who have a JOB category such that there are employees in that category in both Department 20 and Department 30: SELECT JOB, ENAME, DEPTNO FROM EMP WHERE JOB IN ( SELECT JOB FROM EMP WHERE DEPTNO = 20 INTERSECT SELECT JOB FROM EMP WHERE DEPTNO = 30); 23)For all the employees wjo have a salary that is more than the average salary of the employees in their own department and list them in department order. SELECT DEPTNO, ENAME, SAL FROM EMP X WHERE SAL > (SELECT AVG(SAL) FROM EMP WHERE X.DEPTNO = DEPTNO) ORDER BY DEPTNO; 24)third highest salary: (VVVVVImp ------------------------Select * From Emp E1 Where 2 = (Select Count(Distinct(E2.Sal)) From Emp E2 Where E2.Sal > E1.Sal)
chapter-18(oca-exercises) -------------------------1)Write a query to display the ename, department number, and salary of any emplo yee whose department number and salary both match the department number and salary of any employee who earns a commission.
sol: ---select ename,deptno,sal from emp where (deptno,sal) in (select a.deptno,a.sal from emp a where a.comm is not nul l) 2) Display the ename, department name, and salary of any employee whose salary and commission match the salary and commission of any employee working in deptno =2 0 sol: ---select ename,dname,sal from emp,dept where (sal,comm) in (select a.sal,a.comm from emp a where a.deptno=30) and emp.deptno=dept.deptno 3)Create a query to display the ename, hire date, and salary for all employees w ho have the same job and salary as SCOTT. Note: Do not display SCOTT in the result set. Sol: ---select ename,hiredate,sal from emp where (job,sal)=(select job,sal from emp where ename like 'SCOTT') and ename not like 'SCOTT'