Handling Missing Values with the ISNULL Function, Explained

UPI Study › Courses › Database Programming › Free lessons › Lesson 5.3 Free with a free account

Handling Missing Values with the ISNULL Function

Database Programming · Chapter 5: Querying, Filtering, and Sorting DataSection 5.3 · lesson 28 of 768 min · free with a free account

← Controlling Result Size: Limiting Query OutputNext lesson: Core Concepts for Manipulating Databases with SQL →

A free account plays all of Chapter 1 and 3 more lessons of Database Programming. Create a free account →

Earn 3 college credits for Database ProgrammingRecommended for credit by NCCRS · Computer Science 381 · self-paced · one payment, lifetime access

In this lesson

Testing for NULL and replacing missing values with ISNULL, IFNULL, and COALESCE.

  • Distinguish NULL from zero and empty text in stored data
  • Explain how three-valued logic treats comparisons and conditions involving NULL
  • Use IS NULL and IS NOT NULL to find missing values
  • Compare ISNULL in SQL Server and MySQL with IFNULL and COALESCE

Free study notes

Get this lesson's notes by email

The key points and 4 practice questions with answers, in your inbox. One email, free.

One email. Unsubscribe in one click.

Transcript

Everything said in the video, section by section (946 words). Read along, or search it.

Opening Question

A Missing Bonus Shown as Zero

Amina Diallo administers Lantern Books' database. A report labels every missing bonus as zero, and a manager assumes the amounts were reviewed and rejected. But no amount was recorded. How can a report distinguish an unknown value from a known zero, while still showing useful totals? Null marks a missing, unknown, or inapplicable value. Testing for that marker and replacing it are different operations, with confusingly similar function names.

Definition

NULL Is Not Empty Text or Zero

This disposable staff extension uses illustrative departments, salaries, and bonuses. It does not alter our canonical customer accounts or their points. The bonus column permits null. Some staff have an amount recorded, while others have no bonus amount available. A missing marker is not an empty string. Empty text has a known length of zero; a missing phone number tells us no text value is recorded. Zero is also a real value. Recording a zero bonus says the amount is known, whereas leaving a bonus null preserves uncertainty. SQLite normally displays null as an empty terminal cell. Our demonstration files request the word null explicitly, so missing values are easy to identify.

Demonstration

Two Known Zeros and One NULL

Compare these expressions directly. The first is a numeric zero; the second measures empty text; the last is the missing marker. The numeric and text expressions have known results. Their shared zero does not turn either one into a missing value. Run the select with null display enabled. This changes the shell's presentation, without changing database semantics or stored values. The result shows two known zeros and one null. A useful display makes the distinction visible, instead of asking viewers to infer it from whitespace.

Three-Valued Logic

Comparisons With NULL Return Unknown

An ordinary comparison involving null produces unknown. Unknown is a third logical outcome, alongside true and false; it is not the same as false. This query tests a bonus threshold and its negation. In SQLite's displayed expressions, one means true, zero means false, and null represents unknown. A where condition keeps only true rows. False and unknown are both excluded, so negating a comparison does not automatically recover its missing values. Run the two expressions together for the first staff records. Watch what happens to the missing bonus under negation. Ben's comparison and its opposite are both unknown. Asha is above the threshold, and Chen is not; those known outcomes reverse normally.

Truth Rules

When AND and OR Settle Unknown

Combining conditions sometimes settles an answer despite missing information. False and unknown is false, because a conjunction already has a false requirement. True or unknown is true, because one sufficient alternative is already true. Negating unknown, however, leaves it unknown. Run these boundary cases. They explain why missing information matters differently, depending on the surrounding condition. The output preserves all three outcomes. Read logical conditions as claims with evidence, rather than treating a missing value as a disguised zero.

Missingness Tests

Finding Missing Bonuses With IS NULL

Use IS NULL to find missing bonuses, and IS NOT NULL to find recorded values. These predicates answer the missingness question directly. An equality comparison with null does not find the missing rows. Even null equal to null is unknown under ordinary equality. Run the proper missingness test. The condition is true for each record whose bonus has no value, regardless of its salary. Ben, Dara, Farid, and Hugo appear. To find unknown bonuses, keep this direct test rather than replacing values first and searching the replacements.

Replacement Functions

Substituting Zero With IFNULL for Reports

Arithmetic with a missing bonus gives a missing total. Here a second expression uses IFNULL to substitute zero before addition. This SQLite function takes the original expression and a replacement. MySQL uses the same two-argument name for replacement, while SQL Server uses ISNULL. The substitution is a reporting assumption. Use it only when treating an unrecorded bonus as zero is the policy; otherwise show that the total remains unresolved. Run both totals together. A select calculates values for its result, without updating the stored bonus column. Ben's recorded total is missing, while the assumed total equals salary. The label assumed matters: a useful calculation must not masquerade as recovered evidence.

Dialect Trap

ISNULL Tests in MySQL, Replaces in SQL Server

The name ISNULL hides a dialect trap. SQL Server's function takes two arguments and replaces; MySQL's function takes one argument and tests. MySQL's ISNULL function returns one for missing and zero for present. It does not return the original bonus or a chosen replacement. SQL Server normally gives its replacement result the first expression's type. A missing three-character code replaced with Unknown can be truncated to Unk. The shown cast makes that length rule explicit. A literal null first argument has special typing rules, so do not generalize from an untyped literal. COALESCE offers a portable replacement spelling, and NULLIF serves a different purpose. Both still require attention to types and business meaning.

Quick Check

