sql sum show 0 result 1

sql sum show 0 result

# if WHERE... finds no rows, then sum(grade) == null, so returns an empty table
# Using COALESCE, if null, then values = 0, returns 1 row with value == 0
SELECT COALESCE(SUM(grade), 0) as sum FROM grades
WHERE project_title = 'Recipe Storage';

Here is what the above code is Doing:
1. The WHERE clause is used to filter the rows of the table.
2. The SUM function is used to sum the values in the grade column.
3. The COALESCE function is used to return 0 if the sum is null.

Similar Posts