0% found this document useful (0 votes)
45 views

Primer On Panel Data Analysis

Group functions allow you to perform calculations on groups of aggregated data rather than individual rows. The main group functions are COUNT, SUM, AVG, MIN, MAX. You use the GROUP BY clause to group data and the HAVING clause to filter groups. For example, you can get the average salary for each department by grouping rows by department and using the AVG function.

Uploaded by

jcgutz3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Primer On Panel Data Analysis

Group functions allow you to perform calculations on groups of aggregated data rather than individual rows. The main group functions are COUNT, SUM, AVG, MIN, MAX. You use the GROUP BY clause to group data and the HAVING clause to filter groups. For example, you can get the average salary for each department by grouping rows by department and using the AVG function.

Uploaded by

jcgutz3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Group Functions

ICST211 Database Management Systems 1


Objectives
y Identify the available group functions.
y Describe the use of group functions.
y Group data using the GROUP BY clause.
y Include or exclude grouped rows by using the HAVING
clause.
What are Group Functions?
Types of Group Functions
Group Function SYNTAX
Using AVG and SUM functions
y You use AVG and SUM for numeric data.

y The given example displays the average, highest, lowest and


sum of monthly salaries for all sales representative.
Using MIN and MAX Functions
y You can use MIN and MAX for any data type.

y The given example displays the most junior and the most
senior employee.
Using the COUNT function
y COUNT(*) returns the number of rows in a table.

y The given example count the number of employees in


department 50.
Using the COUNT function
y COUNT(expr) returns the number of rows with non-null
values for the expr.

y The given example display the number of employees in


department 80 who can earn a commission.
Using the DISTINCT keyword in COUNT
function
y COUNT(DISTINCT expr) returns the number of distinct
nonnull values of the expr.

y The given example display the number of distinct


departments in the employees table.
Group Functions and Null Values
• Group functions ignore null values in the column.

• In the given example, the average is calculated based only on


the rows in the table where a valid value is stored in the
COMMISSION_PCT column.
Creating Groups of Data
Using the GROUP BY clause
y Divide rows in a table into smaller groups.

y In the syntax:
y group_by_expression specifies columns whose values determine
the basis for grouping rows.
Using the GROUP BY clause
• All columns in the SELECT clause that are not in group
functions must be in the GROUP BY clause.

• The given example displays the department number, and the


average salary for each department.
Using the GROUP BY clause
y The Group by column does not have to be in the SELECT
clause.
Grouping by more than one column
Using Group by clause on multiple
columns
y You can return summary results for groups and subgroups by
listing more than one group by column.
Are the queries correct?
Query #1:
SELECT department_id, count(last_name)
FROM employees;

Query #2:
SELECT department_id, AVG(salary)
FROM employees
WHERE AVG(salary) > 8000
GROUP BY department_id;
Excluding Group Results
Using the HAVING clause
1. Rows are grouped.
2. The group function is applied
3. Groups matching the HAVING clause are displayed.
Using the HAVING clause

• The given example displays department numbers and


maximum salaries for those departments whose maximum
salary is greater than 10000.
Using the HAVING clause
y The given example displays the job ID and total monthly for
each job with a total payroll exceeding 13,000, excluding
sales representatives. The list is sorted by the total monthly
payroll.
Nesting Group Functions
y Display the maximum average salary.

You might also like