SQL NULL vs Zero

Zero is a genuine, known numeric value. NULL is the absence of any value at all — the two are not interchangeable, even though both might look 'empty' on a report.

What & Why

An opens value of 0 means the send was measured and produced no opens. An opens value of NULL means no open count is recorded. Those facts are different even if a report renders both as an empty-looking cell.

See How It Works

BUSINESS QUESTION

Marketing labels each email send by whether its open count is missing, zero, or positive.

idcampaign_idsent_atrecipientsopensclicksunsubscribesbounces
20112024-01-20 15:00:00+001200540180924
20222024-02-15 16:30:00+0080042096512
20332024-03-18 13:00:00+0015006102251431
20442024-04-08 14:00:00+0000000
EXAMPLE QUERY
SELECT
  id AS send_id,
  opens,
  CASE
    WHEN opens IS NULL THEN 'missing'
    WHEN opens = 0 THEN 'zero'
    ELSE 'positive'
  END AS open_state
FROM marketing.email_sends
ORDER BY send_id;
RESULT — exact output from the displayed Queryflo rows
send_idopensopen_state
201540positive
202420positive
203610positive
2040zero

The fixture contains a measured zero but no missing opens value.

Now You Try

Practice this concept

Marketing wants each email send labeled missing, zero, or positive from its opens value.

Available schema
marketing

Prefix tables with marketing.table_name.

send_idopensopen_state
marketing.email_sends
ColumnType
idinteger
campaign_idinteger
sent_attimestamp with time zone
recipientsinteger
opensinteger
clicksinteger
unsubscribesinteger
bouncesinteger
legacy_send_grouptext
query.sql
Intermediate business practice

Sign up free to try it on a real business scenario