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

Instruction

Good. Remember: if all arguments are NULL, you will get NULL as the average. Makes sense.

Of course, you can count NULLs as zero, too. Here is how. Remember, the function COALESCE(x,y)? We can use it to replace x with y if x is NULL:

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

The above query will find the average price for all projects. If a project has a NULL price, it will indeed be treated as a project with the price 0.

Exercise

Find the average number of words per project translated so far by Adriana Fuentes (translator_id = 5). Assume that NULL in column words means that nothing has been translated yet (treat it as words = 0). Name the result average.

Stuck? Here's a hint!

Use AVG(COALESCE(words, 0)).