Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
SQL Total Minutes by User
00:00
5 left

SQL Total Minutes by User

MediumSQL · PostgreSQL

Problem

Write a query that accepts two date and time inputs and returns total minutes by active user, producing a result set like the sample provided with the given assumptions.

Treat the interval as half-open, including the start and excluding the end. Open sessions remain active through the requested end time. Rows with a NULL started_at are ignored, and users with no qualifying activity are excluded.

Output

  1. One row per active user with user_id, user_name, and total_minutes.
  2. Count each minute only once, even when that user's sessions overlap or touch.
  3. Order by total_minutes descending, then user_id ascending.

Schema

users
ColumnTypeDescription
user_idPKINTUnique user identifier
user_nameVARCHAR(100)Display name of the user
user_sessions
ColumnTypeDescription
session_idPKINTUnique session identifier
user_idINTUser associated with the session
started_atTIMESTAMPSession start timestamp
ended_atTIMESTAMPSession end timestamp; NULL means the session is open
session_labelVARCHAR(50)Source label for the session
Tablesusersuser_sessions
Interviewer

Your question is SQL Total Minutes by User. Start with the requirements and the two tables in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

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