{
    "slug": "mysql_null_handling",
    "term": "MySQL NULL Handling",
    "category": "database",
    "difficulty": "beginner",
    "short": "NULL in SQL represents an unknown value — it is not zero, not empty string, and comparisons with = NULL are always false.",
    "long": "NULL = NULL evaluates to NULL (not true) — use IS NULL and IS NOT NULL for comparisons. NULL in aggregate functions is ignored by COUNT(column) but counted by COUNT(*). NULL values sort last in ORDER BY ASC (MySQL-specific — other databases differ). COALESCE(val, default) returns the first non-NULL value. IFNULL(val, default) is MySQL-specific. Columns with frequent NULL values have reduced index efficiency.",
    "aliases": [
        "SQL NULL",
        "IS NULL MySQL",
        "COALESCE MySQL",
        "NULL comparison SQL"
    ],
    "tags": [
        "mysql",
        "database",
        "sql"
    ],
    "misconception": "NULL = NULL is true. NULL compared to anything — including another NULL — always returns NULL (unknown). Use IS NULL for null checks.",
    "why_it_matters": "Using = NULL in WHERE clauses silently matches nothing — a common source of 'query returns no rows' bugs. NULL propagates through arithmetic — NULL + 5 = NULL.",
    "common_mistakes": [
        "Writing WHERE column = NULL instead of WHERE column IS NULL — matches zero rows.",
        "Expecting COUNT(column) to count NULL rows — it doesn't; use COUNT(*) instead.",
        "Sorting by a nullable column and not accounting for NULLs appearing last (ASC) or first (DESC)."
    ],
    "when_to_use": [
        "Use IS NULL / IS NOT NULL for all NULL comparisons — never use = NULL.",
        "Use COALESCE(column, default) to substitute a fallback for NULL values in SELECT."
    ],
    "avoid_when": [
        "Do not use nullable columns for data that is always required — use NOT NULL with a sensible DEFAULT instead.",
        "Do not rely on IFNULL() for portability — use COALESCE() which is SQL standard."
    ],
    "related": [
        "mysql_joins",
        "db_transactions"
    ],
    "prerequisites": [
        "mysql_joins"
    ],
    "refs": [
        "https://dev.mysql.com/doc/refman/8.0/en/working-with-null.html"
    ],
    "bad_code": "-- Wrong: WHERE column = NULL always returns 0 rows\nSELECT * FROM users WHERE deleted_at = NULL; -- always empty\n\n-- Wrong: COUNT(nullable_column) skips NULLs\nSELECT COUNT(deleted_at) FROM users; -- counts only non-NULL deleted_at",
    "good_code": "-- Correct NULL comparison\nSELECT * FROM users WHERE deleted_at IS NULL;\nSELECT * FROM users WHERE deleted_at IS NOT NULL;\n\n-- COALESCE for default values\nSELECT id, COALESCE(display_name, email) AS name FROM users;\n\n-- COUNT distinction\nSELECT COUNT(*) as total, COUNT(email) as with_email FROM users;",
    "quick_fix": "Use IS NULL / IS NOT NULL for comparisons — use COALESCE() to provide defaults for nullable columns",
    "effort": "low",
    "created": "2026-03-31",
    "updated": "2026-03-31",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/mysql_null_handling",
        "html_url": "https://codeclaritylab.com/glossary/mysql_null_handling",
        "json_url": "https://codeclaritylab.com/glossary/mysql_null_handling.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[MySQL NULL Handling](https://codeclaritylab.com/glossary/mysql_null_handling) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/mysql_null_handling"
            }
        }
    }
}