0% found this document useful (0 votes)
6 views3 pages

Chap 11

A Sub-Query consists of two queries: an INNER query that executes first and an OUTER query that uses the results of the INNER query. Sub-Queries are useful for retrieving complex data, such as the name of the highest-paid employee, which cannot be achieved with a regular query alone. The document also explains the syntax for Sub-Queries and provides examples of their usage in SQL.

Uploaded by

[smo] Monkeyy-3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Chap 11

A Sub-Query consists of two queries: an INNER query that executes first and an OUTER query that uses the results of the INNER query. Sub-Queries are useful for retrieving complex data, such as the name of the highest-paid employee, which cannot be achieved with a regular query alone. The document also explains the syntax for Sub-Queries and provides examples of their usage in SQL.

Uploaded by

[smo] Monkeyy-3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Chap 11 – What are Sub-Queries?

 What is a Sub-Query?
A Sub Query is very similar to a regular query except that there are actually 2
queries in one Sub-Query statement. There is an INNER query and an OUTER
query. The INNER query always executes first to completion and is surrounded
by parenthesis, then the OUTER query executes last using the data retrieved by
the INNER Query.

 Why use Sub-Queries?


Subqueries are used when needed because a regular query just can’t give us the
answer we are looking for.

For Example: Let’s say we want the name of the highest paid employee from the
Employees table. With a regular query we can only get the salary from the
Employees table but we cannot get the name of the person paid that salary.
With a Sub-Query you can get the name of the person.

 SUB-QUERY Syntax

SELECT columns \
FROM table[s] > outer query
WHERE condition[s] = /
(SELECT function(column), column \
FROM table[s] > inner qry
WHERE condition[s] ) /
EXAMPLE:

SELECT firstName, lastName, salary


From Employees
WHERE salary = (SELECT MAX(salary) from Employees)
[the inner query will execute first giving us the highest salary in
the Employees table. That value is then used in the WHERE
clause of the OUTER query….WHERE salary = max sal. The
OUTER query executes second and gives us the firstName,
lastName and salary of the person whose salary = highest paid
salary.]

Problem: Who makes the least amount of money?


SELECT firstName, lastName, salary
From Employees
WHERE salary = (SELECT MIN(salary) from Employees)

Problem: Who makes less than the average salary paid to all
employees.
SELECT firstName, lastName, salary
From Employees
WHERE salary < (SELECT AVG(salary) from Employees)

 = or IN
If the INNER query returns only 1 value, then we use an = in the OUTER query in
the WHERE clause. If the INNER query returns more than 1 values, say 2 or 3
values, then we must use an IN in the OUTER query to test all answers returned
from the INNER query.
EXAMPLE:

Problem: Who makes the least amount of $ in each department?


SELECT firstName, lastName, salary, dept
From Employees
WHERE salary IN (SELECT min(salary)
from Employees
Group By Dept)
[The INNER query will execute first giving us a list of the lowest
salary in each dept. So the INNER query gives us back a list of
numbers. So the OUTER query must use an IN statement to
test for all these values.]

 Can you have 2 Inner Queries and only 1 Outer Query?


Sure.

Problem: Who makes the least amount of money in the company


and who makes the most?
SELECT firstName, lastName, salary
From Employees
WHERE salary = (SELECT min(salary)
from Employees)
OR salary = (SELECT max(salary)
From Employees);
[The 2 INNER queries will execute first giving us one values for
each of the WHERE clauses in the OUTER query.]

 Summary

 Other Resources

Click on the link below to get more help with “SQL SUB-QUERIES”.

https://www.w3resource.com/sql/subqueries/understanding-sql-subqueries.php

http://beginner-sql-tutorial.com/sql-subquery.htm

You might also like