Part 5: Mastering NULL Functions

Part 5: Mastering NULL Functions | SQL Challenges & Capstone
SQL Business Logic & Data Handling Practice · Part 5 · Lecture Notes

Mastering NULL Functions

How to replace missing values in query output using IFNULL and COALESCE — creating clean, standardized business reports without changing the database.

Part 5 of 683% Complete
IFNULL()
COALESCE()
NULL Replacement
Default Values
8 Practice Questions

Learning Objectives

  • Use IFNULL() to replace a single column’s NULL values with a meaningful default label
  • Use COALESCE() to check multiple values and return the first non-NULL one found
  • Combine NULL functions with ORDER BY and additional columns for complete business reports
  • Understand that NULL functions only affect query output β€” the database data is never changed
Click any section header to open or close it

NULL functions replace missing values with alternative values inside query results. They do not change any data in the database β€” they only substitute a readable label in the output wherever a NULL would otherwise appear. The result is cleaner, more complete reports that non-technical stakeholders can read without confusion.

In industry, analysts use NULL functions to clean incomplete business data, standardize report outputs, replace missing values with default business labels, improve dashboard readability, and prevent empty cells in exported reports.

Two Functions β€” IFNULL and COALESCE

In MySQL, IFNULL(column, replacement) checks one column and returns the replacement if it is NULL. COALESCE(column1, column2, replacement) checks a list of values and returns the first non-NULL one β€” making it more flexible for complex scenarios.

Basic IFNULL Syntax
SELECT IFNULL(column_name, ‘replacement_value’) FROM table_name;
Basic COALESCE Syntax
SELECT COALESCE(column_name, ‘replacement_value’) FROM table_name;

IFNULL takes two arguments: the column to check, and the value to return if that column is NULL. If the column contains a real value, IFNULL returns the real value unchanged. If the column is NULL, IFNULL returns the replacement. It is the most direct NULL handling function for single-column scenarios.

Reading IFNULL

IFNULL(end_date, ‘Active Subscription’) reads as: “return end_date if it exists, otherwise return the text Active Subscription.” The replacement can be any text, number, or date value.

Replace NULL end_date With a Label
SELECT subscription_id, IFNULL(end_date, ‘Active Subscription’) AS subscription_end FROM subscriptions;
Replace NULL resolved_date With a Label
SELECT ticket_id, IFNULL(resolved_date, ‘Not Resolved’) AS resolution_status FROM support_tickets;

COALESCE checks a list of values from left to right and returns the first one that is not NULL. For simple two-argument cases it produces the same result as IFNULL. Its real power comes when multiple fallback options exist β€” COALESCE(field1, field2, field3, ‘default’) tries each in order and returns the first real value found.

COALESCE vs IFNULL

For single-column NULL replacement, both produce identical results. Use IFNULL when you have one column and one fallback. Use COALESCE when you have multiple columns to try, or when writing SQL that may run on databases other than MySQL (COALESCE is ANSI standard; IFNULL is MySQL-specific).

COALESCE on end_date β€” Ongoing Label
SELECT subscription_id, COALESCE(end_date, ‘Ongoing’) AS subscription_status FROM subscriptions;
COALESCE on resolved_date β€” Pending Label
SELECT ticket_id, COALESCE(resolved_date, ‘Pending Resolution’) AS ticket_resolution FROM support_tickets;

NULL functions can be combined with every other SQL clause. Adding ORDER BY lets analysts sort the cleaned output. Including other columns alongside the NULL function gives full business context. These combinations are standard in subscription reporting and support queue management.

Subscription Reporting Pattern

Combining IFNULL(end_date, ‘Active Member’) with status and ORDER BY monthly_fee DESC produces a complete, sorted subscription report where NULL end dates are clearly labelled β€” ready for a business dashboard without any manual cleanup.

Full Subscription Report With NULL Handling
SELECT subscription_id, ROUND(monthly_fee, 2) AS monthly_fee, IFNULL(end_date, ‘Subscription Active’) AS subscription_end FROM subscriptions ORDER BY monthly_fee DESC;
Support Tickets Sorted by Created Date
SELECT ticket_id, created_date, COALESCE(resolved_date, ‘Awaiting Resolution’) AS resolution_status FROM support_tickets ORDER BY created_date ASC;

Work through each question below. Run your query first, then use Check Answer. A wrong answer reveals the correct SQL with a Copy button; a correct result earns a congratulations message.

