Calculate average revenue per user from sessions

Quick Overview

Write SQL to compute average revenue grouped by user from sessions, handling nulls and duplicates.

Walmart
Data Manipulation (SQL/Python)
Data Scientist
Walmart
Data Scientist
Take-home Project
Data Manipulation (SQL/Python)
Easy

2


Write SQL to compute average revenue grouped by user from sessions, handling nulls and duplicates.

Walmart data science take-homes often start with a basic revenue rollup over session or order data before moving to harder analysis. The interviewer is checking whether you define the metric precisely and treat NULL revenue and duplicate session rows deliberately rather than by accident.

What the Interviewer Expects
  • Clarify whether NULL revenue means zero spend or missing data before filtering it out
  • Deduplicate repeated session rows (for example on session_id) before aggregating
  • Compute per-user average revenue with GROUP BY user_id and AVG
  • Explain how AVG ignores NULLs and how that changes the denominator
  • Sanity-check results against row counts and a few hand-computed users
Key Topics to Cover
SQL aggregation
NULL handling
Deduplication
GROUP BY
Revenue metrics
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
  • How would the answer change if NULL revenue should count as a zero-revenue session?
  • How would you compute average revenue per user across all users, including those with no sessions?
  • How would you deduplicate if the same session appears with different revenue values?
  • How would you make this query efficient on billions of session rows?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice SQL Problems
Sample Answer
Problem Understanding

The goal is to calculate the average revenue per user from session data. The relevant data involves two main entities: sessions and users. Each session has a corresponding user ID and a revenue amount...

Approach
  1. Select the user ID and revenue from the sessions table.
  2. Filter out any records where revenue is NULL to avoid skewing the average.
  3. Group the data by user ID to aggregate the revenue.
    4....

Submit Your Answer
Markdown supported

Related Questions