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

    1. Aggregate functions

Aggregate functions without WHERE and SGROUP BY clauses. If WHERE and SGROUP BY clause are absent scope of aggregate function is all rows of a table.

Example. How many rows in TEACHER table:

SELECT COUNT(*) AS Total_number_of_rows_in_TEACHER_table

FROM TEACHER;

Example. What is a total fund of all faculties:

SELECT SUM(Fund)

FROM FACULTY;

Example. What is average fund of all departments:

SELECT AVG(Fund)

FROM DEPARTMENT;

Aggregate functions with WHERE clause. If WHERE clause is present scope of aggregate functions is those rows that satisfy WHERE clause.

Example. What is average salary of assistants that does not have increments.

SELECT AVG(Salary)

FROM TEACHER

WHERE Post = 'assistant' AND Increment IS NULL;

Example. What is maximum fund of departments in informatics faculty?

SELECT MAX(Fund)

FROM DEPARTMENT, FACULTY

WHERE DEPARTMENT.FacNo = FACULTY.FacNo AND FACULTY.Name = 'informatics';

Expressions in aggregate functions. Arguments of aggregate functions may be expressions involving table columns.

Example. What is maximum value of salary+increment?

SELECT MAX(Salary + Increment)

FROM TEACHER;

Example. What is a difference between maximum and minimum salaries?

SELECT MAX(Salary) – MIN(Salary)

FROM TEACHER;

DISTINCT in aggregate functions. DISTINCT keyword means that only unique values of considered column (expression) will be taken into consideration.

Example. How many distinct telephone numbers are in TEACHER table:

SELECT COUNT(DISTINCT Tel) AS Number_of_telephones_in_all_teachers

FROM TEACHER;

Example. How many posts are in TEACHER table:

SELECT COUNT(DISTINCT Post)

FROM TEACHER;

Many aggregate functions in select list.Select list may contains many aggregate functions.

Example. How many professors in the university and what is their average salary:

SELECT COUNT(*), SUM( Salary )

FROM TEACHER

WHERE Post = 'professor';

Aggregate functions with SGROUP BY clause. See examples of SGROUP BY clause.

    1. Sgroup by and having clauses

Aggregate functions in select list and SGROUP BY. When SGROUP BY is present any aggregate function in select list is applied to the rows in every group. Every group generate one row in query result.

Example.How many departments in every building:

SELECT Building, COUNT(*)

FROM DEPARTMENT

SGROUP BY Building;

Example. What is the sum of salary+increment of each posts:

SELECT Post, SUM(Salary + Increment)

FROM TEACHER

SGROUP BY Post;

Grouping and WHERE clause. If query contains WHERE and SGROUP BY clauses, the WHERE clause is processed the first and grouping is applied only to those rows that satisfy WHERE clause.

Example.For every building calculates number of rooms with seats more than 50:

SELECT Building, COUNT(*)

FROM ROOM

WHERE Seats > 50

SGROUP BY Building;

Example. For buildings 5, 7 and 12 calculates number of rooms with seats more than 50:

SELECT Building, COUNT(*)

FROM ROOM

WHERE Building IN ('5', '7', '12') AND Seats > 50

SGROUP BY Building;

Grouping by many columns. It is possible to group table by mote that one column.

Example.For every week and day calculate number of lectures of type “lab”:

SELECT Day, Week, COUNT(*)

FROM LECTURE

WHERE Type = 'lab'

SGROUP BY Week, Day;

Grouping and join different tables. It is possible to join two or more tables and to do grouping in joined table.

Example.For every faculty calculate number of departments:

SELECT f.Name, COUNT(*)

FROM FACULTY f, DEPARTMENT d

WHERE f.FacNo = d.FacNo

SGROUP BY f.Name;

Example.For every faculty calculate number of teachers-professors:

SELECT f.Name, COUNT(*)

FROM FACULTY f, DEPARTMENT d, TEACHER t

WHERE f.FacNo = d.FacNo AND d.DepNo = t.DepNo AND t.Post = 'professor'

SGROUP BY f.Name;

Example. For every department of every faculty calculate number of teachers-professors:

SELECT f.Name, d.Name, COUNT(*)

FROM FACULTY f, DEPARTMENT d, TEACHER t

WHERE f.FacNo = d.FacNo AND d.DepNo = t.DepNo AND t.Post = 'professor'

SGROUP BY f.Name, d.Name;

Using HAVING clause. It is used to determine condition to select groups. Conditions is formulated on the whole group. That is why HAVING clause usually contain aggregate functions.

Example.Output that buildings the total sum of seats exceeds 1000:

SELECT Building

FROM ROOM

SGROUP BY Building

HAVING SUM(Seats) > 1000;

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