What Are Group Functions? Reporting Aggregated Data Using the Group Functions. Objectives. Types of Group Functions

Similar documents
Reporting Aggregated Data Using the Group Functions. Copyright 2004, Oracle. All rights reserved.

Intermediate SQL: Aggregated Data, Joins and Set Operators

KORA. RDBMS Concepts II

Aggregate Functions. Eng. Mohammed Alokshiya. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Dept. Database Lab (ECOM 4113)

GIFT Department of Computing Science. CS-217: Database Systems. Lab-4 Manual. Reporting Aggregated Data using Group Functions

Institute of Aga. Network Database LECTURER NIYAZ M. SALIH

Institute of Aga. Microsoft SQL Server LECTURER NIYAZ M. SALIH

DUE: 9. Create a query that will return the average order total for all Global Fast Foods orders from January 1, 2002, to December 21, 2002.

Database Management Systems,

Introduction to Oracle9i: SQL

Subquery: There are basically three types of subqueries are:

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

Oracle Database SQL Basics

ÇALIŞMA TEST SORULARI

SQL Functions (Single-Row, Aggregate)

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

CS Week 10 - Page 1

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

Database Systems CSE 303. Outline. Lecture 06: SQL. What is Sub-query? Sub-query in WHERE clause Subquery

CSC Web Programming. Introduction to SQL

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

Based on the following Table(s), Write down the queries as indicated: 1. Write an SQL query to insert a new row in table Dept with values: 4, Prog, MO

CMP-3440 Database Systems

Manipulating Data. Copyright 2004, Oracle. All rights reserved.

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

Oracle Database 11g: SQL and PL/SQL Fundamentals

Lab # 6. Using Subqueries and Set Operators. Eng. Alaa O Shama

Oracle Syllabus Course code-r10605 SQL

EXISTS NOT EXISTS WITH

Oracle MOOC: SQL Fundamentals

ASSIGNMENT NO Computer System with Open Source Operating System. 2. Mysql

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

Limit Rows Selected. Copyright 2008, Oracle. All rights reserved.

Slicing and Dicing Data in CF and SQL: Part 1

Objectives. After completing this lesson, you should be able to do the following:

Database Foundations. 6-4 Data Manipulation Language (DML) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

RETRIEVING DATA USING THE SQL SELECT STATEMENT

1 Writing Basic SQL SELECT Statements 2 Restricting and Sorting Data

Alkérdések II. Copyright 2004, Oracle. All rights reserved.

STRUCTURED QUERY LANGUAGE (SQL)

Retrieving Data Using the SQL SELECT Statement. Copyright 2009, Oracle. All rights reserved.

Database 2: Slicing and Dicing Data in CF and SQL

Using the Set Operators. Copyright 2006, Oracle. All rights reserved.

Advanced SQL GROUP BY Clause and Aggregate Functions Pg 1

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

Table of Contents. PDF created with FinePrint pdffactory Pro trial version

SQL Queries. COSC 304 Introduction to Database Systems SQL. Example Relations. SQL and Relational Algebra. Example Relation Instances

Using the Set Operators. Copyright 2004, Oracle. All rights reserved.

CS 275 Winter 2011 Problem Set 3

Bsc (Hons) Software Engineering. Examinations for / Semester 1. Resit Examinations for BSE/15A/FT & BSE/16A/FT

1Z0-071 Exam Questions Demo Oracle. Exam Questions 1Z Oracle Database 12c SQL.

1Z0-071 Exam Questions Demo Oracle. Exam Questions 1Z Oracle Database 12c SQL.

Join, Sub queries and set operators

COSC 304 Introduction to Database Systems SQL. Dr. Ramon Lawrence University of British Columbia Okanagan

Monitoring and Resolving Lock Conflicts. Copyright 2004, Oracle. All rights reserved.

Why Relational Databases? Relational databases allow for the storage and analysis of large amounts of data.

GIFT Department of Computing Science Data Selection and Filtering using the SELECT Statement

Oracle Database: SQL Fundamentals I. Oracle Internal & Oracle Academy Use Only. Volume II Student Guide. D64258GC10 Edition 1.0 January 2010 D65028

SQL Data Query Language

1Z Oracle Database 11g - SQL Fundamentals I Exam Summary Syllabus Questions

