SQL NULLIF Usage
SQL NULLIF Usage is a SQL function in the SELECT Queries category. NULLIF compares two expressions. If equal, returns NULL. Otherwise returns the first expression. The syntax is NULLIF(expr1, expr2) — returns NULL if equal, else expr1.. It accepts expr1, expr2. It returns nULL or expr1. A typical example: SELECT NULLIF(10, 10); -- NULL (they are equal) SELECT NULLIF(10, 5); -- 10 (different) -- Prevent division by zero: SELECT 100 / NULLIF(quantity, 0) AS price_per_unit FROM products; -- If quantity = 0, NULLIF returns NULL, division = NULL (no error) SELECT department, AVG(NULLIF(salary, 0)) AS avg_salary FROM employees GROUP BY department; -- Excludes salaries of 0 from average calculation -- Clear previous values: UPDATE employees SET salary = NULLIF(salary,… A close relative is SELECT, which retrieves data from a table. The most fundamental SQL statement. A close relative is SELECT *, which selects all columns from a table.