Aggregation
Aggregation
The GROUP BY statement groups rows in different categories where the aggregation functions can be applied on the rows
of a category independently. For example, you can find the number of employees in each department using the GROUP BY
statement on the department.
The HAVING clause is used to filter grouped data using conditions calculated with the aggregate functions. It is used along
with the GROUP BY statement.
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s);
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition;
One thing to keep in mind when using aggregates in SQL is that any column in the SELECT list must be either an aggregate
or be listed in the GROUP BY clause.
The following query will return the number of employees in each department.
Table 2-46
department total
Finance 2
HR 2
S l 1
Sales 1
The following query will return the number of employees in each department where the number of employees in the
department is more than 1.
SELECT
department,
COUNT(*) as total
FROM employee2 GROUP BY department HAVING count(*)>1;
Table 2-47
department total
Finance 2
HR 2
The following query will return the total number of employees as well as the minimum, maximum, and average salary of
the employees in each department.
SELECT
department,
COUNT(*) as total_employee,
MIN(salary) as min_salary,
MAX(salary) as max_salary,
AVG(salary) as average_salary
FROM Employee2 GROUP BY department;
Table 2-48
Watch the following videos by Ben Forta to learn about aggregation in SQL.
Videos:
and HAVING is just like WHERE,
but unlike the WHERE clause,
which functions at the row level,
HAVING functions at the group level,
which means once again
we should revisit the select statement.
So now SELECT column, FROM table,
WHERE and your condition,
GROUP BY and the columns you group
them by
Previous Next
Pivoting
Bookmark this page
Consider there is a large dataset containing the employees records stored in a table and you want to find the number of
employees in each department. In the aggregate data example above, Table 2-46 shows the number of employees in each
department is the summary information calculated from the employee table Table 2-44. This is called pivoting.
In data analytics, pivoting is a process used to summarize data in a large dataset stored in a table so that an analyst can
see trends in the data.
Excel provides a powerful tool called PivotTable that can be used to calculate and summarize data for analysis. The
following are the steps to create a pivot table in Excel.
Figure 2-17
3. A new worksheet will be opened that allows you to select the pivot fields. Here, we have selected the department and
salary field. By default, it is showing the sum of the salary of each department.
Figure 2-18
4. By default, the calculation is the sum, but it can be changed to the count, min, or max.
Copyright © 2024 Pearson Education Inc. or its affiliate(s). All rights reserved.
Choose your calculation (Sum, Count, Average, Max, Min, Product) from the Value Field Settings dialog box and click
OK
Figure 2-20
Previous Next
Previous Next
Aggregate functions
Bookmark this page
An aggregate function is a function that performs a calculation on multiple values and returns a single value. Some of the
most common and frequently used aggregation functions are as follows:
Table 2-43
NULL values are ignored when performing most aggregate functions. However, NULL values in the COUNT function are not
ignored.
Previous Next
Hide Notes
Previous Next
An aggregate function in SQL returns a single value that is calculated from the multiple values in a column. The following
are some of the frequently used aggregation functions in SQL:
COUNT()
SUM()
MIN()
MAX()
AVG()
The COUNT() function returns the number of records that satisfy a specific condition.
The AVG() function returns the average of all the values in a column.
Consider the table EMPLOYEE2 below that has five columns - id, name, age, salary, and department.
Table 2-44
Copyright © 2024 Pearson Education Inc. or its affiliate(s). All rights reserved.
SELECT
count(*) as total_employee,
sum(salary) as total_salary,
min(salary) as min_salary,
max(salary) as max_salary,
avg(salary) as average_salary
FROM employee2;
Table 2-45
Previous Next