Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
BD / Labs / English / Lab5-Functions-Group-by-Having-Order-by.doc
Скачиваний:
37
Добавлен:
20.02.2016
Размер:
738.3 Кб
Скачать
  1. Control questions

Please, give answers to the following questions:

  1. What type of SQL functions do you know?

  2. What is the scope of aggregate functions if SGROUP BY clause is absent and present?

  3. In what clauses of SELECT statement aggregate functions may be used?

  4. What does DISTINCT and ALL keywords in aggregate functions mean?

  5. How do aggregate functions operate with null values?

  6. How does SQL Oracle operates with date values?

  7. What expressions may select list contain if SGROUP BY clause is present in SELECT statement?

  8. What is a purpose of SGROUP BY and HAVING clauses?

  9. What are restrictions of column expressions in ORDER BY clause if SGROUP BY clause is used?

  10. How does Oracle order null values?

  1. Appendices

    1. Appendix a. Answer to lab task

Express in SQL the following queries:

  1. 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';

  1. 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';

  1. 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;

  1. 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;

  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;

  1. 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;

  1. 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';

  1. 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

  1. 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

  1. 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;

32

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