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

Lab 07

Uploaded by

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

Lab 07

Uploaded by

Ali Mohamed
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Using Subqueries to Solve Queries

Lesson Agenda

• Subquery: Types, syntax, and guidelines


• Single-row subqueries:
– Group functions in a subquery
– HAVING clause with subqueries
• Multiple-row subqueries
– Use ALL or ANY operator.
• Null values in a subquery

7-2
Using a Subquery to Solve a Problem

Who has a salary greater than Abel’s salary?

Main query:

Which employees have salaries greater than Abel’s


salary?

Subquery:

What is Abel’s salary?

7-3
Subquery Syntax

SELECT select_list
FROM table
WHERE expr
operator (SELECT select_list
FROM table);

• The subquery (inner query) executes before the main


query (outer query).
• The result of the subquery is used by the main query.

7-4
Using a Subquery

SELECT last_name, salary


FROM employees
WHERE salary > 11000
(SELECT
FROM
salary employees
WHERE last_name = 'Abel');

7-5
Guidelines for Using Subqueries

• Enclose subqueries in parentheses.


• Place subqueries on the right side of the comparison
condition for readability (However, the subquery can
appear on either side of the comparison operator.).
• Use single-row operators with single-row subqueries and
multiple-row operators with multiple-row subqueries.

7-6
Types of Subqueries

• Single-row subquery
Main query
returns
Subquery ST_CLERK

• Multiple-row subquery

Main query
returns ST_CLERK
Subquery
SA_MAN

7-7
Lesson Agenda

• Subquery: Types, syntax, and guidelines


• Single-row subqueries:
– Group functions in a subquery
– HAVING clause with subqueries
• Multiple-row subqueries
– Use ALL or ANY operator
• Null values in a subquery

7-8
Single-Row Subqueries

• Return only one row


• Use single-row comparison operators

Operator Meaning
= Equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
<> Not equal to

7-9
Executing Single-Row Subqueries

SELECT last_name, job_id, salary


FROM employees
WHERE job_id = SA_REP
(
SFROM employees
EWHERE last_name = ‘Taylor’)
AND salary > L 8600
E
(SELECT salary
CFROM employees
TWHERE last_name = ‘Taylor’);

j
o
b
_
i
d
7 - 10
Using Group Functions in a Subquery

SELECT last_name, job_id, salary


FROM
employees WHERE 2500
salary = (SELECT MIN(salary)
FROM employees);

7 - 11
The HAVING Clause with
Subqueries
• The Oracle server executes the subqueries first.
• The Oracle server returns results into the HAVING
clause of the main query.
SELECT department_id, MIN(salary)
FROM employees
GROUP BY department_id 2500
HAVING MIN(salary) >
(SELECT MIN(salary)
FROM employees
WHERE department_id = 50);

7 - 12
What Is Wrong with This Statement?

SELECT employee_id, last_name


FROM employees
WHERE salary =
(SELECT MIN(salary)
FROM employees
GROUP BY department_id);

Single-row operator
with multiple-row
subquery

7 - 13
No Rows Returned by the Inner Query

SELECT last_name, job_id


FROM employees
WHERE job_id =
(SELECT
job_id
FROM employees
WHERE last_name = 'Haas');

Subquery returns no rows because there is


no employee named “Haas.”

7 - 14
Lesson Agenda

• Subquery: Types, syntax, and guidelines


• Single-row subqueries:
– Group functions in a subquery
– HAVING clause with subqueries
• Multiple-row subqueries
– Use ALL or ANY operator
• Null values in a subquery

7 - 15
Multiple-Row Subqueries

• Return more than one row


• Use multiple-row comparison operators
Operator Meaning
IN Equal to any member in the list
ANY
Must be preceded by =, !=, >, <, <=, >=.
Compares a value to each value in a list or
returned by a query. Evaluates to FALSE if the
query returns no rows.
ALL
Must be preceded by =, !=, >, <, <=, >=.
Compares a value to every value in a list or
returned by a query. Evaluates to TRUE if the
query returns no rows.
7 - 16
Using the ANY Operator
in Multiple-Row Subqueries

SELECT employee_id, last_name, job_id, salary


FROM employees 9000, 6000, 4200
WHERE salary < ANY
(SELECT salary
FROM employees
WHERE job_id =
AND 'IT_PROG') job_id <> 'IT_PROG';

7 - 17
Using the ALL Operator
in Multiple-Row Subqueries

SELECT employee_id, last_name, job_id, salary


FROM employees 9000, 6000, 4200
WHERE salary < ALL
(SELECT salary
FROM employees
WHERE job_id =
AND 'IT_PROG') job_id <> 'IT_PROG';

7 - 18
Lesson Agenda

• Subquery: Types, syntax, and guidelines


• Single-row subqueries:
– Group functions in a subquery
– HAVING clause with subqueries
• Multiple-row subqueries
– Use ALL or ANY operator
• Null values in a subquery

7 - 19
Null Values in a Subquery

SELECT emp.last_name
FROM employees emp
WHERE emp.employee_id NOT IN
(S
ELFROM employees mgr);
EC
T
mg
r.
ma
na
ge
r_
id

7 - 20
Quiz

Using a subquery is equivalent to performing two sequential


queries and using the result of the first query as the search
value(s) in the second query.
1. True
2. False

7 - 23
Thank You

You might also like