0% found this document useful (0 votes)
8 views

Subquery is a Select Statement That is Embedded in a Clause of Another

Uploaded by

vedant.walse41
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Subquery is a Select Statement That is Embedded in a Clause of Another

Uploaded by

vedant.walse41
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Subquery is a select statement that is embedded in a clause of another

SELECT statement i.e. nesting of queries or query within query.

Types of subqueries

1) Single row subqueries

2) Multiple row subqueries

3) Multiple column subqueries

Single row subqueries: A single row subquery is one that returns one

row from inner SELECT statement. This type of subquery uses single

row operators = , > , >= , < , <= , < >

Syntax:

SELECT column_name1… column_name n

FROM <table_name>

WHERE column1 operator (SELECT column from

<table_name> where condition);

Example :

Display the employee details whose job title is the same as that

of employee 1005.

Select empno,ename,job,salary,deptno

From emp

Where job=(select job from emp where empno=1005);

Multiple row subqueries: Subqueries that return more than one row

are called multiple-row subqueries. Multiple row operators are used

instead of a subquery, with a multiple row subquery.

Operator Meaning

IN Equal to any member in the list.

ANY Compare value to each value returned by the

subquery.

ALL Compare value to every value returned by the

subquery.
Syntax:

SELECT column_name1… column_name n

FROM <table_name>

WHERE column1 operator (SELECT column from <table_name>

where condition);

Example

Find the employees who earn the same salary as minimum salary for

departments.

Select empno,ename,job,salary,deptno

From emp

Where salary IN (select min(salary) from emp group by deptno);

Multiple column subqueries

Queries that return the values from more than one column are called

multiple column subqueries.

Syntax:

SELECT column_name1, ….. column_name n

FROM <table_name>

WHERE (column_name, column_name) IN

(SELECT column_name, column_name….from

<table_name> Where <condition> );

Example: Display the name, department number, salary and

commission of any employee whose salary and commission matches

both the commission and salary of any employee in department 10

Query: Select empno,deptno,salary,comm

From emp

Where (salary,comm) IN (select salary,comm from emp where

deptno=10);

You might also like