πŸŽ‰ 75% of content is free forever β€” Unlock Premium from $10/mo β†’
CW
Search courses…
πŸ’Ό Servicesℹ️ Aboutβœ‰οΈ ContactView Pricing Plansfrom $10

Handling NULL Values with IS NULL

SQL Data TypesIS NULL🟒 Free Lesson

Advertisement

SQL Data Types

Handling NULL Values with IS NULL

NULL represents missing or unknown data and requires special handling in SQL.

  • IS NULL β€” checks if a value is NULL
  • IS NOT NULL β€” checks if a value exists Handle missing data gracefully in your queries.

What is NULL?

DfNULL Value

NULL is a special marker in SQL indicating that a value does not exist or is unknown. It is not equal to zero, an empty string, or FALSE β€” it represents the complete absence of a value.

-- Find all customers without an email address
SELECT first_name, last_name
FROM customers
WHERE email IS NULL;

-- Find employees who haven't been assigned a department
SELECT employee_name
FROM employees
WHERE department_id IS NULL;

-- Find orders without a ship date
SELECT order_id, order_date
FROM orders
WHERE ship_date IS NULL;

NULL Comparison Rules

DfThree-Valued Logic

SQL uses three-valued logic: TRUE, FALSE, and UNKNOWN. Any comparison with NULL results in UNKNOWN, which is why NULL = NULL does not return TRUE.

Three-Valued Logic with NULLTRUE5 = 5 β†’ TRUE10 > 3 β†’ TRUE'a' = 'a' β†’ TRUEExact match, comparisonFALSE5 = 6 β†’ FALSE3 > 10 β†’ FALSE'a' = 'b' β†’ FALSENo match, fails conditionUNKNOWNNULL = NULL β†’ UNKNOWNNULL = 5 β†’ UNKNOWNNULL > 0 β†’ UNKNOWNUse IS NULL, not = NULLNULL is not a value β€” it represents missing or unknown data
ExpressionResultExplanation
NULL = NULLUNKNOWNTwo NULLs are not equal
NULL != NULLUNKNOWNNULLs are not unequal either
NULL = 5UNKNOWNNULL compared to any value
NULL > 0UNKNOWNNULL in any comparison
NULL AND TRUEUNKNOWNLogical operations with NULL
NULL OR TRUETRUEOR with TRUE yields TRUE
NOT NULLUNKNOWNNOT NULL remains UNKNOWN
-- WRONG: This query returns nothing
SELECT * FROM users WHERE deleted_at = NULL;

-- CORRECT: Use IS NULL
SELECT * FROM users WHERE deleted_at IS NULL;

-- Find records where optional field has value
SELECT customer_id, phone_number
FROM customers
WHERE phone_number IS NOT NULL;

Common NULL Handling Patterns

Use COALESCE, NULLIF, and ISNULL functions to handle NULL values gracefully in expressions and aggregations.

-- Replace NULL with a default value
SELECT 
  first_name,
  COALESCE(email, 'No email provided') AS email_display
FROM customers;

-- Use NULLIF to avoid division by zero
SELECT 
  product_name,
  total_sales / NULLIF(units_sold, 0) AS avg_price
FROM sales_report;

-- COUNT only non-NULL values
SELECT COUNT(email) AS email_count
FROM customers;

-- COUNT all rows regardless of NULLs
SELECT COUNT(*) AS total_rows
FROM customers;

NULL Handling Functions

FunctionPurposeExampleResult
COALESCEFirst non-NULL valueCOALESCE(a, b, c)Returns first non-NULL
NULLIFReturns NULL if equalNULLIF(a, b)NULL if a = b
ISNULLReplace NULL (SQL Server)ISNULL(a, 0)0 if a is NULL
IFNULLReplace NULL (MySQL)IFNULL(a, 0)0 if a is NULL
NVLReplace NULL (Oracle)NVL(a, 0)0 if a is NULL
-- Find orders where optional discount was applied
SELECT order_id, discount_amount
FROM orders
WHERE discount_amount IS NOT NULL;

-- Identify incomplete customer records
SELECT customer_id
FROM customers
WHERE email IS NULL 
   OR phone_number IS NULL
   OR address IS NULL;

-- Aggregate with NULL handling
SELECT 
  department,
  AVG(COALESCE(bonus, 0)) AS avg_bonus
FROM employees
GROUP BY department;

Never use = NULL or != NULL in WHERE clauses. Always use IS NULL or IS NOT NULL for proper NULL comparison.

Key Takeaways

  1. NULL represents missing or unknown data, not zero or empty string
  2. Always use IS NULL or IS NOT NULL to compare NULL values
  3. Any comparison with NULL returns UNKNOWN, not TRUE or FALSE
  4. Use COALESCE to provide default values for NULLs
  5. COUNT(column) excludes NULLs; COUNT(*) includes all rows
⭐

Premium Content

Handling NULL Values with IS NULL

Unlock this lesson and 900+ advanced tutorials with a Premium plan.

🎯End-to-end Projects
πŸ’ΌInterview Prep
πŸ“œCertificates
🀝Community Access

Already a member? Log in

Need Expert SQL Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement