Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Function AVG
22. COALESCE with AVG
Functions SUM, MAX, MIN
Review

Instruction

Okay! That was AVG(COALESCE(x,0)). Now, you may wonder what happens when we swap the functions:

SELECT COALESCE(AVG(price),0)
FROM project;

Will the result be the same? No, it won't.

This query will compute the average for all the projects ignoring NULL values. In case all the values are NULL and so the average is NULL also, the COALESCE function will replace the NULL average with a zero.

Exercise

Let's check that in practice. Change the query from the previous exercise. Swap the functions AVG() and COALESCE(). Note that the result is different now.