Basic Group Functions (without GROUP BY clause) Week 5 – Chapter 5.

Slides:



Advertisements
Similar presentations
Group Functions Using GROUP BY clause Week 5 – Chapter 5.
Advertisements

Copyright  Oracle Corporation, All rights reserved. 4 Aggregating Data Using Group Functions.
5 5 Aggregating Data Using Group Functions Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina,
Database Programming Sections 5 & 6 – Group functions, COUNT, DISTINCT, NVL, GROUP BY, HAVING clauses, Subqueries.
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: Identify the available group.
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
Bordoloi and Bock Chapter 5 : Aggregate Row Functions.
Copyright  Oracle Corporation, All rights reserved. 5 Aggregating Data Using Group Functions.
1Eyad alshareef Enhanced Guide to Oracle 10g Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
GROUP FUNCTIONS. Objectives After completing this lesson, you should be able to do the following: Identify the available group functions Describe the.
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: Identify the available group.
5 Copyright © 2007, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
DATABASE PROGRAMMING Sections 5-7. Write a query that shows the average, maximum, and minimum salaries for all employees with jobs in the programming.
4 การใช้ SQL Functions. Copyright © 2007, Oracle. All rights reserved What Are Group Functions? Group functions operate on sets of rows to give.
Chapter 11 Group Functions
After completing this lesson, you should be able to do the following: Identify the available group functions Describe the use of group functions Group.
LECTURE 10.  Group functions operate on sets of rows to give one result per group.
Introduction to Oracle9i: SQL1 SQL Group Functions.
Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL.
SELECT Advanced. Sorting data in a table The ORDER BY clause is used for sorting the data in either ascending or descending order depending on the condition.
Enhancements to the GROUP BY Clause Fresher Learning Program January, 2012.
Database Programming Sections 5– GROUP BY, HAVING clauses, Rollup & Cube Operations, Grouping Set, Set Operations 11/2/10.
SQL - Part 2 Much of the material presented in these slides was developed by Dr. Ramon Lawrence at the University of Iowa.
Chapter 6 Group Functions. Chapter Objectives  Differentiate between single-row and multiple-row functions  Use the SUM and AVG functions for numeric.
4-1 Copyright  Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Functions Oracle Labs 5 & 6. 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 2 SQL Functions Function arg n arg 2 arg 1. Input Resulting Value.
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
Copyright س Oracle Corporation, All rights reserved. 5 Aggregating Data Using Group Functions.
SELECT Statements Lecture Notes Sree Nilakanta Fall 2010 (rev)
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
Structured Query Language. Group Functions What are group functions ? Group Functions Group functions operate on sets of rows to give one result per group.
Nikolay Kostov Telerik Corporation
Intermediate SQL: Aggregated Data, Joins and Set Operators.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
Retrieving Data Using the SQL SELECT Statement. Objectives After completing this lesson, you should be able to do the following: – List the capabilities.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
An Introduction To SQL Part 2 (Special thanks to Geoff Leese)
1 Information Retrieval and Use (IRU) An Introduction To SQL Part 2.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Grouping These slides are licensed under.
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: –Identify the available group.
Agenda for Class - 03/04/2014 Answer questions about HW#5 and HW#6 Review query syntax. Discuss group functions and summary output with the GROUP BY statement.
Lab 3: Single-row Functions College of Information Technology, Universiti Tenaga Nasional 1 CISB224 02A, 02B Semester I, 2009/2010.
Aggregating Data Using Group Functions. What Are Group Functions? Group functions operate on sets of rows to give one result per group.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
Defining a Column Alias
5-1 Copyright © 2004, Oracle. All rights reserved. DISPLAYING DATA FROM MULTIPLE TABLES OUTER JOIN.
Oracle 10g Retrieving Data Using the SQL SELECT Statement.
Reporting Aggregated Data Using the Group Functions
Aggregating Data Using Group Functions
Enhanced Guide to Oracle 10g
Working with Tables: Join, Functions and Grouping
Chapter 5: Aggregate Functions and Grouping of Data
Aggregating Data Using Group Functions
(SQL) Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Reporting Aggregated Data Using the Group Functions
Section 4 - Sorting/Functions
Reporting Aggregated Data Using the Group Functions
Reporting Aggregated Data Using the Group Functions
Lab 3: Single-row Functions
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Database Programming Using Oracle 11g
Aggregating Data Using Group Functions
Presentation transcript:

Basic Group Functions (without GROUP BY clause) Week 5 – Chapter 5

Objectives Identify group functions Describe how group functions (without a GROUP BY clause) are used

EMPLOYEES maximum salary in salary in the EMPLOYEES table ID SALARY … MAXSAL Group functions operate on sets of rows to give a one row result per group, eg: Group Function Illustration SELECT MAX(salary) MAXSAL FROM employees;

Group Functions COUNT(column) – counts the number of rows with a non-null value for specified column SUM(column) – sums all values for column AVG(column) – averages all non-null values for column MIN(column) – finds minimum of values for column MAX(column) – finds maximum of values for column STDDEV(column) – provides standard deviation of column values VARIANCE(column) – provides variance of column values

