SQL Interview Questions – Number of Calls Between Two Persons

Spread the love

Problem Description –

Write an SQL query to report the number of calls and the total call duration between each pair of distinct persons (person1, person2) where person1 < person2.

Return the result table in any order.

The query result format is in the following example.

Problem Link – Calls between two persons

Difficulty Level – Medium

Solution –

SELECT
    LEAST(from_id, to_id) as person1,
    GREATEST(from_id, to_id) as person2,
    COUNT(*) as call_count,
    SUM(duration) as total_duration
FROM Calls
GROUP BY 1, 2

Rating: 1 out of 5.

Leave a Reply