Data Manipulation Language

Database Programming with SQL

STIDistrict Query (Basic)

Testing Masters Technologies

SELF TEST. List the Capabilities of SQL SELECT Statements

Chapter 6. SQL Data Manipulation

Exam: 1Z Title : Introduction to Oracle9i: SQL. Ver :

Database Programming with PL/SQL

CONCAT SUBSTR LOWER (*) All three will be evaluated simultaneously. Correct

Database Programming - Section 7. Instructor Guide

Using DDL Statements to Create and Manage Tables. Copyright 2006, Oracle. All rights reserved.

Creating Other Schema Objects

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

Copyright 2017, Oracle and/or its aff iliates. All rights reserved.

SQL Structured Query Language (1/2)

SQL. Rodrigo García Carmona Universidad San Pablo-CEU Escuela Politécnica Superior

Fundamentals of Database Systems

Additional Practice Solutions

CS348 INFORMATION SYSTEMS

Test Bank for Database Processing Fundamentals Design and Implementation 13th Edition by Kroenke

COMP 244 DATABASE CONCEPTS & APPLICATIONS

Chapter 8. Joined Relations. Joined Relations. SQL-99: Schema Definition, Basic Constraints, and Queries

Full file at

Oracle Database: Introduction to SQL Ed 2

Táblák tartalmának módosítása. Copyright 2004, Oracle. All rights reserved.

SQL queries II. Set operations and joins

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

Databases - 5. Problems with the relational model Functions and sub-queries

CS 464/564 Introduction to Database Management System Instructor: Abdullah Mueen

GIFT Department of Computing Science. CS-217/224: Database Systems. Lab-5 Manual. Displaying Data from Multiple Tables - SQL Joins

T-SQL Training: T-SQL for SQL Server for Developers

You can write a command to retrieve specified columns and all rows from a table, as illustrated

ITCertMaster. Safe, simple and fast. 100% Pass guarantee! IT Certification Guaranteed, The Easy Way!

5. Single-row function

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

Some Basic Aggregate Functions FUNCTION OUTPUT The number of rows containing non-null values The maximum attribute value encountered in a given column

Oracle Database 11g: SQL Fundamentals II

Transcription:

What Are Group Functions? Group functions operate on sets of rows to give one result per group. Reporting Aggregated Data Using the Group Functions Maximum salary in table Copyright 2004, Oracle. All rights reserved. Objectives 4-3 Copyright 2004, Oracle. All rights reserved. Types of 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 data by using the GROUP BY clause Include or exclude grouped rows by using the HAVING clause AVG COUNT MAX MIN STDDEV SUM VARIANCE Group functions 4-2 Copyright 2004, Oracle. All rights reserved. 4-4 Copyright 2004, Oracle. All rights reserved.

Group Functions: Syntax Using the MIN and MAX Functions SELECT [column,] group_function(column),... [GROUP BY column] You can use MIN and MAX for numeric, character, and date data types. SELECT MIN(hire_date), MAX(hire_date) ; 4-5 Copyright 2004, Oracle. All rights reserved. Using the AVG and SUM Functions 4-7 Copyright 2004, Oracle. All rights reserved. Using the COUNT Function You can use AVG and SUM for numeric data. SELECT AVG(salary), MAX(salary), MIN(salary), SUM(salary) WHERE job_id LIKE '%REP%'; 1 COUNT(*) returns the number of rows in a table: SELECT COUNT(*) WHERE department_id = 50; 2 COUNT(expr) returns the number of rows with nonnull values for the expr: SELECT COUNT(commission_pct) WHERE department_id = 80; 4-6 Copyright 2004, Oracle. All rights reserved. 4-8 Copyright 2004, Oracle. All rights reserved.

Using the DISTINCT Keyword Creating Groups of Data COUNT(DISTINCT expr) returns the number of distinct non-null values of the expr. To display the number of distinct department values in the table: SELECT COUNT(DISTINCT department_id) ; 4400 9500 3500 6400 10033 Average salary in table for each department 4-9 Copyright 2004, Oracle. All rights reserved. Group Functions and Null Values 4-11 Copyright 2004, Oracle. All rights reserved. Creating Groups of Data: GROUP BY Clause Syntax 1 2 Group functions ignore null values in the column: SELECT AVG(commission_pct) ; The NVL function forces group functions to include null values: SELECT AVG(NVL(commission_pct, 0)) ; SELECT column, group_function(column) [GROUP BY group_by_expression] You can divide rows in a table into smaller groups by using the GROUP BY clause. 4-10 Copyright 2004, Oracle. All rights reserved. 4-12 Copyright 2004, Oracle. All rights reserved.

