ComeçarComece de graça

Fixing calculations with coalesce

Null values impact aggregations in a number of ways. One issue is related to the AVG() function. By default, the AVG() function does not take into account any null values. However, there may be times when you want to include these null values in the calculation as zeros.

To replace null values with a string or a number, use the COALESCE() function. Syntax is COALESCE(fieldName,replacement), where replacement is what should replace all null instances of fieldName.

This exercise will walk you through why null values can throw off calculations and how to troubleshoot these issues.

Este exercício faz parte do curso

Reporting in SQL

Ver curso

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

-- Pull events and golds by athlete_id for summer events
SELECT 
    ____,
    ____ AS total_events, 
    ____ AS gold_medals
FROM ____
GROUP BY ____
-- Order by total_events descending and athlete_id ascending
ORDER BY ____;
Editar e executar o código