cswork / libraryOpen practice workspace

Coding questions · Meta

Write SQL for call analytics

This question evaluates a candidate's ability to perform SQL-based data manipulation and analytics, testing skills such as aggregations, joins to user metadata, percentage/rate calculations, and distinct-count reasoning over call records.

You are given two tables.

Table: calls

  • call_id BIGINT
  • sender_id BIGINT
  • receiver_id BIGINT
  • call_ts TIMESTAMP — stored in UTC
  • pickup CHAR(1) — 'Y' if the call was picked up, 'N' otherwise
  • call_type VARCHAR — either 'video' or 'voice'

Assume each row represents one call attempt.

Table: users

  • user_id BIGINT
  • country VARCHAR

Assume calls.sender_id = users.user_id when you need the caller's country. Unless otherwise stated, compute results over the full available dataset.

Write SQL for the following:

  1. Return the total number of calls and the number of picked-up calls for each call_type.

    • Output columns: call_type, total_calls, picked_up_calls
  2. For a given country parameter, compute the percentage of calls placed by senders in that country that were picked up.

    • Output columns: country, pickup_rate_pct
    • Treat pickup_rate_pct as 100.0 * picked_up_calls / total_calls
  3. Compute the percentage of distinct callers who have made at least one video call and at least one voice call.

    • Denominator: all distinct sender_id values in calls
    • Output column: pct_callers_both_types

Write SQL for call analytics

Table calls

columntypeconstraints
call_idBIGINT
sender_idBIGINT
receiver_idBIGINT
call_tsTIMESTAMP
pickupCHAR(1)
call_typeVARCHAR

Sample data:

call_idcall_tscall_typepickupreceiver_idsender_id
12024-01-01 10:00:00videoY31
22024-01-01 10:05:00voiceN41
32024-01-02 12:00:00voiceY12
42024-01-02 12:10:00videoY32
52024-01-03 09:00:00videoN43
62024-01-03 09:05:00voiceN23
72024-01-04 15:00:00voiceY14
82024-01-05 08:00:00videoY25
92024-01-05 08:10:00videoN35
102024-01-05 08:20:00voiceY45

Table users

columntypeconstraints
user_idBIGINT
countryVARCHAR

Sample data:

countryuser_id
US1
US2
CA3
IN4
US5

Expected output

Single unified result set combining: part 1 per call_type counts, part 2 pickup rate for country='US', and part 3 overall percentage of callers who made both call types.

call_typecountrypct_callers_both_typespicked_up_callspickup_rate_pctsectiontotal_calls
videoNULLNULL3NULL15
voiceNULLNULL3NULL15
NULLUSNULLNULL71.432NULL
NULLNULL80NULLNULL3NULL

Hints

  1. Use conditional aggregation with SUM(CASE WHEN pickup='Y' THEN 1 ELSE 0 END) for picked-up counts.
  2. Join calls to users on calls.sender_id = users.user_id when filtering by sender country.
  3. Avoid integer division by multiplying by 100.0 and/or casting counts to numeric before division.
  4. Identify callers who made both types using GROUP BY sender_id HAVING COUNT(DISTINCT call_type) = 2.
  5. If parameters are not supported, emulate a parameter via a CTE (e.g., params AS (SELECT 'US' AS country)).