SQL Tutorial – Aggregates columns which has NULL values.

You want to perform an aggregation on a column but this column contains some Null values. And as we know Aggregation functions ignores the NULL values when doing calculations. In the below table you can see that we have one null values and our goal is to include some this null value in our aggregation.

Solution –

To include NULL values in aggregations, we can use the COALESCE function which converts NULL values to zero.

    AVG(COALESCE(christmas_spending, 0)) as avg_spending

Rating: 1 out of 5.

Leave a Reply