Using COALESCE and NULLIF as CASE Shortcuts
📖 Scenario: You are managing a small customer database for a local store. Some customers have missing phone numbers or prefer to use a secondary contact number. You want to write queries that handle these cases gracefully.
🎯 Goal: Build SQL queries that use COALESCE and NULLIF to simplify CASE expressions for handling missing or default values in customer contact information.
📋 What You'll Learn
Create a table called
customers with columns id, name, phone_primary, and phone_secondary.Insert specific customer data with some
NULL and empty string values in phone columns.Write a query using
COALESCE to select the first available phone number for each customer.Write a query using
NULLIF to treat empty strings as NULL in phone numbers.💡 Why This Matters
🌍 Real World
Handling missing or default values in databases is common in real-world applications like customer management systems.
💼 Career
Knowing how to use COALESCE and NULLIF helps database developers and analysts write cleaner, more efficient queries.
Progress0 / 4 steps