I am working through https://www.udemy.com/course/the-complete-sql-bootcamp/. These are my notes. So far I recommend it.
Aggregate functions: Take many inputs, and return 1 output. Only work in the SELECT or HAVING clause
Aggregate Key Words
AVG: returns average value (Returns Float, you can use ROUND() to specify precision after the decimal)
Average cost of the cars to 2 decimal places: SELECT ROUND(AVG(price), 2) FROM vehicle
COUNT: see SQL Notes 1
MAX: returns max value
MIN: returns min value
SUM: returns the sum of all values
GROUP BY keyword
Must be right after a FROM or WHERE statment.
Any SELECTed columns must appear in the GROUP BY statement (Doesn’t include things like min/max/avg). It won’t throw an error if you don’t, but the data won’t be right
So your table as a categorical column: perhaps SUV, or apple type. The idea is you split up the table by Category value. So you can find the average value of items in Cat A, B, and C
SELECT category, AVG(value) FROM table GROUP BY category;
SELECT category, AVG(value) FROM table WHERE category !=’A’ GROUP BY category;
category | value |
A | 1 |
A | 2 |
B | 20 |
B | 15 |
C | 6 |
C | 9 |
For the demo DB given you can see which customer paid the most, how many interactions they did, and the amount of money you got from them.
select customer_id, sum(amount), count(amount) from payment group by customer_id order by sum(amount) desc;
HAVING
The HAVING keyword lets you filter after an aggregation has already happened