GROUP BY Flashcards

1
Q

SELECT customer_id, SUM(amount) FROM payment
GROUP BY customer_id
ORDER BY SUM(amount) DESC;

A

Answers the questions of how much each customer is spending and puts them in descending order by total spent

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

SELECT customer_id, SUM(amount), COUNT(rental_id) FROM payment
GROUP BY customer_id
ORDER BY SUM(amount) DESC;

A

similiar to previous query, amount spent by customer but also provides # of transactions per customer

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

SELECT DATE(payment_date), SUM(amount) FROM payment
GROUP BY DATE(payment_date)
ORDER BY DATE(payment_date);

A

Provides a list of total sales by day in descending order of day. One note is the DATE function takes a timestamp column and extracts just the day of the month and drops the time

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

SELECT customer_id, SUM(amount) FROM payment
GROUP BY customer_id
ORDER BY SUM(amount) DESC
LIMIT 5;

A

Gives the top 5 customer ids per amount spent

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
SELECT ROUND(AVG(replacement_cost),2), rating FROM film
GROUP BY rating;
A

provides an average replacement cost based off of rating of film

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

SELECT staff_id, COUNT(payment_id) FROM payment
GROUP BY staff_id
ORDER BY COUNT(payment_id) DESC;

A

used to determine which Staff ID has performed the greatest # of transactions

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

HAVING clause

A

allows a filtering to take place AFTER an aggregation has taken place. HAVING is generally a WHERE statement but only used for GROUP BY

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
SELECT SUM(amount), customer_id FROM payment
GROUP BY customer_id
HAVING SUM(amount) > 100;
A

List of customer ids who have spent more than $100. Need to use HAVING instead of WHERE because filtering based off of an aggregate function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
SELECT COUNT(customer_id), store_id FROM customer
GROUP BY store_id
HAVING COUNT(customer_id) > 300;
A

providing a list of store_ids with more than 300 customers

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

SELECT customer_id, COUNT(payment_id) FROM payment
GROUP BY customer_id
HAVING COUNT(payment_id) >= 40;

A

creates a list of customers that have had more than 40 transactions

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

SELECT customer_id, SUM(amount) FROM payment
WHERE staff_id = 2
GROUP BY customer_id
HAVING SUM(amount) > 100;

A

Provides customer_id and sum of amount spent with staff_id number 2. Where statement comes before the having and is not part of aggregate function

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

SELECT customer_id, SUM(amount) FROM payment
WHERE staff_id = 2
GROUP BY customer_id
HAVING SUM(amount) >= 110;

A

provides a list of customers spending more than 110, where purchased were made with staff_id number 2

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