TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2739 1K
๐Ÿš€ SQL Roadmap 2026 โ€” Part 8

NULL Handling, COALESCE & NULLIF โ€” Managing Missing Data in SQL

In real-world databases, missing data is extremely common.

Customers may not have a phone number.

Orders may not have a discount.

Employees may not have a resignation date.

Transactions may have missing reference values.

SQL uses NULL to represent an unknown or missing value.

Understanding NULL properly is essential for accurate SQL queries and data analysis.

๐Ÿง  1. What is NULL?

"NULL" means:

ยซThe value is missing, unknown, or not available.ยป

Example:

customer_id | customer_name | phone
101 | Alice | 9876543210
102 | Bob | NULL
103 | Charlie | 9123456780


Bob's phone number is not stored.

It does not necessarily mean:

โ€ข "0"

โ€ข empty string "''"

โ€ข "Unknown"

โ€ข "N/A"

These are different values.

โš ๏ธ 2. NULL Is Not Equal to 0

SELECT *
FROM customers
WHERE credit_limit = 0;


This finds customers whose credit limit is actually zero.

It will not find customers whose credit limit is missing.

To find missing values:

SELECT *
FROM customers
WHERE credit_limit IS NULL;


โš ๏ธ 3. Never Use = NULL

This is incorrect:

SELECT *
FROM customers
WHERE phone = NULL;


It won't correctly identify NULL values.

Use:

SELECT *
FROM customers
WHERE phone IS NULL;


And for non-NULL values:

SELECT *
FROM customers
WHERE phone IS NOT NULL;


Remember:

= NULL       โŒ
<> NULL โŒ
IS NULL โœ…
IS NOT NULL โœ…


๐Ÿ”ข 4. NULL in Calculations

Suppose:

order_id | price | discount
1 | 1000 | 100
2 | 800 | NULL


Now:

SELECT
price,
discount,
price - discount AS final_price
FROM orders;


For order 2, the result may be:

NULL


because:

800 - NULL = NULL


SQL generally cannot determine the result when one operand is unknown.

๐Ÿ› ๏ธ 5. COALESCE()

COALESCE() is one of the most important functions for handling NULL values.

It returns the first non-NULL value.

Syntax:

COALESCE(value1, value2, value3, ...)


Example:

SELECT
customer_name,
COALESCE(phone, 'Not Available') AS phone
FROM customers;


If "phone" is NULL:

NULL โ†’ Not Available


๐ŸŽฏ 6. COALESCE with Multiple Values

You can provide several fallback values.

SELECT
customer_name,
COALESCE(phone, email, 'No Contact Information') AS contact
FROM customers;


SQL checks in order:

phone
โ†“
email
โ†“
No Contact Information


The first non-NULL value is returned.

๐Ÿ’ฐ 7. COALESCE for Financial Calculations

Suppose discounts can be NULL.

Instead of:

SELECT
price - discount AS final_price
FROM orders;


Use:

SELECT
price - COALESCE(discount, 0) AS final_price
FROM orders;


Now a missing discount is treated as zero.

Example:

Price | Discount | Final Price
1000 | 100 | 900
800 | NULL | 800


This is extremely common in analytics.

๐Ÿ“Š 8. COALESCE with Aggregations

Suppose there are no matching transactions for a customer.

You may want to display:

0


instead of NULL.
  • โค 3
More from @sqlanalyst
  1. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  2. Oct 7, 2026SQL Interview Series โ€” Part 4 ๐Ÿ“Œ Question 4: Find the Highest Salary in Each Department Suโ€ฆ
  3. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  4. Sep 29, 2026SQL Interview Series โ€” Part 2 ๐Ÿ“Œ Question 2: Find Duplicate Records Suppose you have an Emโ€ฆ
  5. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
  6. Sep 29, 2026SQL Interview Series โ€” Part 1 Hi guys, let's start a SQL interview series covering frequenโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’