Introduction
Function AVG
Functions SUM, MAX, MIN
28. SUM and COALESCE
Revision

Instruction

Fine. Again, if we don't like to get NULL as a result, we can use COALESCE to replace it with any value of our choice:

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

If all prices happen to be NULLs, the above query will return 0 and not a NULL;

Note that this time you can also write:

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

and the result will stay the same.

Exercise

Show translator ids with the sum of prices all the projects they have done. Exclude project_id = 13 so that one of the translators has NULL prices only. Instead of that NULL sum, show 0.

Stuck? Here's a hint!

Group the rows by the translator id and use COALESCE.

Console

Code editor

Result

TableConsole