SQL Playground
TutorialsPlayground
SQL FormatterSQL MinifierSyntax ValidatorJSON to SQLCSV to SQLSQL to JSONER Diagram Generator
Daily ChallengeInterviewsCheat SheetBlog

© 2026 SQL Playground. Built for developers.

PrivacyTermsAboutRSS Feed
SQL Playground
TutorialsPlayground
SQL FormatterSQL MinifierSyntax ValidatorJSON to SQLCSV to SQLSQL to JSONER Diagram Generator
Daily ChallengeInterviewsCheat SheetBlog
Back to Blog
2026-01-31
2 min read

Calculating Weighted Averages in SQL

sqlanalyticsmathreporting

The standard AVG() function in SQL is great, but it treats every row equally. In the real world, some data points matter more than others.

If you buy 1 share of stock at $100 and 100 shares at $200, your average purchase price isn't $150. It's much closer to $200. This is where Weighted Averages come in.

Weighted Average Concept
Weighted Average Concept

The Formula

The formula for a weighted average is:

Weighted Average = Sum(Value * Weight) / Sum(Weight)

In SQL, this translates to:

SUM(value_column * weight_column) / SUM(weight_column)

Real-World Example: Product Reviews

Imagine you want to compare the quality of two products based on user ratings.

  • Product A: 100 reviews, average rating 4.5
  • Product B: 5 reviews, average rating 5.0

Volume Matters: Reviews Example
Volume Matters: Reviews Example

A simple average of the "average ratings" would be misleading if we were aggregating categories. But let's look at a portfolio example where this is clearer.

Investment Portfolio

We have a table of investments with share_price and quantity.

stockshare_pricequantity
APPL150.0010
GOOG2800.001
MSFT300.005

If we run AVG(share_price), we get: (150 + 2800 + 300) / 3 = 1083.33. But that's meaningless because we only own 1 share of the expensive Google stock!

Let's calculate the true weighted average price of our portfolio.

Interactive SQL
Loading...

Handling Division by Zero

One edge case to watch out for is when the sum of your weights is 0. This will cause a division by zero error.

You can handle this with NULLIF:

SELECT 
    SUM(value * weight) / NULLIF(SUM(weight), 0)
FROM table_name;

Summary

When analyzing data where volume, frequency, or importance varies, don't settle for AVG(). The Weighted Average gives you the true center of mass for your data.

Remember the pattern: SUM(val * weight) / SUM(weight)!

Share this article:

Related Articles

sqlreporting

SQL Conditional Aggregation: Beyond Basic GROUP BY

Turn multiple queries into one with conditional aggregation. Learn how CASE WHEN inside aggregate functions creates powerful single-query reports and pivot tables.

Read more
sqlanalytics

Mastering ROLLUP, CUBE, and GROUPING SETS in SQL

Stop running multiple queries for subtotals. Learn how to use advanced GROUP BY extensions to generate powerful reports in a single pass.

Read more
sqlanalytics

Time Series Analysis with SQL: Trends, Growth, and Moving Averages

Turn raw timestamps into business insights. Learn how to calculate Month-over-Month growth and smooth out noisy data with 7-day moving averages.

Read more
Previous

Mastering SQL Set Operations: UNION, INTERSECT, and EXCEPT

Next

SQL Window Frames: ROWS vs RANGE

Comments

© 2026 SQL Playground. Built for developers.

PrivacyTermsAboutRSS Feed