PostgreSQL CASE, COALESCE and NULL Handling: Practical SQL Guide
In PostgreSQL, CASE , COALESCE , and NULL-handling functions help you manage missing values, create conditional results, categorize data, and build meaningful business reports. These concepts are useful for data analysts, SQL developers, reporting, and technical interviews. What Is CASE in PostgreSQL? The CASE expression works like an IF-THEN-ELSE statement. It checks conditions and returns a result based on the first matching condition. Syntax: SELECT column_name, CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 ELSE default_result END AS result_column FROM table_name; 1. Categorize Employees Based on Salary Consider an employees table with these columns: employee_id , employee_name , department , and salary . SELECT employee_name, salary, CASE WHEN salary >= 70000 THEN 'High Salary' WHEN salary >= 50000 THEN 'Medium Salary' ELSE 'Low Salary' END AS sala...