
- •Laboratory work 6
- •Subquery in where clause
- •Subquery in simple comparison condition
- •Subquery in group comparison condition
- •Subquery in membership condition
- •Subquery in exists condition
- •Correlated subqueries
- •Correlated subqueries in where clause
- •Simple and correlated subqueries in having clause
- •Simple subqueries in from clause
- •Варианты заданий
- •Вариант 1
- •Вариант 2
- •Вариант 3
- •Вариант 4
- •Вариант 5
- •Вариант 6
- •Вариант 7
- •Вариант 8
- •Вариант 9
- •Вариант 10
- •Вариант 11
- •Вариант 12
- •Вариант 13
- •Вариант 14
- •Вариант 15
- •Вариант 16
- •Вариант 17
- •Вариант 18
- •Control questions
- •Appendix a. Answer to the lab task
- •Simple embedded queries
- •Aggregate functions in subqueries
- •Subqueries in having clause
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');
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.
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.
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);
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);
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