3.3 Aggregate functions Flashcards

1
Q

What is an aggregate function?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How does a SELECT statement use aggregate functions?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does the COUNT() function do?

A

The COUNT() function counts the number of rows in a specified set.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When is the GROUP BY clause used in SQL?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Can you explain the importance of having the GROUP BY clause in a query?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How does the HAVING clause work with GROUP BY?

A

The HAVING clause filters the results of a GROUP BY operation based on a specified condition, allowing for more refined data analysis.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What happens if NULL values are present in aggregate functions?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the difference between SUM(Salary) + SUM(Bonus) and SUM(Salary + Bonus)?

Aggregate functions: SUM(Salary) | Arithmetic: SUM(Salary + Bonus)

A
  1. Arithmetic operators return NULL when either operand is NULL.
  2. Aggregate functions ignore NULL values & may generate surprising results when NULL is present.

Aggregate functions: SUM(Salary) | Arithmetic: SUM(Salary + Bonus)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Write a SQL query using the aggregate function AVG() to find the average salary from a table called Compensation.

A

SELECT AVG(Salary) FROM Compensation;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How can you filter results based on aggregate functions?

A

You can use the HAVING clause to filter groups formed by the GROUP BY clause based on the results of aggregate functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Refer to the Employee Table

What would the aggregate functions in the SELECT statement result in?
~~~
SELECT AVG(Salary)
FROM Employee;
~~~

A

Result:

AVG(Salary)
57333.333333
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Refer to the Employee Table

What would the aggregate functions in the SELECT statement result in?
~~~
SELECT MIN(Salary)
FROM Employee;
~~~

A

Result:

MIN(SALARY)
32000
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Refer to the Employee Table

What would the aggregate functions in the SELECT statement result in?
~~~
SELECT COUNT(*)
FROM Employee
WHERE Bonus > 500;
~~~

A

Result:

COUNT(*)
2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Refer to the Auto Table

What value returns from the SELECT statement?

SELECT MIN(Year)
FROM Auto;
A

Result:

Min(Year)
2014

MIN(Year) finds the minimum (lowest) year.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Refer to the Auto Table

What value returns from the SELECT statement?

SELECT SUM(Price) 
FROM Auto;
A

Result:

SUM(Price)
92300

The sum of the price is selected.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Refer to the Auto Table

What value returns from the SELECT statement?

SELECT COUNT(*) 
FROM Auto 
WHERE Price < 10000;
A

Result:

COUNT(*)
2

Two rows have Price < 10000

17
Q

Refer to the City Table

What value returns from the SELECT statement?


A

Result:


18
Q

What was the likely code used in the GROUP clause?

A
SELECT CountryCode, SUM(Population)
FROM City
GROUP BY CountryCode;
19
Q

What was the likely code used in the GROUP clause?

A
SELECT CountryCode, District, COUNT(*)
FROM City
GROUP BY CountryCode, District;
20
Q

What was the likely code used in the HAVING clause?

A
SELECT CountryCode, SUM(Population)
FROM City
GROUP BY CountryCode
HAVING SUM(Population) > 2300000;
21
Q

What was the likely code used in the HAVING clause?

A
SELECT CountryCode, District, COUNT(*)
FROM City
GROUP BY CountryCode, District
HAVING COUNT(*) >= 2;
22
Q

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;
A
  1. GROUP BY Make ensures each auto make is counted separately.
  2. HAVING COUNT(Make) > 1 eliminates Ford and Volkswagen, which appear in only one row each.
23
Q

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);
A
  1. The results show the max price in each year and type group, but only for groups having a max price > 15,000.
  2. The HAVING clause excludes the groups (2014, compact, 8800) and (2016, sedan, 10200).