- •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
Control questions
Please, give answers to the following questions:
Appendix a. Answer to the lab task
Please formulate SQL SELECT statements that correspond to the following queries:
Simple embedded queries
Display departments that are located in the same building as informatics faculty
SELECT Name
FROM DEPARTMENT
WHERE Building = (SELECT Building
FROM FACULTY
WHERE Name = 'Computer Science');
Display faculties that have fund less than fund of ASU department:
SELECT Name
FROM FACULTY
WHERE Fund <(SELECT Fund
FROM DEPARTMENT
WHERE Name = 'ASU');
The same table in parent and embedded query:
Display teachers that have the same salary+increment more that have of Ivanov:salary + increment:
SELECT Name
FROM TEACHER
WHERE Salary + Increment + 100 > (SELECT (Salary + Increment) / 2
FROM TEACHER
WHERE Name = 'Иванов');
Correlated subqueries in WHERE clause
Display faculties that have departments in building 5
SELECT Name
FROM FACULTY
WHERE 5 IN (SELECT Building
FROM DEPARTMENT
WHERE FACULTY.#F = DEPARTMENT.#F);
Display teachers that have more than 3 lectures in the 1st week:
SELECT Name
FROM TEACHER
WHERE 3 < (SELECT COUNT(*)
FROM LECTURE
WHERE LECTURE.#T = TEACHER.#T AND Week = 1);
Display buildings that have only one faculty:
SELECT Building
FROM FACULTY F1
WHERE 1 = (SELECT COUNT ( Building)
FROM FACULTY F2
WHERE F1.Building = F2.Building);
Using EXISTS in WHERE clause:
Select teaches that have at least one lecture:
SELECT Name
FROM TEACHER
WHERE EXISTS (SELECT *
FROM LECTURE
WHERE LECTURE.#T = TEACHER.#T);
Select teaches-professors that are not curators of first year groups
SELECT Name
FROM TEACHER
WHERE Post = professor AND
NOT EXISTS (SELECT * FROM GROUP
WHERE GROUP.#Curator = TEACHER.#T AND Course = 1);
Usage ANY, SOME and ALL.
Display teachers of ASU department that have salary less at least one teacher of CS department
SELECT Name
FROM TEACHER T, DEPARTMENT D
WHERE T.#D = D.#D AND D.Name = 'ASU' AND
Salary + Increment < ANY (SELECT Salary + Increment
FROM TEACHER T2, DEPARTMENT D2
WHERE T2.#D = D2.#D AND D.Name = 'CS');
Select departments that have fund less that fund at least one faculty:
SELECT Name
FROM DEPARTMENT
WHERE Fund < ANY (SELECT Fund FROM FACULTY);
SELECT Name
FROM DEPARTMENT
WHERE Fund < (SELECT MAX(Fund) FROM FACULTY);
Display groups that have rating more that all ratings of groups of 5th course of CS department
SELECT Num
FROM GROUP
WHERE Rating >ALL (SELECT Rating
FROM GROUP, DEPARTMENT
WHERE GROUP.#D = DEPARTMENT.#D AND
DEPARTMENT.Name = 'CS' AND GROUP.Course = 5);
SELECT Num
FROM GROUP outer
WHERE NOT EXISTS (SELECT Rating
FROM GROUP inner, DEPARTMENT
WHERE GROUP.#D = DEPARTMENT.#D AND
outer.rating <= inner.rating
DEPARTMENT.Name = 'CS' AND GROUP.Course = 5);
Aggregate functions in subqueries
Display teaches that have salary+increment more that average salary+increment in the university:
SELECT Tch.Name
FROM DEPARTMENT Dep, TEACHER Tch
WHERE Dep.#D = Tch.#T AND Dep.Name = 'ASU' AND
Tch.Salary + Tch.Increment > (SELECT AVG(Salary+Increment) FROM TEAHER);
Dislay faculties that have more than 7 departments:
SELECT Name
FROM FACULTY
WHERE 7 < (SELECT COUNT(*)
FROM DEPARTMENT
WHERE DEPARTMENT.#F = FACULTY.#F);
Display teachers that have more than 10 lectures in the 1st week:
SELECT Name
FROM TEACHER
WHERE 10 < (SELECT COUNT(*)
FROM LECTURE
WHERE LECTURE.#T = TEACHER.#T AND Week = 1);
