SQL ROUND
Rounds a decimal number to a specified number of decimal places — the standard way to clean up a long, ugly fraction for display.
What & Why
ROUND(n, decimals) rounds to the given number of decimal places. Omitting the second argument rounds to the nearest whole number.
See How It Works
BUSINESS QUESTION
Marketing wants click rates rounded to two decimals and recipient estimates rounded in both directions.
| id | campaign_id | sent_at | recipients | opens | clicks | unsubscribes | bounces |
|---|---|---|---|---|---|---|---|
| 201 | 1 | 2024-01-20 15:00:00+00 | 1200 | 540 | 180 | 9 | 24 |
| 202 | 2 | 2024-02-15 16:30:00+00 | 800 | 420 | 96 | 5 | 12 |
| 203 | 3 | 2024-03-18 13:00:00+00 | 1500 | 610 | 225 | 14 | 31 |
| 204 | 4 | 2024-04-08 14:00:00+00 | 0 | 0 | 0 | 0 | 0 |
EXAMPLE QUERY
SELECT
id AS send_id,
ROUND(clicks::numeric / NULLIF(recipients, 0) * 100, 2) AS click_rate_pct,
CEIL(recipients * 0.10) AS audience_ceiling,
FLOOR(recipients * 0.10) AS audience_floor
FROM marketing.email_sends
ORDER BY send_id;RESULT — exact output from the displayed Queryflo rows
| send_id | click_rate_pct | audience_ceiling | audience_floor |
|---|---|---|---|
| 201 | 15.00 | 120 | 120 |
| 202 | 12.00 | 80 | 80 |
| 203 | 15.00 | 150 | 150 |
| 204 | NULL | 0 | 0 |
ROUND, CEIL, and FLOOR operate on the displayed email-send values.
Now You Try
Practice this concept
Marketing wants click rate rounded to two decimals and ten-percent audience estimates rounded upward and downward.
Available schema
marketingPrefix tables with marketing.table_name.
send_idclick_rate_pctaudience_ceilingaudience_floormarketing.email_sends| Column | Type |
|---|---|
| id | integer |
| campaign_id | integer |
| sent_at | timestamp with time zone |
| recipients | integer |
| opens | integer |
| clicks | integer |
| unsubscribes | integer |
| bounces | integer |
| legacy_send_group | text |
query.sql
Sign up free to try it on a real business scenario