Pathubs SQL Curriculum • Module 06

SQL NULL

Master how SQL handles missing and unknown data: understand why = NULL fails, learn IS NULL and IS NOT NULL, explore three-valued logic, and distinguish NULL from 0, empty strings, and text.

⏱️ Estimated Time:45 Minutes
🎯 Level:Beginner
📊 Track:Data Analytics & SQL Mastery
✨ Mode:Interactive NULL & Three-Valued Logic Lab
1

Introduction

In real-world databases, information is frequently incomplete. Consider a customer directory:

EmployeePhone NumberStatus
Rahul9876543210Known & Available
PriyaNULLMissing / Unknown / Not Provided
Amit9123456789Known & Available

In SQL, NULL is a special marker representing the absence of a value. It is not automatically the number zero, not an empty text string, and not the text word "NULL".

2

What Is NULL?

NULL signifies that a data value is missing, unknown, unassigned, or not applicable.

StateSQL RepresentationWhat It Truly Means
NULLNULLNo value exists; value is unknown or unrecorded.
Numeric Zero0An exact known numeric quantity of zero.
Empty String''A known text string containing 0 characters.
Literal Text'NULL'A 4-letter word ('N', 'U', 'L', 'L').
Diagram 1: Distinguishing NULL From Normal Values
NULL
Missing / Unknown
0
Numeric Zero
''
Empty String
'NULL'
4-Letter Word
3

Why Does NULL Exist?

Relational databases require a standard way to store data when a field cannot have a regular value:

  • Information was not provided: A user signed up with an email but skipped the optional phone number.
  • Data is currently unknown: A shipment is created, but the delivery date is not yet known.
  • Data is not applicable: An employee is paid an hourly wage, so the annual salary column does not apply.
4

NULL Is Not a Normal Value: Why = NULL Fails

The #1 mistake SQL beginners make is attempting to query NULL values using standard equality:

-- ❌ THIS NEVER WORKS (Returns 0 rows):
SELECT * FROM employees
WHERE phone = NULL;
⚠️
Why does = NULL fail? In SQL logic, NULL represents an unknown. If you ask the database: "Is this unknown phone number equal to another unknown?", the database cannot say TRUE or FALSE. It answers UNKNOWN. Because a WHERE clause only returns rows where the condition is confirmed TRUE, = NULL drops every row!
5

The IS NULL Operator

To check if a column value is missing, SQL provides the dedicated IS NULL operator:

-- ✅ Correct way to find missing values:
SELECT name, department
FROM employees
WHERE phone IS NULL;
Diagram 2: IS NULL Evaluation
Column Value: phone = NULL
➔ IS NULL? ➔
TRUE (Row Returned)
6

The IS NOT NULL Operator

To find all rows where a column has a recorded, non-missing value, use IS NOT NULL:

SELECT name, phone
FROM employees
WHERE phone IS NOT NULL;
7

NULL vs Zero vs Empty String Deep-Dive

Confusing these values causes severe data calculation bugs. Consider an e-commerce customer table:

Column ValueBusiness MeaningMatches IS NULL?Matches = 0?Matches = ''?
NULLCustomer never answeredTRUEUNKNOWNUNKNOWN
0Customer explicitly has 0 ordersFALSETRUEFALSE
'' (Empty)Blank text box submittedFALSEFALSETRUE
8

NULL and Comparison Operators

When comparing columns with numbers or text (e.g. >, <, =, <>), rows where the column is NULL evaluate to UNKNOWN:

SELECT * FROM employees
WHERE salary > 50000;

If Sneha's salary is NULL, NULL > 50000 produces UNKNOWN. Since WHERE requires TRUE, Sneha is dropped from the result set.

Diagram 3: Three-Valued Logic in WHERE Filtering
Condition = TRUE
✓ Row Returned
Condition = FALSE
✗ Row Excluded
Condition = UNKNOWN (NULL)
✗ Row Excluded
9

NULL With AND / OR / NOT

When logical operators interact with UNKNOWN values, they follow three-valued logic truth rules:

ExpressionEvaluation ResultExplanation
TRUE AND UNKNOWNUNKNOWNAND requires all parts to be TRUE; unknown status leaves it unresolved.
FALSE AND UNKNOWNFALSESince one part is already FALSE, the entire AND condition is definitely FALSE.
TRUE OR UNKNOWNTRUESince one part is already TRUE, the OR condition is satisfied regardless!
NOT UNKNOWNUNKNOWNNegating an unknown value is still unknown.
10

Finding and Cleaning NULL Data

Data analysts spend a significant portion of time isolating missing records for data quality audits:

