- •Laboratory work 5
- •Aggregate functions
- •Single row functions
- •Number functions
- •Character functions
- •Date functions
- •Conversion Functions
- •Miscellaneous Single Row Functions
- •Sgroup by and having clause
- •Syntax:
- •Purpose
- •Order by clause
- •Syntax:
- •Purpose
- •Description and examples
- •Aggregate functions
- •Sgroup by and having clauses
- •Order by clause
- •Варианты заданий
- •Вариант 1
- •Вариант 2
- •Вариант 3
- •Вариант 4
- •Вариант 5
- •Вариант 6
- •Вариант 7
- •Вариант 8
- •Вариант 9
- •Вариант 10
- •Вариант 11
- •Вариант 12
- •Вариант 13
- •Вариант 14
- •Вариант 15
- •Вариант 16
- •Вариант 17
- •Вариант 18
- •Control questions
- •Appendices
- •Appendix a. Answer to lab task
Control questions
Please, give answers to the following questions:
What type of SQL functions do you know?
What is the scope of aggregate functions if SGROUP BY clause is absent and present?
In what clauses of SELECT statement aggregate functions may be used?
What does DISTINCT and ALL keywords in aggregate functions mean?
How do aggregate functions operate with null values?
How does SQL Oracle operates with date values?
What expressions may select list contain if SGROUP BY clause is present in SELECT statement?
What is a purpose of SGROUP BY and HAVING clauses?
What are restrictions of column expressions in ORDER BY clause if SGROUP BY clause is used?
How does Oracle order null values?
Appendices
Appendix a. Answer to lab task
Express in SQL the following queries:
How many students in informatics faculty?
SELECT SUM(SGROUP.Quantity) AS Number_Of_Students_In_Computer_Science_Faculty
FROM FACULTY, DEPARTMENT, SGROUP
WHERE FACULTY.FacNo = DEPARTMENT.FacNo AND
DEPARTMENT.DepNo = SGROUP.DepNo AND
FACULTY.Name = 'informatics';
How many subjects are taught in informatics faculty?
SELECT COUNT(DISTINCT SbjNo)
FROM FACULTY f, DEPARTMENT d, TEACHER t, LECTURE l, SUBJECT s
WHERE f.FacNo = d.FacNo AND
d.DepNo = t.DepNo AND
l.TchNo = t.TchNo AND
l.SbjNo = s.SbjNo AND
FACULTY.Name = 'informatics';
For every faculty outputs its name and difference between faculty fund and sum of funds of all faculty departments.
SELECT FACULTY.Name, FACULTY.Fund – SUM(DEPARTMENT.Fund)
FROM FACULTY, DEPARTMENT
WHERE FACULTY.FacNo = DEPARTMENT.FacNo
SGROUP BY FACULTY.Name;
For teachers of informatics faculty output their names and number of lectures that they have in week 1.
SELECT t.Name, COUNT(*)
FROM FACULTY f, DEPARTMENT d, TEACHER t, LECTURE l,
WHERE f.FacNo = d.FacNo AND d.DepNo = t.DepNo AND l.TchNo = t.TchNo AND
FACULTY.Name = 'informatics' AND Week = 1;
For teachers of informatics faculty output their names and number of subjects that they are taught.
SELECT t.Name, COUNT(DISTINCT SbjNo)
FROM FACULTY f, DEPARTMENT d, TEACHER t, LECTURE l, SUBJECT s
WHERE f.FacNo = d.FacNo AND
d.DepNo = t.DepNo AND
l.TchNo = t.TchNo AND
l.SbjNo = s.SbjNo AND
FACULTY.Name = 'informatics'
SGROUP BY t.TchNo, t.Name;
For every group of informatics faculty output group course, number and curator name. If curator is absent output literal “no curator”. Query result order by group course in descending order.
SELECT SGROUP.Course, SGROUP.Num, NVL(TEACHER.Nmae, 'no curator')
FROM FACULTY, DEPARTMENT, SGROUP, TEACHER
WHERE FACULTY.FacNo = DEPARTMENT.FacNo AND
DEPARTMENT.DepNo = SGROUP.DepNo AND
SGROUP.Curator = TEACHER.TchNo
FACULTY.Name = 'informatics'
ORDER BY SGROUP.Course DESC;
For every subject that is taught in informatics faculty output the only column that have the following string literal:
Subject subject-name is taught by teacher-name to group group-number in room room-number building building-number of week week-number. Order results by subject name and group number.
SELECT 'Subject ' + s.Name + ' is taught by ' + t.Name +
' to group ' + TO_CHAR(g.Num) + ' in room ' + TO_CHAR(r.Num) +
' building ' + r.Building + ' of week ' + TO_CHAR(l.Week)
FROM FACULTY f, DEPARTMENT d, SGROUP g, TEACHER t, LECTURE l, SUBJECT s ROOM r
WHERE f.FacNo = d.FacNo AND
d.DepNo = g.DepNo AND
g.GRPNo = l.GRPNo AND
l.TchNo = t.TchNo AND
l.SbjNo = s.SbjNo AND
l.RomNo = r.RomNo;
FACULTY.Name = 'informatics';
Output number of students in every department of informatics faculty. (That is query result contains pair of columns: department name, number of students in the department).
SELECT DEPARTMENT.Name,
SUM(SGROUP.Quantity) AS Number_Of_Students_In_The_Departments
FROM FACULTY, DEPARTMENT, SGROUP
WHERE FACULTY.FacNo = DEPARTMENT.FacNo AND
DEPARTMENT.DepNo = SGROUP.DepNo AND
FACULTY.Name = 'informatics'
SGROUP BY DEPARTMENT.Name
Output faculties that fund exceed total funds of their departments more than 2000. Query result should contain the columns: faculty name, faculty fund, total fund of all faculty departments.
SELECT FACULTY.Name, FACULTY.Fund, SUM(DEPARTMENT.Fund)
FROM FACULTY, DEPARTMENT
WHERE FACULTY.FacNo = DEPARTMENT.FacNo AND
SGROUP BY FACULTY.Name
HAVING ( Faculty.Fund - SUM(DEPARTMENT.Fund) ) > 2000
For every teacher output the name and number of taught subjects.
SELECT TEACHER.Name, COUNT(DISTINCT SbjNo ) AS Number_Of_Subjects_Taught_By_The_Teacher
FROM TEACHER, LECTURE
WHERE TEACHER.TchNo = LECTURE.TchNo
SGROUP BY TEACHER.Name;
