SQL Division

The / operator divides one value by another — but integer-divided-by-integer truncates the decimal in Postgres, a genuinely common surprise.

What & Why

Plain / divides. The behavior depends on the types involved: dividing two integer values performs integer division, truncating any decimal remainder. Dividing when at least one side is a decimal type gives a proper fractional result.

See How It Works

BUSINESS QUESTION

Marketing wants click-through rate for every email send.

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,
  ROUND(clicks * 1.0 / NULLIF(recipients, 0), 4) AS click_through_rate
FROM marketing.email_sends
ORDER BY send_id;
RESULT — exact output from the displayed Queryflo rows
send_idclick_through_rate
2010.1500
2020.1200
2030.1500
204NULL

The three non-zero sends return their exact click-through rates.

Now You Try

Practice this concept

Marketing wants click-through rate for every email send.

Available schema
marketing

Prefix tables with marketing.table_name.

send_idclick_through_rate
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