Syntax of Group Functions The general syntax for a SELECT statement with a group function and no GROUP BY clause is: SELECT group_function(expression), group_function(expression), … FROMtable [WHEREcondition];  A SELECT statement with a group function(s) and no GROUP BY clause returns only one row as its result  When functions are used in SELECT clause use of an alias is recommended so heading will be meaningful eg SELECT COUNT(*) AS NUMEMPS FROM employees

COUNT Function COUNT counts the number of rows containing a value in the specified column and returns a single value A specific column may be provided as the argument or an * is used to represent all columns The values returned by COUNT(*) and COUNT(column) can differ if nulls are permitted in the column (There can never be a row in a table containing only NULLS since every row in a table must have a value for the Primary Key.)

COUNT Function Examples Example 1: How many employees are there in the company? Example 2: How many employees are in department 20? NUMEMPS 20 NUMDEPT20 2 SELECT COUNT(*) NUMEMPS FROMemployees; SELECT COUNT(*) NUMDEPT20 FROM employees WHERE department_id = 20; Example 3: How many employees are in depts? SELECT COUNT(department_id) DEP FROM employees; DEP 19

COUNT Function Examples(ctd)  Example 4: How many employees are in the company and how many employees are paid on commission? (NULL values are not included in the COUNT of a column!) NUMEMPS NUMCOMM 20 4 SELECT COUNT(*) NUMEMPS, COUNT(commission_pct) NUMCOMM FROM employees

COUNT Function Examples(ctd) Example 5: How many employees are in departments and how many departments are these employees assigned to? (Shows use of DISTINCT with COUNT to count number of distinct values). Default of COUNT is to consider all values including duplicates; use of DISTINCT makes the function count duplicated values only once. SELECTCOUNT(department_id) AS "# EMPS IN DEPTS", COUNT(DISTINCT department_id) AS "# DEPTS with EMPS" FROMemployees; # EMPS IN DEPTS # DEPTS with EMPS 19 7

AVG Function AVG function will work only on numeric columns such as SAL. The average value of the column for a group of 0 or more rows is calculated and a single value returned (i.e. total of all non-null values for the column in rows specified divided by the number of non-null values found) NULL values are ignored by AVG function To have NULLs considered in the calculation of the average you must also use the NVL function

AVG Function Examples SELECTAVG(salary) AVERAGE FROMemployees WHERE department_id = 80; NOTE: When AVG is used, the value returned should normally be rounded or truncated Example 1: Display the average salary paid to employees AVERAGE AVERAGE SELECTROUND(AVG(salary),2) AVERAGE FROMemployees WHERE department_id = 80;

AVG Function Examples (ctd) Example 2: Display the average salary and average commission for all employees paid on commission SELECTCOUNT(*) NUMCOMM, ROUND(AVG(salary),2) AVGSAL, ROUND(AVG(commission_pct),2) AVGCOMM FROMemployees WHERE commission_pct IS NOT NULL; NUMCOMM AVGSAL AVGCOMM

AVG Function Examples (ctd) Example 3: Display the average salary and average commission for all employees (note: NULLL values may occur in commission_pct column so convert to 0 for calculations) SELECTCOUNT(*) NUMCOMM, ROUND(AVG(salary),2) AVGSAL, ROUND(AVG(NVL(commission_pct,0)),2) AVGCOMM FROMemployees; NUMCOMM AVGSAL AVGCOMM

SUM Function SUM works on numeric values such as Salary. The total of the values in the column is calculated and this single value is returned. Example: what is the total of all salaries paid SELECTSUM (salary) TOTSAL FROMemployees; TOTSAL

MIN and MAX Functions MIN returns the minimum value found in the column (NULL does not count as a value unless the NVL function is used) MAX returns maximum value found in column MIN and MAX will work on columns of any data- type Example 1: What are the lowest and highest salaries? MINSAL MAXSAL SELECT MIN(salary) MINSAL, MAX(salary) MAXSAL FROMemployees;

MIN and MAX Functions (ctd) Example 2: What are the earliest and latest hire dates for employees? EARLIEST LATEST 17-JUN JAN-00 SELECT MIN(hire_date) AS EARLIEST, MAX(hire_date) AS LATEST FROMemployees;

MIN and MAX Functions (ctd) Example 2: What are alphabetically the first and last values of last name? FIRST LAST Davies Zlotkey SELECT MIN(last_name) AS FIRST, MAX(last_name) AS LAST FROMemployees;

Important Note on Group Functions If you include a group function in a SELECT clause, you cannot display values from individual rows as well (unless the individual column appears in the GROUP BY clause as will be seen next week). You will receive an error message if you include a group function and a non-group expression referencing a column in a SELECT clause

Group Function Error Example: SELECTdepartment_id, AVG(salary) FROMemployees;  ERROR at line 1: ORA-00937: not a single-group group function  Query does not make sense: there is only one value of average salary to display and there will be 14 values of dept_no to display (one for each row of the employees table)