What is an aggregate function?
An aggregate function processes values from a set of rows (group) 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?
SELECT clause to summarize data from rows that meet the WHERE clause criteria.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?
GROUP BY clause organizes data into groupsprovides insightful analysis of the data; magnifying it
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)
NULL when either operand is NULL.NULL values & may generate surprising results when NULL 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.
It’s the only way to filter results based on aggregate functions in SQL
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.
Refer to the Auto Table
What value returns from the SELECT statement?
SELECT COUNT(*) FROM Auto WHERE Price < 10000;
Result:
COUNT(*) 2
Two rows have Price < 10000
What was the likely code used in the GROUP clause?
SELECT CountryCode, SUM(Population) FROM City GROUP BY CountryCode;
What was the likely code used in the GROUP clause?
SELECT CountryCode, District, COUNT(*) FROM City GROUP BY CountryCode, District;
What was the likely code used in the HAVING clause? With 2300000.
SELECT CountryCode, SUM(Population) FROM City GROUP BY CountryCode HAVING SUM(Population) > 2300000;
What was the likely code used in the HAVING clause?
With >= 2
SELECT CountryCode, District, COUNT(*) FROM City GROUP BY CountryCode, District HAVING COUNT(*) >= 2;
Refer to the Auto table | HAVING Clause
What would be the result of the statement below?
SELECT Make, COUNT(Make) FROM Auto GROUP BY Make HAVING COUNT(Make) > 1;
GROUP BY Make ensures each auto make is counted separately.HAVING COUNT(Make) > 1 eliminates Ford and Volkswagen, which appear in only one row each.Refer to the Auto table | HAVING Clause
What would be the result of the statement below?
SELECT Year, Type, MAX(Price) FROM Auto GROUP BY Year, Type HAVING MAX(Price) > 15000 ORDER BY Year, MAX(Price);
HAVING clause excludes the groups (2014, compact, 8800) and (2016, sedan, 10200).Refer to the image
What common functions are these?
Numeric functions operates on, and evaluates to, specific data type
Refer to the image
What common functions are these?
String functions manipulate string values.