Learn SQL/Intermediate/Numeric Functions/SQL Percentage Calculations

SQL Percentage Calculations

Part divided by whole, times 100 — the reusable formula behind open rate, click rate, conversion rate, and percent-of-total questions.

What & Why

Every "what percentage" question reduces to the same formula: (part / whole) * 100. The genuinely easy way to get this wrong is forgetting to force decimal division — dividing two integers first (as the earlier Division lesson covered) truncates before the percentage math even happens.

See How It Works

BUSINESS QUESTION

Marketing wants open and click rates 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
Watch a safe percentage calculationExample 1 of 4
clicks::numeric / NULLIF(recipients, 0) * 100
idrecipientsopensclicksopen_rate_pctclick_rate_pct
201120054018045.00%15.00%
20280042096??
2031500610225??
204000??

Both rates use Send 201's real recipient count.

EXAMPLE QUERY
SELECT
  id AS send_id,
  ROUND(opens::numeric / NULLIF(recipients, 0) * 100, 2) AS open_rate_pct,
  ROUND(clicks::numeric / NULLIF(recipients, 0) * 100, 2) AS click_rate_pct
FROM marketing.email_sends
ORDER BY send_id;
Now You Try

Practice this concept

Marketing wants safe open and click percentages for every email send.

Available schema
marketing

Prefix tables with marketing.table_name.

send_idopen_rate_pctclick_rate_pct
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