+ All Categories
Home > Documents > Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title...

Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title...

Date post: 31-Mar-2018
Category:
Upload: lehanh
View: 217 times
Download: 4 times
Share this document with a friend
35
Copyright © 2004, Oracle. All rights reserved. Using Subqueries to Solve Queries
Transcript
Page 1: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

Copyright © 2004, Oracle. All rights reserved.

Using Subqueries to Solve Queries

Page 2: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-2 Copyright © 2004, Oracle. All rights reserved.

Objectives -- 1

After completing this lesson, you should be able to do

the following:

• Define subqueries

• Describe the types of problems that subqueries

can solve

• List the types of subqueries

• Write single-row and multiple-row subqueries

Page 3: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-3 Copyright © 2004, Oracle. All rights reserved.

Using a Subquery

to Solve a Problem

Who has a salary greater than Abel’s?

Which employees have salaries greater

than Abel’s salary?

Main query:

What is Abel’s salary?

Subquery:

Page 4: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-4 Copyright © 2004, Oracle. All rights reserved.

• The subquery (inner query) executes once before

the main query (outer query).

• The result of the subquery is used by the main

query.

SELECT select_list

FROM table

WHERE expr operator

(SELECT select_list

FROM table);

Subquery Syntax

Page 5: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-5 Copyright © 2004, Oracle. All rights reserved.

SELECT last_nameFROM employeesWHERE salary >

(SELECT salaryFROM employeesWHERE last_name = 'Abel');

Using a Subquery

11000

Page 6: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-6 Copyright © 2004, Oracle. All rights reserved.

Guidelines for Using Subqueries

• Enclose subqueries in parentheses.

• Place subqueries on the right side of the

comparison condition.

• The ORDER BY clause in the subquery is not

needed unless you are performing Top-N analysis.

• Use single-row operators with single-row

subqueries, and use multiple-row operators with

multiple-row subqueries.

Page 7: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-7 Copyright © 2004, Oracle. All rights reserved.

Types of Subqueries

• Single-row subquery

• Multiple-row subquery

Main query

Subqueryreturns

ST_CLERK

ST_CLERK

SA_MAN

Main query

Subqueryreturns

Page 8: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-8 Copyright © 2004, Oracle. All rights reserved.

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

Page 9: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-9 Copyright © 2004, Oracle. All rights reserved.

SELECT last_name, job_id, salaryFROM employeesWHERE job_id =

(SELECT job_idFROM employeesWHERE employee_id = 141)

AND salary >(SELECT salaryFROM employeesWHERE employee_id = 143);

Executing Single-Row Subqueries

ST_CLERK

2600

Page 10: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-10 Copyright © 2004, Oracle. All rights reserved.

SELECT last_name, job_id, salaryFROM employeesWHERE salary =

(SELECT MIN(salary)FROM employees);

Using Group Functions in a Subquery

2500

Page 11: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-11 Copyright © 2004, Oracle. All rights reserved.

SELECT department_id, MIN(salary)FROM employeesGROUP BY department_idHAVING MIN(salary) >

(SELECT MIN(salary)FROM employeesWHERE department_id = 50);

The HAVING Clause with Subqueries

• The Oracle server executes subqueries first.

• The Oracle server returns results into the HAVING

clause of the main query.

2500

Page 12: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-12 Copyright © 2004, Oracle. All rights reserved.

Multiple-Row Subqueries

• Return more than one row

• Use multiple-row comparison operators

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

Page 13: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-13 Copyright © 2004, Oracle. All rights reserved.

SELECT employee_id, last_name, job_id, salaryFROM employeesWHERE salary < ANY

(SELECT salaryFROM employeesWHERE job_id = 'IT_PROG')

AND job_id <> 'IT_PROG';

Using the ANY Operator

in Multiple-Row Subqueries

9000, 6000, 4200

<

Page 14: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-14 Copyright © 2004, Oracle. All rights reserved.

SELECT employee_id, last_name, job_id, salaryFROM employeesWHERE salary < ALL

