0% found this document useful (0 votes)
4 views27 pages

Day 3 - Course Slides

The document outlines the use of aggregation functions in SQL to summarize data across multiple rows, including functions like SUM, AVG, MIN, MAX, and COUNT. It provides syntax examples for querying payment data and emphasizes the importance of grouping and filtering results using GROUP BY and HAVING clauses. Additionally, it presents scenarios for writing SQL queries to analyze employee performance and film costs based on specific criteria.
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)
4 views27 pages

Day 3 - Course Slides

The document outlines the use of aggregation functions in SQL to summarize data across multiple rows, including functions like SUM, AVG, MIN, MAX, and COUNT. It provides syntax examples for querying payment data and emphasizes the importance of grouping and filtering results using GROUP BY and HAVING clauses. Additionally, it presents scenarios for writing SQL queries to analyze employee performance and film costs based on specific criteria.
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/ 27

Day 3

Aggregation functions

✓ Aggregate values in multiple rows to one value

SUM
Aggregation functions

✓ Aggregate values in multiple rows to one value

AVG
Most common
aggregation functions

SUM()
AVG()
MIN()
MAX()
COUNT()
SYNTAX

SELECT
SUM(amount)
FROM payment
SYNTAX

SELECT
COUNT(*)
FROM payment
What we can't do…

SELECT
SUM(amount)
payment_id
FROM payment
No mixing possible!

SELECT
SUM(amount)
Only possible with
payment_id grouping!
FROM payment
Multiple aggregations is possible!

SELECT
SUM(amount),
COUNT(*),
AVG(amount)
FROM payment
Your manager wants to which of the two employees (staff_id)
is responsible for more payments?

Which of the two is responsible for a higher overall payment


amount?

How do these amounts change if we don't consider amounts


equal to 0?
Write two SQL queries to get the answers!

Result
Solution

SELECT
MIN(replacement_cost),
MAX(replacement_cost),
ROUND(AVG(replacement_cost),2) AS AVG,
SUM(replacement_cost)
FROM film
GROUP BY

✓ Used to GROUP aggregations BY specific columns

SUM
SYNTAX

SELECT
customer_id,
SUM(amount)
FROM payment
GROUP BY customer_id
SYNTAX
SELECT
customer_id,
SUM(amount)
FROM payment
WHERE customer_id >3
GROUP BY customer_id
SYNTAX
SELECT
customer_id,
SUM(amount)
FROM payment
WHERE customer_id >3
GROUP BY customer_id
ORDER BY customer_id
SYNTAX

SELECT
customer_id, Every column:
In GROUP BY or
SUM(amount) in aggregate functions
FROM payment
GROUP BY customer_id
SYNTAX

SELECT
customer_id, Every column:
In GROUP BY or
SUM(amount) in aggregate functions
FROM payment
GROUP BY customer_id
There are two competitions between the two employees.

Which employee had the highest sales amount in a single day?

Which employee had the most sales in a single day (not


counting payments with amount = 0?

Write two SQL queries to get the answers!

Result
Your manager wants to get a better understanding of the
films.
That's why you are asked to write a query to see the
• Minimum
• Maximum
• Average (rounded)
• Sum
of the replacement cost of the films.
Write a SQL query to get the answers!

Result
HAVING

✓ Used to FILTER Groupings BY aggregations

HAVING
COUNT(*)>400

Note!
HAVING an only be used
with GROUP BY!
SYNTAX
SELECT
customer_id,
SUM(amount)
FROM payment
GROUP BY customer_id
HAVING SUM(amount)>200
Solution

SELECT
MIN(replacement_cost),
MAX(replacement_cost),
ROUND(AVG(replacement_cost),2) AS AVG,
SUM(replacement_cost)
FROM film
In 2020, April 28, 29 and 30 were days with very high revenue.
That's why we want to focus in this task only on these days
(filter accordingly).
Find out what is the average payment amount grouped by
customer and day – consider only the days/customers with
more than 1 payment (per customer and day).
Order by the average amount in a descending order.

Write a SQL query to find out!

Result

You might also like