Division by zero SQL Origin: Data Last updated 13 Oct 2026

SQL Error: division by zero / Divide by zero error encountered

An expression divided by a value that was 0 for at least one row.

Exact message you may see division by zeroDivide by zero error encounteredDivision by 0
Seen on: PostgreSQL MySQL SQL

Meaning

PostgreSQL and SQL Server raise an error; MySQL returns NULL (or errors in strict mode with ERROR_FOR_DIVISION_BY_ZERO). Ratios, averages and percentages over empty groups are the usual sources.

Common causes

  • Ratio with a zero denominator (no orders, no visits)
  • Average/percentage computed for empty groups
  • Data with unexpected zeros

⚡ Quick fix

  1. Wrap the denominator with NULLIF(x, 0)
  2. Use CASE to return 0 or NULL explicitly
  3. Filter out zero rows when they aren’t meaningful

Detailed fix by platform

SQL

  1. NULLIF guard:
    sql
    SELECT campaign, clicks * 100.0 / NULLIF(impressions, 0) AS ctr_percent
    FROM campaign_stats;

How to diagnose

  1. Expression — Which division?
  2. Rows — Which rows have a 0 denominator?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Data. Errors that are often confused with or caused by this one (click to open):

DivisionByZeroError — Fatal error: Uncaught DivisionByZeroError: Division by zero (PHP)DivisionByZeroErrorGROUP BY — Column must appear in the GROUP BY clause / isn't in GROUP BY (MySQL 1055 only_full_group_by) (SQL)GROUP BYTypeError: NoneType — TypeError: 'NoneType' object is not subscriptable (Python)TypeError: NoneTypeZeroDivisionError — ZeroDivisionError: division by zero (Python)ZeroDivisionErrorDivision …

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.