In SQL, geometric mean over the sample table is calculated with the query below. On the sample data it returns ₹600.08.
Geometric mean = (x₁ × x₂ × … × xₙ)^(1/n) = exp(average of ln x)
Step by step
- Load daily.csv into a table called daily (day, ad_spend, website_orders, app_orders, avg_basket).
- Run the query and compare with the expected result.
CREATE TABLE daily (
day INTEGER,
ad_spend INTEGER,
website_orders INTEGER,
app_orders INTEGER,
avg_basket INTEGER
);The query
SELECT ROUND(EXP(AVG(LN(avg_basket))), 2) AS geometric_mean
FROM daily;Expected result on the sample data: ₹600.08.
What the result means
Applied to the daily baskets it is slightly below the arithmetic mean, as it always is unless every value is the same. The gap grows with the spread of the values. For growth, take the geometric mean of the growth factors: monthly factors of 1.10 and 0.90 have a geometric mean just under 1, which correctly says the business shrank slightly.
Notes for SQL
- Tested on DuckDB. The same functions exist in PostgreSQL. SQLite and MySQL lack most statistical aggregates; compute in Python or load the data into DuckDB, which can query a CSV file directly.
- Statistical aggregates ignore NULL rows. Decide whether a missing day should be NULL (left out) or 0 (counted as a zero day).
Where geometric mean misleads
- It only works for positive numbers. For growth rates, average the growth factors (1.10, 0.90) and subtract 1 at the end.
- Computing the product directly can overflow; the logarithm form avoids that.
- It is not a replacement for the mean when you need a total.
The plain-language explanation is on the Geometric mean page.
The data
| day | avg_basket |
|---|---|
| 1 | 612 |
| 2 | 598 |
| 3 | 640 |
| 4 | 587 |
| 5 | 605 |
| 6 | 622 |
| 7 | 579 |
| 8 | 601 |
| 9 | 633 |
| 10 | 590 |
| 11 | 615 |
| 12 | 608 |
| 13 | 584 |
| 14 | 597 |
| 15 | 626 |
| 16 | 593 |
| 17 | 548 |
| 18 | 611 |
| 19 | 589 |
| 20 | 603 |
| 21 | 620 |
| 22 | 586 |
| 23 | 599 |
| 24 | 614 |
| 25 | 581 |
| 26 | 595 |
| 27 | 607 |
| 28 | 576 |
| 29 | 602 |
| 30 | 590 |
Same method, other tools
Related methods
Learn more about SQL
- Python and SQL for data analysis, side by side — 3 min read
- Data analysis methods and techniques, and when to use each — 3 min read
About the data on this page
The figures come from a small made-up dataset, printed above in full so you can check every number by hand. Download it as daily.csv. The results are computed from those rows when the page is built, and the Excel formulas, SQL and Python on these pages are run against the same rows before publishing, so the code and the answers agree.
To get these numbers from your own data without writing formulas, try AnalyzeIt: ask in plain words and it shows the working behind every figure.
Legal entity
Devansh Yadav
Moradabad, Uttar Pradesh
Governing law: Courts at Moradabad, Uttar Pradesh
Phone: +91 8433475698
Legal: legal@analyzeit.in