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

Laboratory work 5

SQL Oracle functions. Additional clauses of SELECT statement

Contents

1. Purpose of the lab 2

2. Theoretical backgrounds 2

2.1. SQL Oracle functions 2

2.1.1. Aggregate functions 2

2.1.2. Single row functions 3

2.1.2.1. Number functions 3

2.1.2.2. Character functions 4

2.1.2.3. Date functions 6

2.1.2.4. Conversion Functions 7

2.1.2.5. Miscellaneous Single Row Functions 8

2.2. SGROUP BY and HAVING clause 9

2.2.1. Syntax: 9

2.2.2. Purpose 9

2.3. ORDER BY clause 9

2.3.1. Syntax: 9

2.3.2. Purpose 10

3. Description and examples 10

3.1. Aggregate functions 10

3.2. SGROUP BY and HAVING clauses 11

3.3. ORDER BY clause 12

4. Варианты заданий 13

4.1. Вариант 1 13

4.2. Вариант 2 14

4.3. Вариант 3 15

4.4. Вариант 4 15

4.5. Вариант 5 16

4.6. Вариант 6 17

4.7. Вариант 7 18

4.8. Вариант 8 19

4.9. Вариант 9 20

4.10. Вариант 10 21

4.11. Вариант 11 22

4.12. Вариант 12 23

4.13. Вариант 13 24

4.14. Вариант 14 25

4.15. Вариант 15 26

4.16. Вариант 16 27

4.17. Вариант 17 28

4.18. Вариант 18 29

5. Control questions 30

6. Appendices 30

6.1. Appendix A. Answer to lab task 30

  1. Purpose of the lab

  • To study SQL Oracle functions and SGROUPBY,HAVING,ORDERBYclauses ofSELECTstatement.

  • To acquire practical skills in SQL Oracle functions and SGROUPBY,HAVING,ORDERBYclauses ofSELECTstatement by using SQL*Plus.

  1. Theoretical backgrounds

    1. SQL Oracle functions

SQL functions are built into Oracle and are available for use in various appropriate SQL statements. Do not confuse SQL functions with user functions written in PL/SQL.

If you call a SQL function with a null argument, the SQL function automatically returns null. The only SQL functions that do not follow this rule are CONCAT,DECODE,DUMP,NVL, andREPLACE.

There are two categories of SQL functions:

Single-row functions

Single-row functions return a single result row for every row of a queried table. Single-row functions can appear in select lists (if theSELECTstatement does not contain aSGROUPBYclause) andWHEREclauses.

Aggregate functions

Aggregate functions return a single row based on groups of rows, rather than on single rows. Aggregate functions can appear in select lists andHAVINGclauses.

      1. Aggregate functions

If you use the SGROUPBYclause in aSELECTstatement,SQL divides the rows of a queried table into groups. In a query containing aSGROUPBYclause, all elements of the select list must be expressions from theSGROUPBYclause, expressions containing aggregate functions, or constants.SQL applies the aggregate functions in the select list to each group of rows and returns a single result row for each group.

If you omit the SGROUPBYclause, Oracle applies aggregate functions in the select list to all the rows in the queried table.You use aggregate functions in theHAVINGclause to eliminate groups from the output based on the results of the aggregate functions, rather than on the values of the individual rows of the queried table or view.SGROUPBYand HAVINGclauses will be studied in this Lab later.

Many aggregate functions accept these options:

DISTINCT

- causes an aggregate function to consider only distinct values of the argument expression.

ALL

- causes an aggregate function to consider all values, including all duplicates.

All aggregate functions except COUNT(*) ignore nulls. You can use the NVL in the argument to an aggregate function to substitute a value for a null.

If a query with an aggregate function returns no rows or only rows with nulls for the argument to the aggregate function, the aggregate function returns null

There are the following aggregate functions:

Function

Syntax

Purpose

Example

COUNT

Returns the number of rows in the que­ry. If you specify expr, this func­tion returns rows where expr is not null. You can count either all rows (ALL), or only distinct (DISTINCT) values of expr.

If you specify the asterisk (*), this function returns all rows, including duplicates and nulls.

SELECT COUNT(*) AS Total

FROM TEACHER;

SELECT COUNT(post)

FROM TEACHER;

SELECT COUNT(DISTINCT post)

FROM TEACHER;

AVG

Returns average of values in a column n.

SELECT AVG(Salary)

FROM TEACHER

WHERE Post='professor';

MIN

Returns minimum value of expr

SELECT MIN(Hiredate)

FROM TEACHER;

MAX

Returns maximum value of expr.

SELECT MAX(Hiredate)

FROM TEACHER

WHERE Name LIKE 'А%';

SUM

Returns sum of values in a column n.

SELECT SUM(Salary)

FROM TEACHER

WHERE Post='assistant';

STDEV

Returns standard deviation of x, a number. SQL calculates the standard deviation as the square root of the variance defined for the VARIANCE aggregate function

SELECT STDEV(Salary)

FROM TEACHER;

VARIANCE

Returns variance of x, a number. SQL calculates the variance of x using this formula:

where:

xi is one of the elements of x. n is the number of elements in the set x. If n is 1, the variance is defined to be 0.

SELECT VARIANCE(Salary)

FROM TEACHER;

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