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

sql_sub_query

The document explains the concept of subqueries in SQL, defining them as queries nested within another query. It covers various types of subqueries, including single row, multiple row, multiple column, nested, and correlated subqueries, along with examples for each type. Additionally, it highlights the use of subqueries with common SQL clauses such as WHERE, SELECT, and FROM.

Uploaded by

SantoshJammi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

sql_sub_query

The document explains the concept of subqueries in SQL, defining them as queries nested within another query. It covers various types of subqueries, including single row, multiple row, multiple column, nested, and correlated subqueries, along with examples for each type. Additionally, it highlights the use of subqueries with common SQL clauses such as WHERE, SELECT, and FROM.

Uploaded by

SantoshJammi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

SQL | Subquery

Follow
Manni Chopra
Sub Query :
If a Query that contains another Query, then the Query inside
the main Query is called a Sub Query and the main Query is
known as the parent Query.

In Oracle the Sub Query will executed on the prior basis and
the result will be available to the parent Query and then the
execution of the parent/main Query takes place. A Sub Query
can also be called a Nested/Inner Query. These Sub Queries
can be used with:

▪ WHERE Clause
▪ SELECT Clause
▪ FROM Clause
Common SQL Clauses for Subqueries :

1. Sub Query using WHERE Clause


SELECT * FROM student
WHERE course_id in (SELECT course_id
FROM subject
WHERE course_name = 'Oracle')

2. Sub Query using FROM Clause


SELECT a.course_name, b.Avg_Age
FROM subject a, (SELECT course_id, Avg(Age) as Avg_Age
FROM student GROUP BY course_id) b
WHERE b.course_id = a.course_id

3. Sub Query using SELECT Clause


SELECT course_id, course_name,
(
SELECT count (course_id) as num_of_student
FROM student a
WHERE a.course_id = b.course_id
1. Single Row Sub Query
In a Single Row Sub Query the queries return a single/one row of
results to the parent/main Query. It can include any of the
following operators:

• = Equals to
• > Greater than
• < Less than
• >= Greater than Equals to
• <= Less than Equals to
• <> Not Equals to
SELECT * FROM employees
WHERE salary = (SELECT MIN(salary) FROM employees);

Note : Single Row Sub Query using HAVING Clause


SELECT department_id, MIN(salary)
FROM employees
GROUP BY department_id
HAVING MIN(salary) > ( SELECT MIN(salary)
FROM employees
WHERE department_id = 50);
2. Multiple Row Sub Query
A Multiple Row Sub Query returns a result of multiple rows to
the outer/main/parent query. It includes the following
operators:

• IN
• ANY
• ALL or EXISTS

Example :
SELECT e.first_name, e.salary
FROM employees e
WHERE salary IN ( SELECT MIN(e.salary)
FROM employees e
GROUP BY e.department_id);
3. Multiple Column Sub Query
Multiple Column Sub Queries are queries that return multiple
columns to the outer SQL query. It uses the IN operator for the
WHERE and HAVING clause.
SELECT e.department_id, e.job_id,e.salary
FROM employees e
WHERE (e.job_id, e.salary) IN ( SELECT e.job_id, e.salary
FROM employees e
WHERE e.department_id = 50) ;

Note: We can use a Sub Query using a FROM


clause in the main query.

SELECT e.first_name, e.salary, e.department_id, b.salary


_avg
FROM employees e,
(SELECT e1.department_id, AVg(e1.salary) salary_avg
FROM employees e1
GROUP BY e1.department_id) b
WHERE e.department_id = b.department_id AND e.salar
y > b.salary_avg;
4. Nested Sub Query
When we write a Sub Query in a WHERE and HAVING clause
of another Sub Query then it is called a nested Sub Query.

SELECT e.first_name,e.salary
FROM employees e
WHERE e.manager_id in
( SELECT e.manager_id
FROM employees e
WHERE department_id in (select d.department_id
FROM departments d
WHERE d.department_name='Purchasing' ));
5. Correlated Sub Query
A Correlated Sub Query contains a reference to a table that
appears in the outer query. It is used for row by row
processing, in other words the Sub Query will execute row
by row for the parent query.

SELECT a.first_name||' '||a.last_name, a.department_id,


(SELECT b.first_name||' '||b.last_name
FROM employees b
WHERE b.employee_id in
(SELECT d.manager_id
FROM departments d
WHERE d.department_name='IT' ) ) as MANAGER
FROM employees a ;
Follow me to
Get more
Information
And content
Like this.

Manni Chopra
Follow me on Linkedin

You might also like