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

      1. Subquery in simple comparison condition

Syntax:

Description:

The following rules are applied to the simple comparison condition with subquery in WHERE clause:

  • Subquery should returns single row.

  • If left hand side is expr, than subquery must return single row with the only value that type compatible withexpr.

  • If left hand side is expr_list, than subquery must return single row with the list of values that corresponds in number and type withexpr_list. In this case comparison operator returns TRUE if every value inexpr_listis equal (in case =) or not equal (in case !=, ^=, <>) to the every value returned by query.

Examples:

1. Select departments that are located in the same building as informatics faculty:

SELECT Name

FROM DEPARTMENT

WHERE Building = (SELECT Building

FROM FACULTY

WHERE Name = 'informatics');

2. Select faculties that have funds less than fund of CAD department

SELECT Name

FROM FACULTY

WHERE Fund <(SELECT Fund

FROM DEPARTMENT

WHERE Name = 'CAD');

3. Select teachers that salary + commission is more than 100 of the half of salary + commission if Bill:

SELECT Name

FROM TEACHER

WHERE Salary + Commission + 100 > (SELECT (Salary + Commission) / 2

FROM TEACHER

WHERE Name = 'Bill');

4. Select teachers that have the same department No and post as Bill:

SELECT Name

FROM TEACHER

WHERE (DepNo, Post)= (SELECT (DepNo, Post

FROM TEACHER

WHERE Name = 'Bill');

      1. Subquery in group comparison condition

Syntax:

Description:

The following rules are applied to the group comparison condition with subquery in WHERE clause:

  • Subquery may return zero or more rows.

  • If left hand side is expr, than subquery must return rows with the only value that type compatible withexpr.

  • If left hand side is expr_list, than subquery must return rows with the list of values that corresponds in number and type withexpr_list.

ANY and SOME are equivalent and compares a value to each value in a list of rowsreturned by a query. Query may return zero or more rows. Evaluates toTRUE if at least one row returned by query is in relation (corresponding comparison operator) with value (list of values) defined by the first operand, otherwise it evaluates FALSE. If query does not returns any row it evaluates to FALSE

ALL compares a value (list of values)to every value (list of values)in a list of rowsreturned by a query. It Evaluates toTRUE if ALL rows returned by query is in relation (corresponding comparison operator) with value (list of values) defined by the first operand, otherwise it evaluates FALSE. If query does not returns any row it evaluates to TRUE

Examples:

1. Display departments with fund that is more that fund of at least one faculty:

SELECT Name

FROM DEPARTMENT

WHERE Fund > ANY (SELECT Fund FROM FACULTY);

ANY and aggregate functions. Pay attention, that operator <ANY is equivalent the following statement: “the left value is less than maximum of right values”, and operator >ANY is equivalent the following statement: “the left value is more than minimum of right values”. That is why these ANY operators may be expressed with the help of MAX and MIN functions in subquery.

2. Display departments with fund that is more that fund of at least one faculty:

SELECT Name

FROM DEPARTMENT

WHERE Fund > ANY (SELECT Fund FROM FACULTY);

SELECT Name

FROM DEPARTMENT

WHERE Fund > (SELECT MIN(Fund) FROM FACULTY);

3. Display groups that have ratings more than ratings of all groups of the fifth course of “DBMS” department:

SELECT Num

FROM GROUP

WHERE Rating >ALL (SELECT Rating

FROM GROUP, DEPARTMENT

WHERE GROUP.DepNo = DEPARTMENT.DepNo AND

DEPARTMENT.Name = 'DBMS' AND GROUP.Course = 5);

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