(SELECT salaryFROM employeesWHERE job_id = 'IT_PROG')

AND job_id <> 'IT_PROG';

Using the ALL Operator

in Multiple-Row Subqueries

9000, 6000, 4200

Page 15: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-15 Copyright © 2004, Oracle. All rights reserved.

SELECT emp.last_nameFROM employees empWHERE emp.employee_id NOT IN

(SELECT mgr.manager_idFROM employees mgr);

no rows selected

Null Values in a Subquery

Page 16: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-16 Copyright © 2004, Oracle. All rights reserved.

SELECT select_list

FROM table

WHERE expr operator

(SELECT select_listFROM table);

Summary --1

In this lesson, you should have learned how to:

• Identify when a subquery can help solve a

question

• Write subqueries when a query is based on

unknown values

Page 17: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-17 Copyright © 2004, Oracle. All rights reserved.

Objectives --2

After completing this lesson, you should be able to do

the following:

• Write a multiple-column subquery

• Use scalar subqueries in SQL

• Solve problems with correlated subqueries

• Use the EXISTS and NOT EXISTS operators

• Use the WITH clause

Page 18: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-18 Copyright © 2004, Oracle. All rights reserved.

Multiple-Column Subqueries

Main query

WHERE (MANAGER_ID, DEPARTMENT_ID) IN

Subquery

100 90

102 60

124 50

Each row of the main query is compared to values from a multiple-row and multiple-column subquery.

Page 19: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-19 Copyright © 2004, Oracle. All rights reserved.

Column Comparisons

Column comparisons in a multiple-column subquery

can be:

• Pairwise comparisons

• Nonpairwise comparisons

Page 20: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-20 Copyright © 2004, Oracle. All rights reserved.

Pairwise Comparison Subquery

Display the details of the employees who are managed

by the same manager and work in the same department as the employees with EMPLOYEE_ID 199

or 174.

SELECT employee_id, manager_id, department_idFROM employeesWHERE (manager_id, department_id) IN

(SELECT manager_id, department_idFROM employeesWHERE employee_id IN (199,174))

AND employee_id NOT IN (199,174);

Page 21: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-21 Copyright © 2004, Oracle. All rights reserved.

Nonpairwise Comparison Subquery

Display the details of the employees who are managed

by the same manager as the employees with EMPLOYEE_ID 174 or 199 and work in the same

department as the employees with EMPLOYEE_ID 174

or 199.

SELECT employee_id, manager_id, department_idFROM employeesWHERE manager_id IN

(SELECT manager_idFROM employeesWHERE employee_id IN (174,199))

AND department_id IN (SELECT department_idFROM employeesWHERE employee_id IN (174,199))

AND employee_id NOT IN(174,199);

Page 22: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-22 Copyright © 2004, Oracle. All rights reserved.

Scalar Subquery Expressions

• A scalar subquery expression is a subquery that

returns exactly one column value from one row.

• Scalar subqueries can be used in:

– Condition and expression part of DECODE and CASE

– All clauses of SELECT except GROUP BY

Page 23: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-23 Copyright © 2004, Oracle. All rights reserved.

Scalar Subqueries: Examples

• Scalar subqueries in CASE expressions

SELECT employee_id, last_name,

(CASE

WHEN department_id =

(SELECT department_id

FROM departments

WHERE location_id = 1800)

THEN 'Canada' ELSE 'USA' END) location

FROM employees;

SELECT employee_id, last_name

FROM employees e

ORDER BY (SELECT department_name

FROM departments d

WHERE e.department_id = d.department_id);

20

• Scalar subqueries in ORDER BY clause

Page 24: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-24 Copyright © 2004, Oracle. All rights reserved.

Correlated Subqueries

Correlated subqueries are used for row-by-row

processing. Each subquery is executed once for every

row of the outer query.

GET

candidate row from outer query

EXECUTE

inner query using candidate row value

USE

values from inner query to qualify or

disqualify candidate row

Page 25: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-25 Copyright © 2004, Oracle. All rights reserved.

