惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
The Cloudflare Blog
博客园_首页
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
人人都是产品经理
人人都是产品经理
爱范儿
爱范儿
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Jina AI
Jina AI
美团技术团队
博客园 - 【当耐特】
博客园 - Franky
T
Tailwind CSS Blog
雷峰网
雷峰网
小众软件
小众软件
阮一峰的网络日志
阮一峰的网络日志
Apple Machine Learning Research
Apple Machine Learning Research
Last Week in AI
Last Week in AI
V
V2EX
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
How NULL Values Skew Comparisons, Aggregations, and Dedup...
Michael · 2026-06-04 · via DEV Community

Michael

NULL in a gbase database doesn't behave like an ordinary value, nor is it simply a blank. It affects comparisons, deduplication, aggregations, and conditional logic in ways that often contradict intuition. Many "data is fine but results are off" issues trace back to mishandled NULL semantics.

First, Separate Three Easily Confused Concepts

Value Meaning Most Common Misjudgment
NULL Unknown, missing, not assigned Treated as an empty string
Empty string '' Zero‑length string Treated as NULL
Default value Filled by business rules or DDL Treated as a real business value

When these three coexist, you get the classic "some rows match, some don't" confusion.

Common Symptoms

  • WHERE col = '' returns nothing, but rows clearly exist.
  • COUNT(col) is far smaller than COUNT(*) — business thinks data was lost.
  • A mysterious "empty group" appears after GROUP BY, and nobody can say whether it's NULL or an empty string.
  • After a LEFT JOIN, dimension columns become NULL, and subsequent filters or aggregations silently shift the result.
  • CASE WHEN col = NULL THEN ... looks logical but produces wrong outcomes.

How to Diagnose

Step 1: Quantify the distribution of NULLs, empty strings, and valid values

Don't guess — query the actual distribution:

SELECT
    COUNT(*) AS total_cnt,
    SUM(CASE WHEN cust_level IS NULL THEN 1 ELSE 0 END) AS null_cnt,
    SUM(CASE WHEN cust_level = '' THEN 1 ELSE 0 END) AS empty_cnt,
    SUM(CASE WHEN cust_level IS NOT NULL AND cust_level <> '' THEN 1 ELSE 0 END) AS valid_cnt
FROM dwd_customer;

Enter fullscreen mode Exit fullscreen mode

Often, NULLs and empty strings come from completely different sources.

Step 2: Verify the aggregation function matches the business intent

SELECT COUNT(*) AS total_rows, COUNT(cust_level) AS non_null_rows FROM dwd_customer;

Enter fullscreen mode Exit fullscreen mode

If the requirement is "total row count," don't casually write COUNT(column). The core question is: are you counting records, or non‑null values?

Step 3: Check whether conditions silently exclude NULLs

-- This does not include NULL rows
SELECT * FROM dwd_customer WHERE cust_level <> 'VIP';

-- To include unknowns:
SELECT * FROM dwd_customer WHERE cust_level <> 'VIP' OR cust_level IS NULL;

Enter fullscreen mode Exit fullscreen mode

A Realistic Example

A customer table has channel_code values that are a mix of real values, NULLs, and empty strings. A business query counts users by channel:

SELECT channel_code, COUNT(*) AS cust_cnt
FROM dwd_customer
GROUP BY channel_code;

Enter fullscreen mode Exit fullscreen mode

An "empty channel" row appears, sparking debate. A more robust approach is to standardize the semantics before aggregation:

SELECT
    CASE
        WHEN channel_code IS NULL THEN 'NULL_VALUE'
        WHEN channel_code = '' THEN 'EMPTY_STRING'
        ELSE channel_code
    END AS channel_tag,
    COUNT(*) AS cust_cnt
FROM dwd_customer
GROUP BY channel_tag;

Enter fullscreen mode Exit fullscreen mode

This shifts the conversation from "the result feels wrong" to "which specific class of values is affecting the output."

SQL Areas Most Affected by NULL

SQL Type Common Deviation What to Check First
Filtering Missing NULL rows Are IS NULL / IS NOT NULL explicitly written?
Aggregation COUNT(col) too low Is a non‑null count mistaken for a total row count?
Conditional CASE WHEN col = NULL not working Is the comparison syntax correct?
Grouping NULL and empty string conflated Is the grouping semantics standardized first?

Common Pitfalls

  1. Treating NULL as an empty string — especially common in text columns. They look the same but have different semantics.
  2. No agreed business semantics, so developers write their own defaults — one report treats NULL as "Unknown", another simply excludes it. Without alignment, results diverge.
  3. Ignoring NULLs introduced by LEFT JOIN — right‑side columns become NULL, and downstream filters or groupings unwittingly change the data scope.
  4. Fixing NULLs only at query time, while the raw layer remains messy — if a column persistently contains both NULLs and empty strings, upstream governance is already broken.

Recommendations

  1. Make semantics explicit in SQL: Use CASE WHEN to map NULLs and empty strings to business labels instead of relying on mental assumptions.
  2. Schedule regular NULL‑distribution checks for critical columns — treat it as routine inspection, not a last‑minute fire drill.
  3. Standardize key columns at the detail layer — don't let every downstream query invent its own NULL‑handling logic.

NULL rarely throws an error in a gbase database. It just quietly changes the answer. Separating NULLs, empty strings, and default values before you compare, aggregate, or group is usually the fastest way to close a result‑accuracy debate.