← SQL workplace practice

Statistical reporting contracts · Original fictional data · Free files and full answers

Compute a weighted operational mean

For each team ignore NULL values and their weights. Return team,weighted_mean rounded to two decimals, using SUM(value*weight)/SUM(weight) over known values. Sort team.

Use this fixed business case to build a checked CSV and a replayable SQL handoff. No account required. Scenario datasets are shared across related exercises; each exercise has its own decision and output contract.

Your output contract

  1. For each team ignore NULL values and their weights. Return team,weighted_mean rounded to two decimals, using SUM(value*weight)/SUM(weight) over known values. Sort team.
  2. Return exactly the declared columns and order. Money uses integer cents; all time values are elapsed UTC minutes unless a field is explicitly a day index. NULL is unknown and differs from numeric zero.
  3. These are original fictional records and exercise policies. All source tables in this case are included in the downloadable pack.

Return columns exactly as team, weighted_mean. Sort order follows the task. Output has 3 rows; an empty CSV field means NULL.

Source data

observations

16 source rows · Download observations CSV

id INTEGERteam TEXTday INTEGERvalue INTEGERweight INTEGER
1A1101
2A2202
3A3NULL1
4A4401
5A51001
6A6203
7B152
8B251
9B3151
10B4252
11B5351
12B6551
13C101
14C2101
15C3302
16C4101
Field dictionary and schema
observations:
  id INTEGER — Stable record or business key; retain its declared type.
  team TEXT — Literal text label or key; comparisons use SQLite binary text semantics.
  day INTEGER — Exact integer measurement; use the task rule for units and ordering.
  value INTEGER — Exact integer measurement; use the task rule for units and ordering.
  weight INTEGER — Exact integer measurement; use the task rule for units and ordering.

Download complete SQLite setup

A method you can reuse

  1. For each team ignore NULL values and their weights. Return team,weighted_mean rounded to two decimals, using SUM(value*weight)/SUM(weight) over known values. Sort team.
  2. Inspect the source grain, keys and ties before writing the query. Use the worked SQL and complete expected CSV to review any mismatch.

Watch for this wrong turn

AVG(value) gives every row equal influence and ignores the declared weighting.

Run and check in SQLite

Your SQL runs locally in the existing SQLite worker. The checker compares every column, row, type, value and row order with this fixed output. It accepts equivalent SELECT or WITH queries. Browser storage keeps your SQL on this device when available.

Complete worked SQL and expected output
SELECT team,ROUND(1.0*SUM(value*weight)/SUM(CASE WHEN value IS NOT NULL THEN weight END),2) weighted_mean FROM observations GROUP BY team ORDER BY team;
"team","weighted_mean"
"A","31.25"
"B","21.25"
"C","16"
Reference SQL · Complete expected CSV · Empty answer CSV

Choose another SQL task · Original data exception cases · Larger workplace projects