TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2231 3.41K
โœ… SQL Programming: Handling NULL Values ๐Ÿ› ๏ธ

Missing data is common in databases. COALESCE() helps you fill in defaults and avoid null-related issues.

1๏ธโƒฃ What is COALESCE?
Returns the first non-null value in a list.
SELECT name, COALESCE(phone, 'Not Provided') AS contact  
FROM customers;

โœ… If phone is NULL, it shows โ€˜Not Providedโ€™.

2๏ธโƒฃ COALESCE with Calculations
Prevent nulls from breaking math.
SELECT name, salary, COALESCE(bonus, 0) AS bonus,  
salary + COALESCE(bonus, 0) AS total_income
FROM employees;

โœ… If bonus is NULL, treat it as 0 to compute total.

3๏ธโƒฃ Nested COALESCE
Use multiple fallback options.
SELECT name, COALESCE(email, alt_email, 'No Email') AS contact_email  
FROM users;

โœ… Checks email, then alt_email, then default text.

4๏ธโƒฃ COALESCE in WHERE clause
Filter even when data has nulls.
SELECT *  
FROM products
WHERE COALESCE(category, 'Uncategorized') = 'Electronics';


๐ŸŽฏ Use COALESCE When You Want To:
โ€ข Replace NULLs with defaults
โ€ข Keep math & filters working
โ€ข Avoid errors in reports or dashboards

๐Ÿ“ Practice Tasks:
1. Replace nulls in city with โ€˜Unknownโ€™
2. Show total amount = price + tax (tax may be null)
3. Replace nulls in description with โ€˜No Info Availableโ€™

โœ… Solution for Practice Tasks ๐Ÿ‘‡

1๏ธโƒฃ Replace NULLs in city with 'Unknown'
SELECT name, COALESCE(city, 'Unknown') AS city  
FROM customers;


2๏ธโƒฃ Show total amount = price + tax (tax may be NULL)
SELECT product_name, price, COALESCE(tax, 0) AS tax,  
price + COALESCE(tax, 0) AS total_amount
FROM products;


3๏ธโƒฃ Replace NULLs in description with 'No Info Available'
SELECT product_name, COALESCE(description, 'No Info Available') AS description  
FROM products;


๐Ÿ’ฌ Tap โค๏ธ for more!
  • โค 4
More from @sqlanalyst
  1. Oct 13, 2026SQL Interview Series โ€” Part 8 ๐Ÿ“Œ Question 8: Find Customers Who Have Never Placed an Orderโ€ฆ
  2. Oct 13, 2026๐—ง๐—ข๐—ฃ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ข๐—จ๐—ฅ๐—ฆ๐—˜๐—ฆ ๐—ง๐—ข ๐—•๐—ข๐—ข๐—ฆ๐—ง ๐—ฌ๐—ข๐—จ๐—ฅ ๐—ฆ๐—ž๐—œ๐—Ÿ๐—Ÿ๐—ฆ ๐—œ๐—ก ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ! Explorโ€ฆ
  3. Oct 12, 2026SQL Interview Series โ€” Part 6 ๐Ÿ“Œ Question 6: Find the Top 2 Highest-Paid Employees in Eachโ€ฆ
  4. Oct 12, 2026๐Ÿš€ ๐—ช๐—ต๐—ถ๐—ฐ๐—ต ๐—ง๐—ฒ๐—ฐ๐—ต ๐—ฆ๐—ธ๐—ถ๐—น๐—น ๐—ช๐—ถ๐—น๐—น ๐—ฌ๐—ผ๐˜‚ ๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ? Want to builโ€ฆ
  5. Oct 9, 2026SQL Interview Series โ€” Part 5 ๐Ÿ“Œ Question 5: Find Employees Who Earn More Than Their Managโ€ฆ
  6. Oct 9, 2026๐Ÿ‡ฎ๐Ÿ‡ณ ๐—š๐—ข๐—ฉ๐—˜๐—ฅ๐—ก๐— ๐—˜๐—ก๐—ง ๐—ข๐—™ ๐—œ๐—ก๐——๐—œ๐—” โ€” ๐—”๐—œ๐—–๐—ง๐—˜ ๐—œ๐—ก๐—ง๐—˜๐—ฅ๐—ก๐—ฆ๐—›๐—œ๐—ฃ๐—ฆ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ ๐Ÿš€โ€ฆ
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 โ†’