Let's talk about the AVG function in SQL, which is calculating the average value of a bunch of numbers.
AVG works on numbers, so you can use it to find out things like the average price of items sold on Amazon or the average number of likes on an Instagram post.
Imagine you're looking at data from Instagram and you want to know what the average likes are for posts in the last month.
instagram_posts table sample:
| post_id | user_id | likes | post_date |
|---|---|---|---|
| 1 | 1001 | 120 | 2021-01-01 |
| 2 | 1002 | 200 | 2021-01-02 |
| 3 | 1003 | NULL | 2021-01-03 |
| ... | ... | ... | ... |
To get the average likes for January 2021, you'd use:
SELECT AVG(likes)
FROM instagram_posts
WHERE post_date BETWEEN '2021-01-01' AND '2021-01-31';
Here is the caveat of AVG: It skips any posts that don't have likes recorded (i.e. if the likes column is NULL).
When you use AVG, it ignores any missing values (NULLs). So, if a post has no likes and is recorded as NULL, AVG just skips it. In some cases, though, you might want to treat missing values as zeros. That's a bit more advanced and involves replacing NULLs with zeros before calculating the average, but we'll get to that in a future lesson.
In short, AVG is super useful for getting a general idea of numbers, like prices, scores, likes, etc., especially when you want to smooth out the extremes and get a sense of the typical or middle ground.
Now let's practice 🎉!