Structured Query Language. Group Functions What are group functions ? Group Functions Group functions operate on sets of rows to give one result per group.

Slides:



Advertisements
Similar presentations
Copyright  Oracle Corporation, All rights reserved. 4 Aggregating Data Using Group Functions.
Advertisements

Basic Queries. 2 Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not the.
SQL Query Slides Sharif University Of Technology Database Systems CE 384 Prepared By: Babak Bagheri Hariri
Your Logo Fundamentals of Database Systems Fourth Edition El Masri & Navathe Instructor: Mr. Ahmed Al Astal Chapter 8 (Cont.) SQL-99: Schema Definition,
4c. Structured Query Language - Built-in Functions Lingma Acheson Department of Computer and Information Science IUPUI CSCI N207 Data Analysis with Spreadsheets.
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.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Chapter 6 Set 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.
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.
Data Warehousing/Mining 1 Data Warehousing/Mining Comp 150 Aggregation in SQL (not in book) Instructor: Dan Hebert.
4 การใช้ SQL Functions. Copyright © 2007, Oracle. All rights reserved What Are Group Functions? Group functions operate on sets of rows to give.
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.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 7: Aggregates.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 7: Aggregates.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
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.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
Information Resource Engineering SQL4. Recap - Ordering Output  Usually, the order of rows returned in a query result is undefined.  The ORDER BY clause.
SQL-5 (Group By.. Having). Group By  Need: To apply the aggregate functions to subgroups of tuples in a relation, where the subgroups are based on some.
Chapter 7 SQL HUANG XUEHUA. AGGREGATE FUNCTIONS Include COUNT, SUM, MAX, MIN, and AVG Query 15: Find the maximum salary, the minimum salary, and the.
Basic Group Functions (without GROUP BY clause) Week 5 – Chapter 5.
1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)
1 Querying a Single Table Structured Query Language (SQL) - Part II.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
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.
Aggregating Data Using Group Functions. What Are Group Functions? Group functions operate on sets of rows to give one result per group.
SQL Aggregeringsfunktioner. AGGREGATE FUNCTIONS Include COUNT, SUM, MAX, MIN, and AVG Query 15: Find the maximum salary, the minimum salary, and the average.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
5-1 Copyright © 2004, Oracle. All rights reserved. DISPLAYING DATA FROM MULTIPLE TABLES OUTER JOIN.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Reporting Aggregated Data Using the Group Functions
CS580 Advanced Database Topics
Aggregating Data Using Group Functions
Enhanced Guide to Oracle 10g
SQL AGGREGATE FUNCTIONS
Chapter # 6 The Relational Algebra and Calculus
6/22/2018.
Chapter 3 Introduction to SQL(3)
Using Relational Databases and SQL
Group Functions Lab 6.
Working with Tables: Join, Functions and Grouping
Aggregating Data Using Group Functions
(SQL) Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
SQL – Entire Select.
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Reporting Aggregated Data Using the Group Functions
Query Functions.
Reporting Aggregated Data Using the Group Functions
Reporting Aggregated Data Using the Group Functions
Aggregate functions Objective- understand and able to use aggregate functions in select statement Aggregate functions are used to implement calculation.
SQL Grouping, Ordering & Arithmetics Presented by: Dr. Samir Tartir
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Database Programming Using Oracle 11g
Aggregating Data Using Group Functions
Presentation transcript:

Structured Query Language

Group Functions What are group functions ? Group Functions Group functions operate on sets of rows to give one result per group. These sets may be the whole table or the table split into groups.

What are Group Functions? DNO SALARY MAX(SAL) “maximum Salary in The Employee table”

Types of Group Functions AVG COUNT MAX MIN SUM

Group Functions The avg(fieldname) function returns the average value of a numeric column’s returned values.  The min(fieldname) and max(fieldname) functions return minimum and maximum numeric value of a column respectively. The count(fieldname) returns an integer representing the count of the number of rows retrieved where value of the fieldname is not null.. The count(*) returns count of all rows, including the null values.  The sum(fieldname) returns sum of a numeric column.

Query 1 Find the maximum salary, the minimum salary, and the average salary among all employees. SELECT MAX(SALARY), MIN(SALARY), AVG(SALARY) FROMEMPLOYEE

Query 2 Retrieve the total number of employees in the company SELECT COUNT (*) FROM EMPLOYEE

Groups of data Until now, all group functions have treated the table as one large group of information. At times, you need to divide the table of information into smaller groups. This can be done by using the GROUP BY clause.

GROUPING In many cases, we want to apply the group functions to subgroups of rows in a table Each subgroup of rows consists of the set of tuples that have the same value for the grouping attribute(s) The function is applied to each subgroup independently SQL has a GROUP BY-clause for specifying the grouping attributes, which must also appear in the SELECT-clause

Query 3  For each department, retrieve the department number, the number of employees in the department, and their average salary SELECT DNO, COUNT (*), AVG (SALARY) FROM EMPLOYEE GROUP BY DNO

Aggregate Functions In the previous query, the EMPLOYEE table rows are divided into groups, each group having the same value for the grouping attribute DNO The COUNT and AVG functions are applied to each such group of rows separately The SELECT-clause includes only the grouping attribute and the functions to be applied on each group

Excluding Group Results Sometimes we want to retrieve the values of the group functions for only those groups that satisfy certain conditions The HAVING-clause is used for specifying a selection condition on groups (rather than on individual rows) o The having clause is designed for use with the group by clause to restrict the groups that appear. This is very similar to the where clause, except that the where clause filters individual rows, whereas the having clause filters groups.

Excluding Group Results EMP DEPTNO SAL DEPTNO MAX(SAL) “maximum salary per department greater than $2900”

Query 4 Display the department numbers, and maximum salary for those departments whose maximum salary is greater than $ SELECT DNO, MAX(SALARY) FROM EMPLOYEE GROUP BY DNO HAVING MAX(SALARY) > 40500

Query 5 o Display the number of employees, and the average salaries for each department that have more than one employee working : Select dno, count(ssn), avg(salary) From employee Group by dno Having count(ssn) > 1;

Query 6 o Count the number of distinct salary values in the employee table SELECT COUNT(DISTINCT SALARY) FROM EMPLOYEE

Query 7 o Retrieve the department number that has more than one location SELECT dnumber FROM dept_locations GROUP BY dnumber HAVING count(dnumber)>2

Query 8 o Write a query that will display the difference between the highest salary and lowest salary. Label the column DIFFERENCE SELECT max(salary)-min(salary) DIFFERENCE FROM employee