50% found this document useful (2 votes)
315 views8 pages

Single Row Sub Query (Sub Query Returns Single Row) Multiple Row Sub Query (Sub Query Returns Multiple Rows)

This document discusses single row and multiple row subqueries. Single row subqueries return a single value that can be compared to a column in the outer query. Examples include finding employees whose salary is less than a specified employee's salary. Multiple row subqueries return multiple rows/values that can be compared using operators like IN, ANY, ALL. These subqueries are used to filter rows based on comparisons to salary values from other departments. The document also provides examples of queries using each type of subquery.

Uploaded by

Manohar Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
50% found this document useful (2 votes)
315 views8 pages

Single Row Sub Query (Sub Query Returns Single Row) Multiple Row Sub Query (Sub Query Returns Multiple Rows)

This document discusses single row and multiple row subqueries. Single row subqueries return a single value that can be compared to a column in the outer query. Examples include finding employees whose salary is less than a specified employee's salary. Multiple row subqueries return multiple rows/values that can be compared using operators like IN, ANY, ALL. These subqueries are used to filter rows based on comparisons to salary values from other departments. The document also provides examples of queries using each type of subquery.

Uploaded by

Manohar Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 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;

You might also like