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

Syntax:

Description:

A membership condition tests for membership in a subquery.

Examples:

1. Select teachers that have lectures by at least one subject that have lectures the teacher Bill:

SELECT Name

FROM TEACHER T, LECTURE L

WHERE T.TchNo = L.TchNo AND SbjNo IN (SELECT SbjNo

FROM TEACHER TCH, LECTURE LEC

WHERE TCH.TchNo = LEC.TchNo AND TCH.Name = 'Bill');

      1. Subquery in exists condition

Syntax:

Description:

Evaluates to TRUEif a subquery returns at least one row.

Because of EXISTS are usually used in correlated subquery we will consider it more detailed later.

    1. Correlated subqueries

In order to correlate subquery it is necessary that subquery references to the column name of the parent query. 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.

The following examples show the general syntax of a correlated subquery:

SELECT select_list

FROM table1 t_alias1

WHERE expr operator

(SELECT column_list

FROM table2 t_alias2

WHERE t_alias1.column operator t_alias2.column);

UPDATE table1 t_alias1

SET column =

(SELECT expr

FROM table2 t_alias2

WHERE t_alias1.column = t_alias2.column);

DELETE FROM table1 t_alias1

WHERE column operator

(SELECT expr

FROM table2 t_alias2

WHERE t_alias1.column = t_alias2.column);

Now we will discuss correlated subqueries in WHERE clause of SELECT statement.

      1. Correlated subqueries in where clause

Examples:

1. Display teachers that have at least one lecture:

SELECT Name

FROM TEACHER

WHERE EXISTS (SELECT *

FROM LECTURE

WHERE LECTURE.#T = TEACHER.#T);

Here by defining conditionLECTURE.#T = TEACHER.#Twe reference from subquery to outer query

2. Display teachers that have no lectures:

SELECT Name

FROM TEACHER

WHERE NOT EXISTS (SELECT *

FROM LECTURE

WHERE LECTURE.#T = TEACHER.#T);

    1. Simple and correlated subqueries in having clause

You may use simple and correlated subqueries in HAWING clause.

If you use correlated subquery in HAVING clause in subquery you may reference to those elements of parent query that may be used in HAVING clause (usually they are grouped columns).

Examples:

1. List faculties where sum of all funds of all its departments exceeds more than 20000 the fund of the faculty’s department with maximum fund.

SELECT FACULTY.Name

FROM FACULTY F1, DEPARTMENT D1

WHERE F1.#F = D1.#F

GROUP BY F1.Name

HAVING SUM(D1.Fund) > (SELECT 200000 + MAX(Fund)

FROM FACULTY F2, DEPARTMENT D2

WHERE F2.#F = D2.#F AND F1.Name = F2.Name);

    1. Simple subqueries in from clause

FROM clause may contain not only list of table names, but also subqueries. An aliases must be assigned to the subqueries in order to have possibilities to reference to such tables.

There are class of queries that cannot be expressed without subqueries in FROM clause. They are the queries that demand independent calculation of two or more queries and after that common usage of results of such queries.

Example:

Display average fund of all faculties and average salary of all teachers:

SELECT Fac.AvgFund, Tch.AvgSalary

FROM (SELECT AVG(Fund) AS AvgFund FROM FACULTY) Fac,

(SELECT AVG(Salary) AS AvgSalary FROM TEACHER) Tch

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