Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Get started
Dplyr Cohort Aggregation
00:00
5 left

Dplyr Cohort Aggregation

MediumSQL · PostgreSQL

Problem

Given this raw dataset of user interactions, write a dplyr pipeline to aggregate the average session length by user cohort at Betterup.

For this PostgreSQL exercise, write the equivalent SQL query using the provided users and user_interactions tables. Include cohorts without recorded interactions, and ignore missing session lengths when calculating averages.

Output

  1. One row per user cohort, including cohorts without interactions
  2. Columns: cohort, average_session_length
  3. Round the average to two decimal places and sort cohorts ascending, with NULL cohorts last

Schema

users
ColumnTypeDescription
user_idPKINTUnique BetterUp user identifier
cohortVARCHAR(7)User cohort represented as a YYYY-MM value
user_interactions
ColumnTypeDescription
interaction_idPKINTUnique interaction identifier
user_idINTUser associated with the interaction
session_length_minutesINTSession duration in minutes
Tablesusersuser_interactions
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the two tables are in the Question tab.

You need to log in / sign up to run or submit.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.