Counting Ratings That Pass a Mixed Condition

A new ratings sandbox contains a missing score, a zero, and a four. The condition combines a negated comparison with an explicit missingness test. How many ratings pass? Track the truth value for each record before counting the retained rows. Pause, and decide.

Answer

Unknown OR True Becomes True

The answer is two ratings. Four passes the negated comparison, and the missing score passes the explicit IS NULL alternative. Zero fails both alternatives. Keeping only the known passing score misses the missingness branch; keeping every record ignores the comparison's false result. Unknown or true becomes true, so the missing score survives here. The query deliberately asks a different question from the negated comparison alone.

Key Takeaways

Test Gaps and Label Assumptions

Amina can now distinguish an unknown bonus from a reviewed zero. Her report can test the gap, and identify any assumption used to calculate a total. Use IS NULL for missingness, and remember that WHERE keeps TRUE. Negation preserves unknown until another condition settles the result. Choose replacement functions by dialect, check their types, and name the reporting policy. Replacing a value in a result does not correct the underlying record.

About Database Programming at UPI Study

Database Programming (Computer Science 381) is a self-paced online course of 76 video lessons in 12 chapters, from database applications, SQL setup, schema planning and normalization to keys and constraints, data types, filtering and sorting, joins, subqueries, views, indexes, ROLLUP and PIVOT, regular expressions and database security. It carries a NCCRS credit recommendation for 3 semester credits.

Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Compare: Database Fundamentals vs Database Programming

Why take it here

Why students take Database Programming with UPI Study

Write the SQL behind every appDatabase Programming teaches SQL in MySQL and SQL Server side by side: designing and normalizing tables, keys and constraints, data types, inserting, updating and deleting rows, filtering, grouping, joins, subqueries, views, indexes, transactions, and protecting data from SQL injection. It suits computer science and information technology degrees.
Credit at a fraction of the priceThe same 3 credits can cost $750 or more at a US college. Here it is one payment, self-paced, with lifetime access.
Finish your degree soonerTransfer credits reduce the courses you still owe, so you can graduate earlier or lighten a heavy semester.
Strengthen an applicationA completed college-level course on an official transcript shows admissions and scholarship committees you can do the work.
Study on your own scheduleNo start dates, no deadlines. Watch the lessons, practice, and take the final when you are ready.
130,000+students helped with college creditsACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Where the credit goes

Universities that work with NCCRS

Database Programming carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.

FAQ

Questions students ask us

About Handling Missing Values with the ISNULL Function

What does ISNULL do in SQL Server?
In SQL Server, ISNULL(expression, replacement) returns the replacement when the expression is NULL and the expression otherwise. For example, salary + ISNULL(bonus, 0) treats a missing bonus as zero in the result without changing the stored column. The result takes the first argument's data type, so replacing a NULL CHAR(3) value with 'Unknown' returns 'Unk'. Treating missing as zero is a reporting assumption and should be labeled as one.
What is the difference between ISNULL in MySQL and SQL Server?
MySQL's ISNULL tests whether a value is NULL, while SQL Server's ISNULL replaces a NULL with another value. In MySQL, ISNULL(expr) takes one argument and returns 1 when it is NULL and 0 otherwise. In SQL Server, ISNULL(expr, replacement) takes two arguments. The MySQL equivalent of SQL Server's ISNULL is IFNULL(expr, replacement), and COALESCE performs this replacement in MySQL, SQL Server, SQLite, and Oracle.
Why does WHERE bonus = NULL not find missing values?
Under standard settings, comparing anything with NULL using = returns unknown rather than true, and WHERE keeps only rows whose condition is true. Even NULL = NULL is unknown. To find missing values, write WHERE bonus IS NULL, and use IS NOT NULL for recorded ones. Negation does not rescue the comparison either: NOT (bonus > 500) is still unknown for a missing bonus, so that row is filtered out too.
Is NULL the same as zero or an empty string?
No, NULL marks a missing or unknown value, while zero and an empty string are known values. A bonus of 0 means no bonus was paid, but a NULL bonus means the amount is not recorded. MySQL, SQL Server, and SQLite store an empty string as known text of length zero, although Oracle treats it as NULL. Arithmetic shows the difference: salary + bonus returns NULL when the bonus is missing.

About the Database Programming course

How many college credits is Database Programming?
Database Programming (Computer Science 381) carries a NCCRS credit recommendation for 3 semester credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
How much does Database Programming cost and what is included?
$250, paid once, with lifetime access: all 76 video lessons, a practice quiz after each, the graded work and final exam, and an official transcript when you finish. Students who want several courses choose UPI Study PRO instead.
Which universities accept UPI Study credits?
UPI Study has signed partner agreements with universities such as Thomas Edison State University, University of the People and Charter Oak State College, and works with universities such as Penn State, SNHU and SUNY. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities. Use "Find my college" on this page to check yours.
How long does Database Programming take?
It is self-paced with no deadlines. The 76 lessons total about 11 hours of video; most students add reading and practice time and finish in a few weeks to a few months, on their own schedule.
How are the exams done?
Quizzes and assignments are online in the course. The final exam is taken online with proctoring; a person on the UPI Study team checks assignments, usually within two days.
How do my credits reach my college?
When the course shows 100% complete, request the official transcript from your dashboard. It is sent to the college you name, and you can send it to more than one. Check your college first with "Find my college" on this page, or ask Athena.

Ready for the full course? Earn 3 credits in Database Programming.

76 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.

Updated

Database Programming
3 credits · NCCRS
Enroll →