Integer Division
in SQL Metrics
Why dividing two integers silently truncates your results, and how to cast your way to accuracy across every SQL dialect.
What is Integer Division?
In most programming languages and many SQL dialects, when you divide two integers, the engine performs integer division: it divides and then truncates the decimal portion, returning only the whole number.
Why It Matters for Metrics
SQL metrics often compute ratios, rates, and averages from integer columns: counts, sums, and totals. Integer division silently corrupts these calculations.
The Type System Behind It
SQL engines follow type promotion rules when evaluating expressions. The result type of a division depends on the types of its operands.
The Core Rule
Dialect Differences
Not all SQL dialects handle integer division the same way. This is one of the most common sources of bugs when migrating queries between databases.
| Dialect | \( 7 / 2 \) Result | Behavior |
|---|---|---|
| PostgreSQL | 3 | Integer division (truncates) |
| MySQL | 3.5000 | Auto-promotes to decimal |
| SQL Server | 3 | Integer division (truncates) |
| SQLite | 3 | Integer division (truncates) |
| BigQuery | 3.5 | Auto-promotes to FLOAT64 |
| Snowflake | 3.5 | Auto-promotes to NUMBER |
Truncation vs. Rounding
Integer division truncates (chops off the decimal). It does not round to the nearest integer. This distinction matters because the error is always in one direction.
\( -7 / 2 = -3 \) (toward zero)
\( \text{ROUND}(-3.5) = -4 \)
How to Fix It: Step by Step
Three Casting Techniques
Standard SQL. Works everywhere. Most explicit and readable.
Quick shorthand. Promotes n to decimal implicitly.
PostgreSQL shorthand. Concise but not portable.
Example 1: Conversion Rate
A marketing team wants the purchase conversion rate from a table with purchases (INT) and visits (INT).
Example 2: Average with Rounding
A school wants average test scores. Both total_points (INT) and num_students (INT) are integers.
Example 3: Safe Percentage with CASE
Computing a percentage metric that also guards against division by zero: a common companion bug.
The Multiplication Order Trap
A subtle variant: multiplying by 100 before vs. after division changes whether integer truncation strikes.
Aggregate Functions and Division
Functions like SUM() and COUNT() return integers. Dividing their results inherits the same integer division trap.
Common Mistakes
Summary: Fix Patterns
| Pattern | Buggy | Fixed |
|---|---|---|
| Simple ratio | \( a / b \) | \( \text{CAST}(a \text{ AS FLOAT}) / b \) |
| Percentage | \( (a / b) \times 100 \) | \( \text{CAST}(a \text{ AS FLOAT}) / b \times 100 \) |
| Quick cast | \( a / b \) | \( a \times 1.0 / b \) |
| PostgreSQL | \( a / b \) | \( a\text{::FLOAT} / b \) |
| Aggregate | \( \text{SUM}(x) / \text{COUNT}(*) \) | \( \text{CAST}(\text{SUM}(x) \text{ AS FLOAT}) / \text{COUNT}(*) \) |
| Late cast | \( \text{CAST}(a/b \text{ AS FLOAT}) \) | \( \text{CAST}(a \text{ AS FLOAT}) / b \) |
Question 1 of 5
In PostgreSQL, what does SELECT 5 / 2; return?
Question 2 of 5
Which of these fixes integer division correctly?
Question 3 of 5
A query computes SUM(revenue) / COUNT(*) in SQL Server. Both return INT. What does this expression compute?
Question 4 of 5
Which SQL dialect does NOT perform integer division on 7 / 2?
Question 5 of 5
Given passed = 3 and total = 10 (both INT), what does (passed / total) * 100 return in PostgreSQL?