Correlated Subqueries

The subquery references a column from a table in the

parent query.

SELECT column1, column2, ...FROM table1WHERE column1 operator

(SELECT column1, column2FROM table2WHERE expr1 =

.expr2);

outer

outer

Page 26: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-26 Copyright © 2004, Oracle. All rights reserved.

SELECT last_name, salary, department_idFROM employees outerWHERE salary >

(SELECT AVG(salary)FROM employeesWHERE department_id = outer.department_id);

Using Correlated Subqueries

Find all employees who earn more than the average

salary in their department.

Each time a row from

the outer query

is processed, the

inner query is

evaluated.

Page 27: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-27 Copyright © 2004, Oracle. All rights reserved.

Using Correlated Subqueries

Display details of those employees who have changedjobs at least twice.

SELECT e.employee_id, last_name,e.job_id

FROM employees e

WHERE 2 <= (SELECT COUNT(*)

FROM job_history

WHERE employee_id = e.employee_id);

Page 28: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-28 Copyright © 2004, Oracle. All rights reserved.

Using the EXISTS Operator

• The EXISTS operator tests for existence of rows in

the results set of the subquery.

• If a subquery row value is found:

– The search does not continue in the inner query

– The condition is flagged TRUE

• If a subquery row value is not found:

– The condition is flagged FALSE

– The search continues in the inner query

Page 29: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-29 Copyright © 2004, Oracle. All rights reserved.

SELECT employee_id, last_name, job_id, department_idFROM employees outerWHERE EXISTS ( SELECT 'X'

FROM employeesWHERE manager_id =

outer.employee_id);

Find Employees Who Have at Least One

Person Reporting to Them

Page 30: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-30 Copyright © 2004, Oracle. All rights reserved.

SELECT department_id, department_nameFROM departments dWHERE NOT EXISTS (SELECT 'X'

FROM employeesWHERE department_id = d.department_id);

Find All Departments That Do Not Have

Any Employees

<

Page 31: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-31 Copyright © 2004, Oracle. All rights reserved.

The WITH Clause

• Using the WITH clause, you can use the same

query block in a SELECT statement when it occurs

more than once within a complex query.

• The WITH clause retrieves the results of a query

block and stores it in the user’s temporary

tablespace.

• The WITH clause improves performance.

Page 32: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-32 Copyright © 2004, Oracle. All rights reserved.

WITH Clause: Example

Using the WITH clause, write a query to display the

department name and total salaries for those

departments whose total salary is greater than the

average salary across departments.

Page 33: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-33 Copyright © 2004, Oracle. All rights reserved.

WITH Clause: Example

WITH dept_costs AS (

SELECT d.department_name, SUM(e.salary) AS dept_totalFROM employees e JOIN departments dON e.department_id = d.department_idGROUP BY d.department_name),

avg_cost AS (SELECT SUM(dept_total)/COUNT(*) AS dept_avgFROM dept_costs)

SELECT * FROM dept_costs WHERE dept_total >

(SELECT dept_avg FROM avg_cost)

ORDER BY department_name;

Page 34: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-34 Copyright © 2004, Oracle. All rights reserved.

Summary

In this lesson, you should have learned the following:

• A multiple-column subquery returns more than

one column.

• Multiple-column comparisons can be pairwise or

nonpairwise.

• A multiple-column subquery can also be used in the FROM clause of a SELECT statement.

Page 35: Using Subqueries to Solve Queries - ELTE IKpeople.inf.elte.hu/sila/DB1Pract/SQL1_Les06.pdf · Title (Microsoft PowerPoint - SQL1_Les06 [Kompatibilis m\363d]) Author: Sila Created

6-35 Copyright © 2004, Oracle. All rights reserved.

Summary

• Correlated subqueries are useful whenever a

subquery must return a different result for each

candidate row.

• The EXISTS operator is a Boolean operator that

tests the presence of a value.

• You can use the WITH clause to use the same

query block in a SELECT statement when it occurs

more than once.


Recommended