Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
BD / Labs / English / Lab6-Embaded queries.doc
Скачиваний:
25
Добавлен:
20.02.2016
Размер:
170.5 Кб
Скачать

Laboratory work 6

Embedded queries (subqueries) in SQL Oracle

Contents

1. Purpose of the lab 1

2. Theoretical backgrounds 2

2.1. Subquery in WHERE clause 2

2.1.1. Subquery in simple comparison condition 2

2.1.2. Subquery in group comparison condition 3

2.1.3. Subquery in membership condition 5

2.1.4. Subquery in EXISTS condition 5

2.2. Correlated subqueries 5

2.2.1. Correlated subqueries in WHERE clause 6

2.3. Simple and correlated subqueries in HAVING clause 6

2.4. Simple subqueries in FROM clause 6

3. Варианты заданий 6

3.1. Вариант 1 7

3.2. Вариант 2 7

3.3. Вариант 3 8

3.4. Вариант 4 9

3.5. Вариант 5 9

3.6. Вариант 6 10

3.7. Вариант 7 10

3.8. Вариант 8 11

3.9. Вариант 9 12

3.10. Вариант 10 12

3.11. Вариант 11 13

3.12. Вариант 12 14

3.13. Вариант 13 14

3.14. Вариант 14 15

3.15. Вариант 15 15

3.16. Вариант 16 16

3.17. Вариант 17 17

3.18. Вариант 18 17

4. Control questions 18

5. Appendix A. Answer to the lab task 18

Simple embedded queries 18

Aggregate functions in subqueries 19

Subqueries in HAVING clause 20

  1. Purpose of the lab

  • To study SQL Oracle possibilities to formulate and process subqueries.

  • To acquire practical skills in formulating and processing subqueries.

  1. Theoretical backgrounds

A subquery is a query whose results are passed as the argument for another query. Subqueries enable you to bind several queries together.

A queryis an operation that retrieves data from one or more tables. In this reference, a top-level query is called aSELECTstatement, and a query nested within aSELECTstatement is called asubquery. That is asubquery is a query whose results are passed as the argument for another query. Subqueries enable you to bind several queries together.

Subqueries are used to:

  • define the set of rows to be inserted into the target table of an INSERTorCREATE TABLEstatement;

  • define one or more values to be assigned to existing rows in an UPDATEstatement;

  • provide values for conditions in a WHEREclause,HAVINGclause, orSTARTWITHclause ofSELECT,UPDATE, andDELETEstatements;

  • provide values for a specified column in an INSERT...VALUESlist;

  • define a table to be operated on by a containing query.

You do this by placing the subquery in the FROM clause of the containing query as you would a table name. You may use subqueries in place of tables in this way as well in INSERT, UDPATE, and DELETE statements.

Subqueries so used can employ correlation variables, but only those defined within the subquery itself, not outer references. Outer references ("left-correlated subqueries") are allowed only in the FROM clause of a SELECT statement.

A subquery answers multiple-part questions. For example, to determine who works in John's department, you can first use a subquery to determine the department in which John works. You can then answer the original question with the parent SELECT statement.

A subquery can contain another subquery. Oracle places no limit on the level of query nesting.

If tables in a subquery have the same name as tables in the containing statement, you must prefix any reference to the column of the table from the containing statement with the table name or alias. To make your statements easier for you to read, always qualify the columns in a subquery with the name or alias of the table.

Oracle performs a correlated subquery when the subquery references a column from a table referred to in the parent statement. A correlated subquery is evaluated once for each row processed by the parent statement. The parent statement can be a SELECT, UPDATE, or DELETE statement.

A correlated subquery answers a multiple-part question whose answer depends on the value in each row processed by the parent statement. For example, you can use a correlated subquery to determine which teachers earn more than the average salaries for their departments. In this case, the correlated subquery specifically computes the average salary for each department.

Соседние файлы в папке English