3.3 Aggregate functions Flashcards
What is an aggregate function?
An aggregate function processes values from a set of rows and returns a summary value, such as COUNT(), MIN(), MAX(), SUM(), and AVG().
Aggregate functions: SUM(Salary)
| Arithmetic: SUM(Salary + Bonus)
How does a SELECT
statement use aggregate functions?
Aggregate functions are included in the SELECT
clause to summarize data from rows that meet the WHERE
clause criteria. If no WHERE
clause exists, they summarize all rows.
What does the COUNT()
function do?
The COUNT()
function counts the number of rows in a specified set.
When is the GROUP BY
clause used in SQL?
The GROUP BY
clause is used to group rows that have the same values in specified columns, allowing SQL to perform aggregate functions on each group separately.
Can you explain the importance of having the GROUP BY
clause in a query?
The GROUP BY
clause organizes data into groups, allowing aggregate functions to calculate summary values for each group, providing insightful analysis of the data.
How does the HAVING
clause work with GROUP BY
?
The HAVING
clause filters the results of a GROUP BY
operation based on a specified condition, allowing for more refined data analysis.
What happens if NULL
values are present in aggregate functions?
Aggregate functions ignore NULL
values, which can lead to unexpected results when arithmetic operations are applied to aggregate values that include NULLs
.
Aggregate functions: SUM(Salary)
| Arithmetic: SUM(Salary + Bonus)
What is the difference between SUM(Salary) + SUM(Bonus)
and SUM(Salary + Bonus)
?
Aggregate functions: SUM(Salary)
| Arithmetic: SUM(Salary + Bonus)
- Arithmetic operators return
NULL
when either operand isNULL
. - Aggregate functions ignore
NULL
values & may generate surprising results whenNULL
is present.
Aggregate functions: SUM(Salary)
| Arithmetic: SUM(Salary + Bonus)
Write a SQL query using the aggregate function AVG()
to find the average salary from a table called Compensation.
SELECT AVG(Salary) FROM Compensation;
How can you filter results based on aggregate functions?
You can use the HAVING
clause to filter groups formed by the GROUP BY
clause based on the results of aggregate functions.
Refer to the Employee
Table
What would the aggregate functions in the SELECT statement result in?
~~~
SELECT AVG(Salary)
FROM Employee;
~~~
Result:
AVG(Salary) 57333.333333
Refer to the Employee
Table
What would the aggregate functions in the SELECT statement result in?
~~~
SELECT MIN(Salary)
FROM Employee;
~~~
Result:
MIN(SALARY) 32000
Refer to the Employee
Table
What would the aggregate functions in the SELECT statement result in?
~~~
SELECT COUNT(*)
FROM Employee
WHERE Bonus > 500;
~~~
Result:
COUNT(*) 2
Refer to the Auto Table
What value returns from the SELECT
statement?
SELECT MIN(Year) FROM Auto;
Result:
Min(Year) 2014
MIN(Year) finds the minimum (lowest) year.
Refer to the Auto Table
What value returns from the SELECT
statement?
SELECT SUM(Price) FROM Auto;
Result:
SUM(Price) 92300
The sum of the price is selected.