SQL Practice Logo

SQLPractice Online

NULL Handling in Aggregate Functions: Real-World

Module: Aggregate Functions & Grouping

NULL values in aggregates are everywhere and dangerous. Missing employee bonuses make average salary calculations wrong. NULL discount codes break revenue reports. Incomplete survey responses skew analytics. Understanding NULL behavior prevents silent errors.

HR Compensation Analysis

Employee performance ratings are NULL for new hires. Bonus amounts are NULL for ineligible employees.

SELECT

department,

COUNT(*) AS total_employees,

COUNT(bonus) AS bonus_eligible,

AVG(bonus) AS avg_bonus_among_eligible,

AVG(COALESCE(bonus, 0)) AS avg_bonus_cost_per_employee

FROM employees

GROUP BY department;

Prevents HR from making decisions based on incomplete data. Clear distinction between eligible employees vs total cost.

All