Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
all labs.pdf
Скачиваний:
74
Добавлен:
25.02.2016
Размер:
2.04 Mб
Скачать

Using a Subquery

SELECT last_name, salary FROM employees

WHERE salary > 11000 (SELECT salary FROM employees

WHERE last_name = 'Abel');

Using a Subquery

Copyright © 2009, Oracle. All rights reserved.

Academy

 

 

 

 

 

 

Oracle

 

In the slide, the inner query determines the salary of employee Ab l. The outer query takes the result

 

of the inner query and uses this result to display all the employees who earn more than employee

 

Abel.

 

 

Only

 

 

 

&

 

 

Internal

 

 

 

Oracle

Use

 

 

 

 

 

Oracle Database 11g: SQL Fundamentals I 7 - 6

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.

Guidelines for Using Subqueries

 

Academy

 

 

Copyright © 2009, Oracle. All rights reserved.

 

 

A subquery must be enclosed in parentheses.

 

 

 

Place the subquery on the right side of the comparison

ondition for readability. However, the

subquery can appear on either side of the compa ison operator.

• Two classes of comparison conditions are used in subqueries: single-row operators and

multiple-row operators.

 

Only

Oracle

 

&

 

Internal

 

Oracle

Use

 

 

Oracle Database 11g: SQL Fundamentals I 7 - 7

Types of Subqueries

Single-row subquery

Main query

 

 

 

 

 

returns

 

Subquery

 

 

 

 

ST_CLERK

 

 

 

 

 

 

 

 

 

 

 

 

 

Multiple-row subquery

Main query

 

 

 

 

 

returns

ST_CLERK

 

Subquery

 

 

 

 

SA_MAN

 

 

 

 

 

 

 

 

 

Types of Subqueries

Copyright © 2009, Oracle. All rights reserved.

Academy

 

 

 

 

 

 

Oracle

 

Single-row subqueries: Queries that return only one row from the inner SELECT statement

 

Multiple-row subqueries: Queries that return more than one row from the inner SELECT

 

statement

 

 

 

 

 

Note: There are also multiple-column subqueries, which are queries that return more than one

 

 

 

 

Only

 

 

column from the inner SELECT statement. These are covered in the Oracle Database 11g: SQL

 

Fundamentals II course.

&

 

 

 

Internal

 

 

 

Oracle

Use

 

 

 

 

 

Oracle Database 11g: SQL Fundamentals I 7 - 8

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

 

Copyright © 2009, Oracle. All rights reserved.

Academy

 

 

 

 

Oracle

&

Only

 

 

Internal

 

 

 

 

Use

 

Oracle

 

 

 

Oracle Database 11g: SQL Fundamentals I 7 - 9

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]