Oracle Database 11g: SQL Lesson 05

download Oracle Database 11g: SQL Lesson 05

of 29

Transcript of Oracle Database 11g: SQL Lesson 05

  • 7/29/2019 Oracle Database 11g: SQL Lesson 05

    1/29

    Copyright 2010, Oracle and/or its affiliates. All rights reserved.

    Reporting Aggregated Data

    Using the Group Functions

  • 7/29/2019 Oracle Database 11g: SQL Lesson 05

    2/29

    Copyright 2010, Oracle and/or its affiliates. All rights reserved.5 - 2

    Objectives

    After completing this lesson, you should be able to do thefollowing:

    Identify the available group functions

    Describe the use of group functions

    Group data by using the GROUPBY clause

    Include or exclude grouped rows by using the HAVING

    clause

  • 7/29/2019 Oracle Database 11g: SQL Lesson 05

    3/29

    Copyright 2010, Oracle and/or its affiliates. All rights reserved.5 - 3

    Lesson Agenda

    Group functions: Types and syntax

    Use AVG, SUM, MIN, MAX, COUNT

    Use the DISTINCT keyword within group functions

    NULL values in a group function Grouping rows:

    GROUPBY clause HAVING clause

    Nesting group functions

  • 7/29/2019 Oracle Database 11g: SQL Lesson 05

    4/29

    Copyright 2010, Oracle and/or its affiliates. All rights reserved.5 - 4

    What Are Group Functions?

    Group functions operate on sets of rows to give one result pergroup.

    EMPLOYEES

    Maximum salary in

    EMPLOYEEStable

  • 7/29/2019 Oracle Database 11g: SQL Lesson 05

    5/29

    Copyright 2010, Oracle and/or its affiliates. All rights reserved.5 - 5

    Types of Group Functions

    AVG

    COUNT

    MAX

    MIN

    STDDEV

    SUM

    VARIANCE

    Groupfunctions

  • 7/29/2019 Oracle Database 11g: SQL Lesson 05

    6/29

    Copyright 2010, Oracle and/or its affiliates. All rights reserved.5 - 6

    SELECT group_function(column), ...

    FROM table

    [WHERE condition]

    [ORDER BY column];

    Group Functions: Syntax

  • 7/29/2019 Oracle Database 11g: SQL Lesson 05

    7/29Copyright 2010, Oracle and/or its affiliates. All rights reserved.5 - 7

    SELECT AVG(salary), MAX(salary),

    MIN(salary), SUM(salary)

    FROM employees

    WHERE job_id LIKE '%REP%';

    Using theAVG and SUMFunctions

    You can use AVG and SUM for numeric data.

  • 7/29/2019 Oracle Database 11g: SQL Lesson 05

    8/29Copyright 2010, Oracle and/or its affiliates. All rights reserved.5 - 8

    SELECT MIN(hire_date), MAX(hire_date)

    FROM employees;

    Using theMIN andMAX Functions

    You can use MIN and MAX for numeric, character, and datedata types.

  • 7/29/2019 Oracle Database 11g: SQL Lesson 05

    9/29Copyright 2010, Oracle and/or its affiliates. All rights reserved.5 - 9

    Using the COUNT Function

    COUNT(*) returns the number of rows in a table:

    COUNT(expr) returns the number of rows with non-null values

    forexpr:

    SELECT COUNT(commission_pct)

    FROM employeesWHERE department_id = 80;

    SELECT COUNT(*)

    FROM employees

    WHERE department_id = 50;1

    2

  • 7/29/2019 Oracle Database 11g: SQL Lesson 05

    10/29Copyright 2010, Oracle and/or its affiliates. All rights reserved.5 - 10

    SELECT COUNT(DISTINCT department_id)

    FROM employees;

    Using the DISTINCT Keyword

    COUNT(DISTINCTexpr) returns the number of distinctnon-null values ofexpr.

    To display the number of distinct department values in theEMPLOYEES table:

  • 7/29/2019 Oracle Database 11g: SQL Lesson 05

    11/29Copyright 2010, Oracle and/or its affiliates. All rights reserved.5 - 11

    Group Functions and Null Values

    Group functions ignore null values in the column:

    The NVL function forces group functions to include null values:

    SELECT AVG(commission_pct)

    FROM employees;

    SELECT AVG(NVL(commission_pct, 0))

    FROM employees;

    1

    2

  • 7/29/2019 Oracle Database 11g: SQL Lesson 05

    12/29Copyright 2010, Oracle and/or its affiliates. All rights reserved.5 - 12

    Lesson Agenda

    Group functions: Types and syntax

    Use AVG, SUM, MIN, MAX, COUNT

    Use DISTINCT keyword within group functions

    NULL values in a group function Grouping rows:

    GROUPBY clause

    HAVING clause

    Nesting group functions

  • 7/29/2019 Oracle Database 11g: SQL Lesson 05

    13/29Copyright 2010, Oracle and/or its affiliates. All rights reserved.5 - 13

    Creating Groups of Data

    EMPLOYEES

    4400

    9500

    3500

    6400

    10033

    Average salary in theEMPLOYEES table for

    each department

  • 7/29/2019 Oracle Database 11g: SQL Lesson 05

    14/29Copyright 2010, Oracle and/or its affiliates. All rights reserved.5 - 14

    Creating Groups of Data: GROUPBY Clause Syntax

    You can divide rows in a table into smaller groups by using theGROUPBY clause.

    SELECT column, group_function(column)

    FROM table

    [WHERE condition]

    [GROUP BY group_by_expression][ORDER BY column];

  • 7/29/2019 Oracle Database 11g: SQL Lesson 05

    15/29Copyright 2010, Oracle and/or its affiliates. All rights reserved.5 - 15

    SELECT department_id, AVG(salary)

    FROM employees

    GROUP BY department_id ;

    Using the GROUPBY Clause

    All the columns in the SELECT list that are not in groupfunctions must be in the GROUPBY clause.

  • 7/29/2019 Oracle Database 11g: SQL Lesson 05

    16/29Copyright 2010, Oracle and/or its affiliates. All rights reserved.5 - 16

    Using the GROUPBY Clause

    The GROUPBY column does not have to be in the SELECT list.

    SELECT AVG(salary)

    FROM employees

    GROUP BY department_id ;

  • 7/29/2019 Oracle Database 11g: SQL Lesson 05

    17/29Copyright 2010, Oracle and/or its affiliates. All rights reserved.5 - 17

    Grouping by More Than One Column

    EMPLOYEES Add the salaries in the EMPLOYEEStable for each job, grouped bydepartment.

  • 7/29/2019 Oracle Database 11g: SQL Lesson 05

    18/29Copyright 2010, Oracle and/or its affiliates. All rights reserved.5 - 18

    SELECT department_id, job_id, SUM(salary)

    FROM employees

    WHERE department_id > 40

    GROUP BY department_id, job_id

    ORDER BY department_id;

    Using the GROUPBY Clause on Multiple Columns

  • 7/29/2019 Oracle Database 11g: SQL Lesson 05

    19/29Copyright 2010, Oracle and/or its affiliates. All rights reserved.5 - 19

    Illegal Queries Using Group Functions

    Any column or expression in the SELECT list that is not anaggregate function must be in the GROUPBY clause:

    SELECT department_id, COUNT(last_name)

    FROM employees;

    SELECT department_id, job_id, COUNT(last_name)

    FROM employees

    GROUP BY department_id;

    A GROUPBY clause must be added tocount the last names for eachdepartment_id.

    Either add job_idin the GROUPBY orremove the job_idcolumn from theSELECT list.

  • 7/29/2019 Oracle Database 11g: SQL Lesson 05

    20/29Copyright 2010, Oracle and/or its affiliates. All rights reserved.5 - 20

    Illegal Queries Using Group Functions

    You cannot use the WHERE clause to restrict groups. You use the HAVING clause to restrict groups.

    You cannot use group functions in the WHERE clause.

    SELECT department_id, AVG(salary)

    FROM employeesWHERE AVG(salary) > 8000

    GROUP BY department_id;

    Cannot use the

    WHERE clause torestrict groups

  • 7/29/2019 Oracle Database 11g: SQL Lesson 05

    21/29Copyright 2010, Oracle and/or its affiliates. All rights reserved.5 - 21

    Restricting Group Results

    EMPLOYEES

    The maximum salary perdepartment when it isgreater than $10,000

  • 7/29/2019 Oracle Database 11g: SQL Lesson 05

    22/29Copyright 2010, Oracle and/or its affiliates. All rights reserved.5 - 22

    SELECT column, group_functionFROM table

    [WHERE condition]

    [GROUP BY group_by_expression]

    [HAVING group_condition][ORDER BY column];

    Restricting Group Results with the HAVING Clause

    When you use the HAVING clause, the Oracle server restrictsgroups as follows:

    1. Rows are grouped.

    2. The group function is applied.

    3. Groups matching the HAVING clause are displayed.

  • 7/29/2019 Oracle Database 11g: SQL Lesson 05

    23/29Copyright 2010, Oracle and/or its affiliates. All rights reserved.5 - 23

    SELECT department_id, MAX(salary)

    FROM employees

    GROUP BY department_id

    HAVING MAX(salary)>10000 ;

    Using the HAVING Clause

  • 7/29/2019 Oracle Database 11g: SQL Lesson 05

    24/29Copyright 2010, Oracle and/or its affiliates. All rights reserved.5 - 24

    SELECT job_id, SUM(salary) PAYROLL

    FROM employees

    WHERE job_id NOT LIKE '%REP%'

    GROUP BY job_id

    HAVING SUM(salary) > 13000

    ORDER BY SUM(salary);

    Using the HAVING Clause

  • 7/29/2019 Oracle Database 11g: SQL Lesson 05

    25/29Copyright 2010, Oracle and/or its affiliates. All rights reserved.5 - 25

    Lesson Agenda

    Group functions: Types and syntax

    Use AVG, SUM, MIN, MAX, COUNT

    Use DISTINCT keyword within group functions

    NULL values in a group function

    Grouping rows:

    GROUPBY clause

    HAVING clause

    Nesting group functions

  • 7/29/2019 Oracle Database 11g: SQL Lesson 05

    26/29

    Copyright 2010, Oracle and/or its affiliates. All rights reserved.5 - 26

    SELECT MAX(AVG(salary))

    FROM employees

    GROUP BY department_id;

    Nesting Group Functions

    Display the maximum average salary:

  • 7/29/2019 Oracle Database 11g: SQL Lesson 05

    27/29

    Copyright 2010, Oracle and/or its affiliates. All rights reserved.5 - 27

    Quiz

    Identify the guidelines for group functions and the GROUPBYclause.

    1. You cannot use a column alias in the GROUPBY clause.

    2. The GROUPBY column must be in the SELECT clause.

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

    4. The GROUPBY 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.

  • 7/29/2019 Oracle Database 11g: SQL Lesson 05

    28/29

    Copyright 2010, Oracle and/or its affiliates. All rights reserved.5 - 28

    SELECT column, group_function

    FROM table[WHERE condition]

    [GROUP BY group_by_expression]

    [HAVING group_condition]

    [ORDER BY column];

    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 GROUPBY clause

    Write queries that use the HAVING clause

  • 7/29/2019 Oracle Database 11g: SQL Lesson 05

    29/29

    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