SQL Practice Logo

SQLPractice Online

NULL Handling in Aggregate Functions: Interview

Module: Aggregate Functions & Grouping

What is the difference between COUNT(*) and COUNT(column) when dealing with NULL?

COUNT(*) counts all rows regardless of NULL values. COUNT(column) counts only rows where that column has a non-NULL value.

How do SUM, AVG, MIN, and MAX handle NULL values?

These aggregates completely ignore NULL values. AVG does NOT treat NULL as zero - it calculates average of only non-NULL values.

Write a query showing difference between average bonus per recipient vs per employee.

SELECT

COUNT(*) AS total_employees,

COUNT(bonus) AS bonus_recipients,

AVG(bonus) AS avg_bonus_per_recipient,

AVG(COALESCE(bonus, 0)) AS avg_bonus_per_employee

FROM employees;

AVG(bonus) ignores NULL and shows average among recipients. AVG(COALESCE(bonus, 0)) treats NULL as zero for average across all employees.