Sie sind auf Seite 1von 8

SUB QUERY

Single row sub query (Sub

Query returns single row)


Multiple row sub query (Sub
Query returns multiple rows)

Single Row Sub Queries


Display the employees whose salary is less

than the salary of ALLEN


SELECT * FROM EMP WHERE SAL<(SELECT SAL
FROM EMP WHERE ENAME='ALLEN');
Display 2nd max sal from EMP
SELECT MAX(SAL) FROM EMP WHERE
SAL<(SELECT MAX(SAL) FROM EMP);

Display 3RD max sal from EMP


SELEMAX(SAL) FROM EMP WHERE SAL<(SELECT

MAX(SAL) FROM EMP WHERE SAL<(SELECT


MAX(SAL) FROM EMP));
Write a query to find the salary of employees

whose salary is greater than the salary of


employee whose id is 7788?
SELECT EMPNO,ENAME, SAL FROM EMP WHERE SAL
> ( SELECT SAL FROM EMP WHERE EMPNO = 7788);

Write a query to find the employees who all are earning

the highest salary?


SELECT EMPNO, SAL FROM EMP WHERE SAL =
( SELECT MAX(SAL) FROM EMP );
List out the employees who are having salary less than

the maximum salary and also having hiredate greater


than the hiredate of an employee who is having the
maximum salary.
select * from emp where
sal<(select max(sal) from emp) AND
HIREDATE>(SELECT HIREDATE FROM EMP WHERE
SAL=(SELECT MAX(SAL) FROM EMP));

Multiple Row Sub Query


Write a query to find the employees whose

salary is equal to the salary of at least one


employee in department of id 10?
SELECT * FROM EMP WHERE SAL IN
(
SELECT SAL
FROM EMP
WHERE DEPTNO = 10
);

Write a query to find the employees whose

salary is greater than at least on employee in


department of id 500?
SELECT * FROM EMP WHERE SAL > ANY
(
SELECT SAL
FROM EMP
WHERE DEPTNO = 10
);

Write a query to find the employees whose salary

is less than the salary of all employees in


department of id 10?
SELECT *
FROM EMP
WHERE SAL < ALL
(
SELECT SAL
FROM EMP
WHERE DEPTNO = 10
);

Write a query to get the department name of

an employee?
SELECT EMPLOYEE_ID,
DEPARTMENT_ID,
(SELECT DEPARTMENT_NAME
FROM DEPARTMENTS D
WHERE D.DEPARTMENT_ID = E.DEPARTMENT_ID
) DNAME
FROM EMPLOYEES E;

Das könnte Ihnen auch gefallen