SQL QuestSQL Interview Questions › NULL Handling

Fare Imputation Analysis

MediumFreeQuerying BasicsNULL HandlingAggregation & GroupingConditional LogicSubqueries & CTEs

Analyse Titanic fare data quality by class. For each pclass show: passengers_with_fare, passengers_missing_fare, avg_fare_known (actual average, NULLs excluded), and avg_fare_imputed (NULLs replaced with the overall fare average across all classes before computing). The two columns reveal what happens when you impute with the global mean — classes with below-average fares see their averages rise, classes with above-average fares see them fall. Order by pclass ascending.

Solve it in the browser editor →

Runs on SQLite in your browser, graded against the expected result, no signup. A wrong answer gets a diagnosis, not just "incorrect".

Schema

passengers

passenger_idsurvivedpclassnamesexagesibspparchfareembarked
103Braund, Mr. Owen Harrismale22107.25S
211Cumings, Mrs. John Bradleyfemale381071.28C
313Heikkinen, Miss. Lainafemale26007.93S

Expected output: Per-class fare completeness with raw vs globally-imputed averages

Hint

For avg_fare_imputed, use AVG(COALESCE(fare, (SELECT AVG(fare) FROM passengers WHERE fare IS NOT NULL))) — a non-correlated subquery returns the single overall fare mean, which you substitute for NULL fares. Note: if you impute with the CLASS mean instead of the overall mean, avg_fare_imputed will equal avg_fare_known by definition (adding the mean back into the mean doesn't change the mean).

Concepts

SELECT NULL Handling COALESCE GROUP BY Aggregation CASE Subquery

Practise the topic: SQL practice questions · NULL handling practice · GROUP BY exercises · CASE WHEN practice · CTE practice

In these company practice sets

Plaid · Snowflake

A SQL Quest challenge matched to patterns reported for these companies — not a question any of them has published.

Related questions

Genre Box Office ReportMedium · FreeLEFT JOIN NULL Semantics: Inactive CustomersMedium · FreeManagement Hierarchy OverviewMedium · FreeHandle NULL AgesMedium · FreeMembership Tier Revenue AnalysisMedium · FreeSenior Passengers by PortMedium · Free

Where would this cost you points in an interview?

Ten questions, no signup: a Skillmap across nine SQL skills and the one to fix first.

Take the readiness test