Practice 1 Replace NULL end_date values with Active Subscription in the subscriptions table
AI Prompt
Write a MySQL query to replace NULL end_date values with Active Subscription in the subscriptions table.
SQL Answer
SELECT subscription_id, IFNULL(end_date, ‘Active Subscription’) AS subscription_end FROM subscriptions;
Practice 2 Replace NULL resolved_date values with Not Resolved in the support_tickets table
AI Prompt
Write a MySQL query to replace NULL resolved_date values with Not Resolved in the support_tickets table.
SQL Answer
SELECT ticket_id, IFNULL(resolved_date, ‘Not Resolved’) AS resolution_status FROM support_tickets;
Practice 3 Use COALESCE to replace NULL end_date values with Ongoing in the subscriptions table
AI Prompt
Write a MySQL query using COALESCE to replace NULL end_date values with Ongoing in the subscriptions table.
SQL Answer
SELECT subscription_id, COALESCE(end_date, ‘Ongoing’) AS subscription_status FROM subscriptions;
Practice 4 Use COALESCE to replace NULL resolved_date values with Pending Resolution in the support_tickets table
AI Prompt
Write a MySQL query using COALESCE to replace NULL resolved_date values with Pending Resolution in the support_tickets table.
SQL Answer
SELECT ticket_id, COALESCE(resolved_date, ‘Pending Resolution’) AS ticket_resolution FROM support_tickets;
Practice 5 Replace NULL end_date values with Active Member and display status from the subscriptions table
AI Prompt
Write a MySQL query to replace NULL end_date values with Active Member and display status from the subscriptions table.
SQL Answer
SELECT subscription_id, status, IFNULL(end_date, ‘Active Member’) AS subscription_end FROM subscriptions;
Practice 6 Replace NULL resolved_date values with Awaiting Resolution and sort by created_date
AI Prompt
Write a MySQL query to replace NULL resolved_date values with Awaiting Resolution and sort by created_date.
SQL Answer
SELECT ticket_id, created_date, COALESCE(resolved_date, ‘Awaiting Resolution’) AS resolution_status FROM support_tickets ORDER BY created_date ASC;
Practice 7 Replace NULL end_date values with Subscription Active and sort by monthly_fee rounded to 2 decimal places
AI Prompt
Write a MySQL query to replace NULL end_date values with Subscription Active and sort by monthly_fee rounded to 2 decimal places.
SQL Answer
SELECT subscription_id, ROUND(monthly_fee, 2) AS monthly_fee, IFNULL(end_date, ‘Subscription Active’) AS subscription_end FROM subscriptions ORDER BY monthly_fee DESC;
Practice 8 Use COALESCE to replace NULL resolved_date values with Open Ticket from the support_tickets table
AI Prompt
Write a MySQL query using COALESCE to replace NULL resolved_date values with Open Ticket from the support_tickets table.
SQL Answer
SELECT ticket_id, COALESCE(resolved_date, ‘Open Ticket’) AS ticket_status FROM support_tickets;

Knowledge Check

Five questions β€” select the best answer for each.

1. What does IFNULL(end_date, ‘Active Subscription’) do?
AIt deletes rows where end_date is NULL
BIt returns end_date if it exists, and Active Subscription if end_date is NULL
CIt filters subscriptions that are currently active
DIt renames the end_date column to Active Subscription permanently
2. What is the key difference between IFNULL and COALESCE?
AIFNULL works on dates only; COALESCE works on text
BCOALESCE can check multiple values and return the first non-NULL; IFNULL checks one column with one fallback
CCOALESCE permanently updates NULL values; IFNULL does not
DIFNULL is faster than COALESCE on all queries
3. Does IFNULL or COALESCE change any data in the database?
AYes β€” they update NULL values to the replacement permanently
BYes β€” but only for the duration of the session
CNo β€” they only affect the output of the query
DIt depends on whether you include ORDER BY
4. An analyst writes COALESCE(resolved_date, ‘Pending Resolution’). When does Pending Resolution appear in the output?
AWhen resolved_date contains the text Pending
BWhen resolved_date is NULL
CWhen resolved_date is greater than today’s date
DWhen resolved_date equals zero
5. Which MySQL function is ANSI SQL standard and works across all major database systems?
AIFNULL
BISNULL
CCOALESCE
DNULLIF

Part 5 Summary

The key ideas from this lecture β€” keep these in mind as you move forward.

IFNULL and COALESCE help analysts replace missing business data and create cleaner, more standardized SQL outputs. IFNULL checks one column with one fallback. COALESCE checks a list and returns the first non-NULL value. Neither changes the database.

IFNULLCOALESCENULL ReplacementMissing DataDefault ValuesReport CleaningSubscriptionsSupport TicketsANSI Standard