-- Audit missing contact info
SELECT id, name
FROM customers
WHERE email IS NULL OR phone IS NULL;
Live Interactive NULL Lab
📦 Candidate Row Three-Valued Evaluation8 Total Records
NameDeptSalaryPhoneEvaluation Status
Rahul SharmaSales₹62,0009876543210Phone is '9876543210' ➔ FALSE (Drop)
Priya PatelHR₹48,000NULLPhone is NULL ➔ TRUE (Keep)
Amit VermaEngineering₹85,0009123456789Phone is '9123456789' ➔ FALSE (Drop)
Sneha RaoSalesNULL9988776655Phone is '9988776655' ➔ FALSE (Drop)
Vikram SinghIntern₹0NULLPhone is NULL ➔ TRUE (Keep)
Ananya GuptaHR₹45,000'' (Empty)Phone is '' ➔ FALSE (Drop)
Rohan JoshiSales₹71,000NULLPhone is 'NULL' ➔ FALSE (Drop)
Kavita NairEngineeringNULL9845123456Phone is '9845123456' ➔ FALSE (Drop)
✍️ SQL NULL Query Editor● Three-Valued Evaluator
📋 Output Result Set2 Rows Kept (6 Dropped)
namedepartmentsalaryphone
Priya PatelHR₹48,000NULL
Vikram SinghIntern₹0NULL
🔍 Interactive Tool: NULL vs 0 vs '' vs 'NULL'

Switch the sample cell value below to see how standard SQL filters evaluate against each distinct data state:

Current Cell State:
NULL (Absent)
WHERE col IS NULL:
TRUE (Matches!)
WHERE col = 0:
UNKNOWN (Does NOT match)
WHERE col = '':
UNKNOWN (Does NOT match)
11

Common NULL Mistakes

1. Writing = NULL or <> NULL

Never use comparison operators with NULL. Always use IS NULL or IS NOT NULL.

2. Assuming NULL Becomes Zero in Math

In SQL, salary + 1000 evaluates to NULL when salary is NULL. Math on NULL always produces NULL.

3. Searching for the String 'NULL'

Writing WHERE phone = 'NULL' searches for four literal alphabet characters, not missing data.

🧠 Interactive Challenge: Predict The Result (1 of 3)
SELECT name FROM employees WHERE phone = NULL;
How many rows will this query return?
12

Practical NULL Exercises

Task GoalTarget TableRequired SQL SolutionConcept Tested
1. Customers Without PhonecustomersSELECT * FROM customers WHERE phone IS NULL;IS NULL syntax
2. Verified Active EmailsusersSELECT * FROM users WHERE email IS NOT NULL;IS NOT NULL filter
3. Missing Product DescriptionsproductsSELECT * FROM products WHERE description IS NULL;Text column NULL detection
4. Both Contacts MissingcontactsSELECT * FROM contacts WHERE phone IS NULL AND email IS NULL;Multiple IS NULL conjunctions
5. High Earners With Known SalaryemployeesSELECT * FROM employees WHERE salary IS NOT NULL AND salary > 80000;Safe numeric filtering
13

NULL Best Practices

  • Always Use IS NULL / IS NOT NULL: Never write = NULL or != NULL.
  • Do Not Assume NULL Equals Zero: If an employee took 0 leave days, record 0. If their leave records are lost, record NULL.
  • Account for UNKNOWN in WHERE Filters: Be aware that standard comparison filters automatically exclude rows with NULL values.
  • Handle Missing Data Intentionally: Know your schema design and verify whether empty strings or NULL markers are used for blank user inputs.
14

What You Should Know Now

  • Meaning of NULL: Represents absent, missing, or unknown data
  • NULL vs 0: NULL is absence; 0 is an actual number
  • NULL vs '': NULL is absent; '' is a 0-character string
  • IS NULL: Tests if a value is missing
  • IS NOT NULL: Tests if a value is present
  • Three-Valued Logic: Comparisons with NULL produce UNKNOWN

🎯 Knowledge Check Quiz: SQL NULL

Test your understanding of missing data handling, three-valued logic, and IS NULL / IS NOT NULL operators.

1. What does NULL represent in a relational database?
2. Why does "WHERE column_name = NULL" never return any rows in standard SQL?
3. Which SQL clause correctly retrieves customers who do not have a recorded email address?
4. What is the boolean outcome of "WHERE 50000 = NULL"?
5. How does SQL WHERE treat a condition that evaluates to UNKNOWN?
6. Which query finds all employees who have a known, recorded phone number?
7. What is the critical difference between 0 and NULL in a "bonus_amount" column?
8. What is the recommended best practice when checking for missing text values in data analysis?