Compute average call duration per user per day, skipping incomplete calls

Quick Overview

Write a SQL query that returns each user's average call duration in seconds for each day, ignoring calls with a missing start or end time. It tests timestamp subtraction, grouping by the date part of a timestamp, and how NULL filtering changes which groups appear in the result.

Compute average call duration per user per day, skipping incomplete calls

Company: Twilio

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Each row of `call_records` is one phone call placed by a user. Return the average call duration in seconds for each user on each day. A call's duration is the number of seconds between `call_start` and `call_end`, and its day is the date portion of `call_start`. Calls where either timestamp is missing are ignored. ### Input Table(s) Table `call_records` | Column | Type | Meaning | |---|---|---| | call_id | INT | One id per call (primary key) | | user_id | INT | User who placed the call, NOT NULL | | call_start | TIMESTAMP | When the call started. Can be NULL | | call_end | TIMESTAMP | When the call ended. Can be NULL | Primary key: `call_id`. ### Output Contract One row per (user, day) that has at least one complete call, with these columns in this order: - `user_id` - `call_day`: the date of `call_start`, as a DATE. - `avg_duration_seconds`: the average duration in seconds of that user's complete calls on that day, rounded to 2 decimal places. Order by `user_id` ascending, then `call_day` ascending. Each (`user_id`, `call_day`) pair appears once, so this order is unique. ### Example `call_records` | call_id | user_id | call_start | call_end | |---|---|---|---| | 1 | 1 | 2024-03-01 09:00:00 | 2024-03-01 09:02:00 | | 2 | 1 | 2024-03-01 14:30:00 | 2024-03-01 14:30:45 | | 3 | 1 | 2024-03-01 23:59:30 | 2024-03-02 00:00:40 | | 4 | 1 | 2024-03-02 08:00:00 | NULL | | 5 | 2 | 2024-03-01 10:00:00 | 2024-03-01 10:05:00 | | 6 | 2 | NULL | 2024-03-01 11:00:00 | | 7 | 2 | 2024-03-02 16:00:00 | 2024-03-02 16:00:10 | Expected result: | user_id | call_day | avg_duration_seconds | |---|---|---| | 1 | 2024-03-01 | 78.33 | | 2 | 2024-03-01 | 300.00 | | 2 | 2024-03-02 | 10.00 | User 1's calls on 2024-03-01 last 120, 45 and 70 seconds, and 235 / 3 = 78.33. Call 3 ends after midnight but counts on 2024-03-01, the date it started. User 1 has no row for 2024-03-02 because call 4 has no end time, and call 6 is dropped because it has no start time. ### Constraints and Clarifications - The table holds between 1 and 100,000 rows. - `call_end >= call_start` whenever both are present. - A call with a NULL `call_start` or a NULL `call_end` is ignored entirely. A user-day whose calls are all incomplete does not appear in the output. - `call_id` is unique, so there are no duplicate rows. Two separate calls with identical timestamps both count. - All timestamps share one time zone and carry whole seconds. ```hint Which groups survive `AVG` already skips NULL values. Think about which (user, day) groups would still show up in the result if incomplete calls were left in. ```

Overview: Write a SQL query that returns each user's average call duration in seconds for each day, ignoring calls with a missing start or end time. It tests timestamp subtraction, grouping by the date part of a timestamp, and how NULL filtering changes which groups appear in the result.

Twilio logo
Twilio
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Each row of call_records is one phone call placed by a user. Return the average call duration in seconds for each user on each day. A call's duration is the number of seconds between call_start and call_end, and its day is the date portion of call_start. Calls where either timestamp is missing are ignored.

Input Table(s)

Table call_records

ColumnTypeMeaning
call_idINTOne id per call (primary key)
user_idINTUser who placed the call, NOT NULL
call_startTIMESTAMPWhen the call started. Can be NULL
call_endTIMESTAMPWhen the call ended. Can be NULL

Primary key: call_id.

Output Contract

One row per (user, day) that has at least one complete call, with these columns in this order:

  • user_id
  • call_day : the date of call_start , as a DATE.
  • avg_duration_seconds : the average duration in seconds of that user's complete calls on that day, rounded to 2 decimal places.

Order by user_id ascending, then call_day ascending. Each (user_id, call_day) pair appears once, so this order is unique.

Example

call_records

call_iduser_idcall_startcall_end
112024-03-01 09:00:002024-03-01 09:02:00
212024-03-01 14:30:002024-03-01 14:30:45
312024-03-01 23:59:302024-03-02 00:00:40
412024-03-02 08:00:00NULL
522024-03-01 10:00:002024-03-01 10:05:00
62NULL2024-03-01 11:00:00
722024-03-02 16:00:002024-03-02 16:00:10

Expected result:

user_idcall_dayavg_duration_seconds
12024-03-0178.33
22024-03-01300.00
22024-03-0210.00

User 1's calls on 2024-03-01 last 120, 45 and 70 seconds, and 235 / 3 = 78.33. Call 3 ends after midnight but counts on 2024-03-01, the date it started. User 1 has no row for 2024-03-02 because call 4 has no end time, and call 6 is dropped because it has no start time.

Constraints and Clarifications

  • The table holds between 1 and 100,000 rows.
  • call_end >= call_start whenever both are present.
  • A call with a NULL call_start or a NULL call_end is ignored entirely. A user-day whose calls are all incomplete does not appear in the output.
  • call_id is unique, so there are no duplicate rows. Two separate calls with identical timestamps both count.
  • All timestamps share one time zone and carry whole seconds.