Using the GROUP BY Clause Grouping by More Than One Column All columns in the SELECT list that are not in group functions must be in the GROUP BY clause. SELECT department_id, AVG(salary) GROUP BY department_id ; Add the salaries in the table for each job, grouped by department 4-13 Copyright 2004, Oracle. All rights reserved. Using the GROUP BY Clause 4-15 Copyright 2004, Oracle. All rights reserved. Using the GROUP BY Clause on Multiple Columns The GROUP BY column does not have to be in the SELECT list. SELECT AVG(salary) GROUP BY department_id ; SELECT department_id dept_id, job_id, SUM(salary) GROUP BY department_id, job_id ; 4-14 Copyright 2004, Oracle. All rights reserved. 4-16 Copyright 2004, Oracle. All rights reserved.

Illegal Queries Using Group Functions Restricting Group Results Any column or expression in the SELECT list that is not an aggregate function must be in the GROUP BY clause: SELECT department_id, COUNT(last_name) ; SELECT department_id, COUNT(last_name) * ERROR at line 1: ORA-00937: not a single-group group function Column missing in the GROUP BY clause The maximum salary per department when it is greater than $10,000 4-17 Copyright 2004, Oracle. All rights reserved. Illegal Queries Using Group Functions 4-19 Copyright 2004, Oracle. All rights reserved. Restricting Group Results with the HAVING Clause 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) WHERE AVG(salary) > 8000 GROUP BY department_id; WHERE AVG(salary) > 8000 * ERROR at line 3: ORA-00934: group function is not allowed here Cannot use the WHERE clause to restrict groups When you use the HAVING clause, the Oracle server restricts groups as follows: 1. Rows are grouped. 2. The group function is applied. 3. Groups matching the HAVING clause are displayed. SELECT column, group_function [GROUP BY group_by_expression] [HAVING group_condition] 4-18 Copyright 2004, Oracle. All rights reserved. 4-20 Copyright 2004, Oracle. All rights reserved.

Using the HAVING Clause Nesting Group Functions SELECT department_id, MAX(salary) GROUP BY department_id HAVING MAX(salary)>10000 ; Display the maximum average salary: SELECT MAX(AVG(salary)) GROUP BY department_id; 4-21 Copyright 2004, Oracle. All rights reserved. Using the HAVING Clause 4-23 Copyright 2004, Oracle. All rights reserved. Summary SELECT job_id, SUM(salary) PAYROLL WHERE job_id NOT LIKE '%REP%' GROUP BY job_id HAVING SUM(salary) > 13000 ORDER BY SUM(salary); In this lesson, you should have learned how to: Use the group functions COUNT, MAX, MIN, and AVG Write queries that use the GROUP BY clause Write queries that use the HAVING clause SELECT column, group_function [GROUP BY group_by_expression] [HAVING group_condition] 4-22 Copyright 2004, Oracle. All rights reserved. 4-24 Copyright 2004, Oracle. All rights reserved.

Practice 4 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 prior to inclusion in a group calculation. True/False Practice 4 (continued) 6. Write a query to display the number of people with the same job. The HR department needs the following reports: 4. Find the highest, lowest, sum, and average salary of all employees. Label the columns Maximum, Minimum, Sum, and Average, respectively. Round your results to the nearest whole number. Place your SQL statement in a text file named lab_04_04.sql. 5. Modify the query in lab_04_04.sql to display the minimum, maximum, sum, and average salary for each job type. Resave lab_04_04.sql as lab_04_05.sql. Run the statement in lab_04_05.sql. 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_04_06.sql. 7. Determine the number of managers without listing them. Label the column Number of Managers. Hint: Use the MANAGER_ID column to determine the number of managers. 8. Find the difference between the highest and lowest salaries. Label the column DIFFERENCE. 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. Oracle Database 10g: SQL Fundamentals I 4-26 Oracle Database 10g: SQL Fundamentals I 4-27