Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
all labs.pdf
Скачиваний:
74
Добавлен:
25.02.2016
Размер:
2.04 Mб
Скачать

Nesting Group Functions

Display the maximum average salary:

SELECT MAX(AVG(salary))

FROM employees

GROUP BY department_id;

Nesting Group Functions

Copyright © 2009, Oracle. All rights reserved.

Academy

 

 

 

 

 

 

Oracle

 

Group functions can be nested to a depth of two functions. The xample in the slide calculates the

 

average salary for each department_id and then displays the maximum average salary.

 

Note that GROUP BY clause is mandatory when nesting g oup functions.

 

 

&

Only

 

 

Internal

 

 

 

Oracle

Use

 

 

 

 

 

Oracle Database 11g: SQL Fundamentals I 5 - 26

Quiz

Identify the guidelines for group functions and the GROUP BY clause.

1. You cannot use a column alias in the GROUP BY clause. 2. The GROUP BY column must be in the SELECT clause.

3. By using a WHERE clause, you can exclude rows before dividing them into groups.

4. The GROUP BY clause groups rows and ensures order of the result set.

5.If you include a group function in a SELECT clause, you cannot select individual results as well.

Answer: 1, 3

Copyright © 2009, Oracle. All rights reserved.

Academy

 

 

 

 

 

 

Oracle

 

 

&

Only

 

 

Internal

 

 

 

Oracle

Use

 

 

 

 

 

Oracle Database 11g: SQL Fundamentals I 5 - 27

Summary

In this lesson, you should have learned how to:

Use the group functions COUNT, MAX, MIN, SUM, and AVG

Write queries that use the GROUP BY clause

Write queries that use the HAVING clause

SELECT column, group_function FROM table

[WHERE condition]

[GROUP BY group_by_expression] [HAVING group_condition]

[ORDER BY column];

Copyright © 2009, Oracle. All rights reserved.

Academy

Summary

There are several group functions available in SQL, such as:

 

AVG, COUNT, MAX, MIN, SUM, STDDEV, and VARIANCE

 

Oracle

You can create subgroups by using the GROUP BY clause. Further, groups can be restricted using the

HAVING clause.

Place the HAVING and GROUP BY clauses after the WHERE c ause in a statement. The order of the

GROUP BY and HAVING clauses following&the WHERE clause is not important. Place the ORDER BY

clause at the end.

Only

The Oracle server evaluates the clauses in the following order:

1.

If the statement con ains a WHERE clau e, the server establishes the candidate rows.

2.

The server identifies

he groups thatUseare specified in the GROUP BY clause.

3.

The HAVING Internalclause further restricts result groups that do not meet the group criteria in the

 

HAVING c ause.

 

Oracle

 

Note: For a omplete list of the group functions, see Oracle Database SQL Language Reference 11g, Rele se 1 (11.1).

Oracle Database 11g: SQL Fundamentals I 5 - 28

Practice 5: Overview

This practice covers the following topics:

Writing queries that use the group functions

Grouping by rows to achieve more than one result

Restricting groups by using the HAVING clause

Practice 5: Overview

Copyright © 2009, Oracle. All rights reserved.

Academy

 

 

 

 

 

 

Oracle

 

At the end of this practice, you should be familiar with using group functions and selecting groups of

 

data.

 

 

Only

 

 

 

&

 

 

Internal

 

 

 

Oracle

Use

 

 

 

 

 

Oracle Database 11g: SQL Fundamentals I 5 - 29

Practice 5

Determine the validity of the following three statements. Circle either True or False.

1.Group functions work across many rows to produce one result per group. True/False

2.Group functions include nulls in calculations.

True/False

3.The WHERE clause restricts rows before inclusion in a group calculation. True/False

The HR department needs the following reports:

4. Find the highest, lowest, sum, and average salary of all employees. Label the columns

as Maximum, Minimum, Sum, and Average, respectively. Round your results to the nearest whole number. Save your SQL statement as lab_05_04.sql. Run the query.

 

 

Academy

5. Modify the query in lab_05_04.sql to display the minimum, maximum, sum, and average

salary for each job type. Resave lab_05_04.sql as lab_05 05.sql. Run the statement

in lab_05_05.sql.

 

 

 

Oracle

 

&

Only

Internal

 

 

Use

 

Oracle

 

 

Oracle Database 11g: SQL Fundamentals I 5 - 30

Practice 5 (continued)

6. Write a query to display the number of people with the same job.

 

 

Academy

Generalize the query so that the user in the HR department is prompted for a job title. Save the script

to a file named lab_05_06.sql. Run the query. Enter IT_PROG when prompted.

 

Oracle

7. Determine the number of managers without listing them. Lab l the column as Number of

Managers. Hint: Use the MANAGER_ID column to determine the number of managers.

&

Only

Internal

 

 

8. Find the difference between the highest and lowest salaries. Label the column DIFFERENCE.

Use

Oracle

 

 

Oracle Database 11g: SQL Fundamentals I 5 - 31

Practice 5 (continued)

If you have time, complete the following exercises:

9.Create a report to display the manager number and the salary of the lowest-paid employee for that manager. Exclude anyone whose manager is not known. Exclude any groups where the minimum salary is $6,000 or less. Sort the output in descending order of salary.

If you want an extra challenge, complete the following exercises:

10. Create a query to display the total number of employees and, of that total, the number of employees hired in 1995, 1996, 1997, and 1998. Create appropriate column headings.

 

 

Academy

11. Create a matrix query to display the job, the salary for that job based on department number,

and the total salary for that job, for departments 20, 50, 80, and 90, giving each colu n an

appropriate heading.

 

 

 

Oracle

 

&

Only

Internal

 

 

Use

 

Oracle

 

 

Oracle Database 11g: SQL Fundamentals I 5 - 32

Displaying Data from Multiple Tables

 

Copyright © 2009, Oracle. All rights reserved.

Academy

 

 

 

 

Oracle

&

Only

 

 

Internal

 

 

 

 

Use

 

Oracle

 

 

 

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]