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
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Database Programming.
Lesson complete
Earn the 3 college credits for Database Programming
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: Core Concepts for Manipulating Databases with SQL →← 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 →
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.
Enroll now → Course details Download the syllabus All Computer Science courses Every credit course with PRO →
Why take it here
Why students take Database Programming with UPI Study
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.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →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?
What is the difference between ISNULL in MySQL and SQL Server?
Why does WHERE bonus = NULL not find missing values?
Is NULL the same as zero or an empty string?
About the Database Programming course
How many college credits is Database Programming?
How much does Database Programming cost and what is included?
Which universities accept UPI Study credits?
How long does Database Programming take?
How are the exams done?
How do my credits reach my college?
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
3 credits · NCCRSEnroll →