Answers of Subquery
Answers of Subquery
Answers of Subquery
ON SUBQUERY:
1.DISPLAY ALL THE EMPLOYEES WHOSE DEPARTMENT NAMES ENDING
'S'
SELECT ENAME
FROM EMP
WHERE DEPTNO IN(SELECT DEPTNO
FROM DEPT
WHERE DNAME LIKE ‘%S’);
SELECT ENAME
FROM EMP
WHERE DEPTNO IN(SELECT DEPTNO
FROM DEPT
DNAME LIKE ‘_O%’);
FROM EMP
GROUP BY MGR
HAVING COUNT(*)>=2));
16. WRITE A QUERY TO DISPLAY ALL THE EMPLOYEE WHOSE JOB NOT
SAME AS ALLEN AND SALARY IS GREATER THAN MARTIN.
SELECT ENAME
FROM EMP
WHERE JOB NOT IN(SELECT JOB
FROM EMP
WHERE ENAME =’ALLEN’) AND SAL>( SELECT SAL
FROM EMP
WHERE
ENAME=’MARTIN’));
22. LIST EMPLOYEES WHO WORK FOR SALES DEPARTMENT AND THEIR
SALARY GREATER THAN AVERAGE SALARY OF THEIR DEPARTMENT.
SELECT ENAME
FROM EMP
WHERE DEPTNO=(SELECT DEPTNO
FROM DEPT
WHERE DNAME=’SALES’) AND SAL>(SELECT
AVG(SAL)
FROM EMP
FROM DEPT
WHERE DNAME=’SALES’))
23. LIST EMPLOYEES WHO ARE WORKING IN RESEARCH DEPARTMENT
AND THEY ARE MANAGER.
SSELECT ENAME
FROM EMP
WHERE DEPTNO=(SELECT DEPTNO
FROM DEPT
WHERE DNAME='RESEARCH') AND JOB=’MANAGER’;
27. LIST ALL THE DEPT NAME AND LOC OF ALL THE SALESMAN
MANAGER-MANAGER'S
SELECT DNAME,LOC
FROM DEPT
WHERE DEPTNO IN(SELECT DEPTNO
FROM EMP
WHERE EMPNO IN(SELECT MGR
FROM EMP
WHERE EMPNO IN(SELECT MGR
FROM EMP
WHERE JOB='SALESMAN')));
28. LIST THE EMPLOYEE DEPT NAME AND LOC OF ALL THE
EMPLOYEES WHO ARE CLERK ,REPORTING TO BLAKE AND SALARY IS
LESSER THAN MARTIN SALARY
SELECT DNAME,LOC
FROM DEPT
WHERE DEPTNO IN(SELECT DEPTNO
FROM EMP
WHERE JOB =’CLERK’) AND MGR IN(SELECT EMPNO
FROM EMP
WHERE ENAME =’BLAKE’) AND SAL<(SELECT SAL
FROM EMP
WHERE ENAME =’MARTIN’);
32. LIST ALL THE EMPLOYEES WHOSE JOB IS SAME AS JONES AND
THEIR SALARY LESSER THAN SCOTT
SELECT ENAME
FROM EMP
WHERE JOB=(SELECT JOB
FROM EMP
WHERE ENAME='JONES') AND SAL<(SELECT SAL
FROM EMP
WHERE ENAME='SCOTT');
34.DISPLAY ALL THE EMPLOYEES WHO ARE EARN LESS THAN ANY OF
THE SALESMAN?
SELECT ENAME
FROM EMP
WHERE SAL < ANY (SELECT SAL
FROM EMP
WHERE JOB ='SALESMAN');
35.DISPLAY ALL THE EMPLOYEES WHO ARE JOINED BEFORE THE LAST
PERSON?
SELECT ENAME
FROM EMP
WHERE HIREDATE<(SELECT MAX(HIREDATE)
FROM EMP);
36.DISPLAY ALL THE EMPLOYEES WHO ARE EARNING MORE THAN ANY
OF THE MANAGER.
SELECT ENAME
FROM EMP
WHERE SAL > ANY(SELECT SAL
FROM EMP
WHERE JOB='MANAGER');
SELECT MGR,JOB,DEPTNO
FROM EMP
WHERE COMM IS NULL;
59. DISPLAY ALL THE EMPLOYEES WHO ARE JOINED AFTER BLAKE.
SELECT ENAME
FROM EMP
WHERE HIREDATE>(SELECT HIREDATE
FROM EMP
WHERE ENAME='BLAKE');
60. LIST THE DEPT NAME THAT ARE HAVING AT LEAST 3 EMPLOYEES
BUT NOT MORE THAN 5 EMPLOYEES IN IT.
SELECT DNAME
FROM DEPT
WHERE DEPTNO IN (SELECT DEPTNO
FROM EMP
GROUP BY DEPTNO
HAVING COUNT(*)>=3 AND COUNT (*)>5);
61. DISPLAY THE LOCATION OF ALL EMPLOYEES WHOSE REPORTING
MANAGER SALARY IS GREATER THAN 2000.
SELECT LOC
FROM DEPT
WHERE DEPTNO IN ( SELECT DEPTNO
FROM EMP
WHERE SAL >200 AND EMPNO IN ( SELECT MGR
FROM EMP
WHERE MGR IS NOT NULL));
SELECT ENAME
FROM EMP
WHERE DEPTNO IN(SELECT DEPTNO
FROM DEPT
WHERE DNAME LIKE '%E%E%');
FROM EMP
WHERE JOB IN ‘MANAGER’
AND DEPTNO IN (SELECT DEPTNO
FROM DEPT
WHERE LOC IN ‘CHICAGO’);
70. SELECT ENAME OF EMPLOYEE WHO EARNS 2ND MAX SALARY AND
WORKS FOR LOCATION DALLAS.
SELECT ENAME
FROM EMP
WHERE SAL<(SELECT MAX(SAL)
FROM EMP
WHERE SAL<(SELECT MAX(SAL)
FROM EMP
WHERE DEPTNO IN(SELECT DEPTNO
FROM DEPT
WHERE LOC='DALLAS')));
90.DISPLAY LOC AND DNAME WHOSE JOB IS MANAGER AND HAS SAL
LESS THAN CLERK.
SELECT LOC,DNAME
FROM DEPT
WHERE DEPTNO IN(SELECT DEPTNO
FROM EMP
WHERE JOB='MANAGER' AND SAL < (SELECT SAL
FROM EMP
WHERE JOB='CLERK'));
91.DISPLAY EMPLOYEES LOCATION WHO HAS SOME COMMISSION.
SELECT LOC
FROM DEPT
WHERE DEPTNO IN(SELECT DEPTNO
FROM EMP
WHERE COMM IS NOT NULL);
92.DISPLAY EMPNO, ENAME, JOB, WHOSE JOB HAS 'E' IN IT AND DISPLAY
EMPNO IN DESCENDING ORDER.
SELECT EMPNO,ENAME,JOB
FROM EMP
WHERE JOB LIKE '%E%'
ORDER BY EMPNO DESC;
101. DISPLAY ALL THE EMPLOYEES WHOSE JOB SAME AS 'SMITH' AND
DEPARTMENT SAME AS 'JONES' AND SALARY MORE THAN 'TURNER'
SELECT ENAME
FROM EMP
WHERE JOB IN(SELECT JOB
FROM EMP
WHERE ENAME='SMITH') AND DEPTNO IN(SELECT DEPTNO
FROM DEPT
WHERE DEPTNO IN(SELECT DEPTNO
FROM EMP
WHERE ENAME='JONES')) AND SAL>(SELECT SAL
FROM EMP
WHERE ENAME='TURNER');
102. DISPLAY ALL THE EMPLOYEES WHOSE NAME START WITH 'S' AND
HAVING SALARY MORE THAN 'ALLEN' AND LESS THAN FORD
SELECT ENAME
FROM EMP
WHERE ENAME LIKE 'S%' AND SAL>(SELECT SAL
FROM EMP
WHERE ENAME='ALLEN') AND SAL<(SELECT SAL
FROM EMP
WHERE ENAME='FORD');
103. DISPLAY ALL THE CLERKS AND ANALYST WHO ARE NOT WORKING
FOR 'DALLAS'
SELECT JOB
FROM EMP
WHERE JOB IN ('CLERK','ANALYST') AND DEPTNO NOT IN(SELECT
DEPTNO
FROM DEPT
WHERE LOC='DALLAS');
104. DISPLAY DEPARTMENT NAME WHICH IS HAVING AT LEAST ONE
'MANAGER'
SELECT DNAME
FROM DEPT
WHERE DEPTNO IN(SELECT DEPTNO
FROM EMP
WHERE JOB='MANAGER'
GROUP BY JOB
HAVING COUNT(*)>=1);
108. DISPLAY ALL THE EMPLOYEES WHO ARE EARNING MORE THAN
ALL THE MANAGERS(JOB).
SELECT ENAME
FROM EMP
WHERE SAL> ALL (SELECT SAL
FROM EMP
WHERE JOB='MANAGER');
109. DISPLAY ALL THE EMPLOYEES WHO ARE EARNING MORE THAN
ANY OF THE MANAGER(JOB)
SELECT ENAME
FROM EMP
WHERE SAL>ANY(SELECT SAL
FROM EMP
WHERE JOB='MANAGER');
110. SELECT EMPNO, JOB AND SALARY OF ALL THE ANALYST WHO ARE
EARNING MORE THAN ANY OF THE MANAGER(JOB)
SELECT EMPNO,JOB,SAL
FROM EMP
WHERE JOB='ANALYST' AND SAL>ANY (SELECT SAL
FROM EMP
WHERE JOB='MANAGER');