Percentages and Rates in SQL
Rates are everywhere: conversion, completion, usage. Each is a numerator over a denominator, scaled to a percentage.
A rate is a fraction with a story: how many of the whole match some condition. The shape is always numerator ÷ denominator × 100. The numerator is a conditional count; the denominator is the total.
One thing to watch in some databases: dividing two integers can throw away
the fraction (so
17 / 24
becomes
0). Writing the multiplier as
100.0
keeps the pattern portable, and
round()
tidies the result.
select round(100.0 * converted / total, 1) as conversion_pct
from funnel Once you have a rate per group, averaging those rates doesn't give the overall rate: see why averaging averages gives the wrong result.
Schema · Garden Shop Table · orders 6 columns · 24 rows
One row per order. Unshipped orders have a null shipped_date.
What share of orders have shipped? Extend the starter so it also returns
pct_shipped: the percentage of all orders whose status is
'shipped', rounded to one decimal place.
Run your query to inspect the result.
Expected answer
- Columns: total_orders, shipped_orders, pct_shipped.
- Rows: 1 summary row.
- 17 of 24 orders shipped, so pct_shipped is 70.8.
Related
Nested SELECTs, IN, EXISTS, and correlation.
Ranking, LAG/LEAD, and running totals at a glance.
Practice ranking, running totals, and top row per group.
The default RANGE frame groups tied rows; use ROWS.
Filter window results in a CTE or with QUALIFY.
The WITH clause, chained steps, and CTE vs subquery.