Find Longest Calls - Problem
Call Duration Analysis Challenge
You're working as a data analyst for a telecommunications company and need to identify the longest call records for quality analysis purposes.
Given two database tables:
•
•
Your task is to find the three longest calls for each call type (incoming and outgoing). The results should be:
• Ordered by type (descending), duration (descending), then first_name (descending)
• Duration formatted as
• Include contact names from the Contacts table
This problem tests your ability to work with JOINs, window functions, and data formatting in SQL.
You're working as a data analyst for a telecommunications company and need to identify the longest call records for quality analysis purposes.
Given two database tables:
•
Contacts: Contains customer information (id, first_name, last_name)•
Calls: Contains call records (contact_id, type, duration in seconds)Your task is to find the three longest calls for each call type (incoming and outgoing). The results should be:
• Ordered by type (descending), duration (descending), then first_name (descending)
• Duration formatted as
HH:MM:SS• Include contact names from the Contacts table
This problem tests your ability to work with JOINs, window functions, and data formatting in SQL.
Input & Output
example_1.sql — Basic Dataset
$
Input:
Contacts: [(1,'John','Doe'), (2,'Jane','Smith'), (3,'Bob','Johnson')]
Calls: [(1,'incoming',3600), (1,'outgoing',7200), (2,'incoming',1800), (2,'outgoing',5400), (3,'incoming',2700)]
›
Output:
| first_name | last_name | type | duration |
|------------|-----------|----------|----------|
| John | Doe | outgoing | 01:30:00 |
| Jane | Smith | outgoing | 01:30:00 |
| John | Doe | incoming | 01:00:00 |
| Bob | Johnson | incoming | 00:45:00 |
| Jane | Smith | incoming | 00:30:00 |
💡 Note:
Returns top 3 calls per type, ordered by type DESC (outgoing first), then duration DESC, then first_name DESC. Since there are only 2 outgoing calls, both are included.
example_2.sql — Equal Duration Handling
$
Input:
Contacts: [(1,'Alice','Brown'), (2,'Charlie','Davis'), (3,'Eve','Wilson')]
Calls: [(1,'incoming',3600), (2,'incoming',3600), (3,'incoming',3600), (1,'outgoing',1800)]
›
Output:
| first_name | last_name | type | duration |
|------------|-----------|----------|----------|
| Alice | Brown | outgoing | 00:30:00 |
| Eve | Wilson | incoming | 01:00:00 |
| Charlie | Davis | incoming | 01:00:00 |
| Alice | Brown | incoming | 01:00:00 |
💡 Note:
When durations are equal, ordering by first_name DESC breaks ties. Eve > Charlie > Alice alphabetically in descending order.
example_3.sql — Large Dataset Edge Case
$
Input:
Contacts: 5 contacts with IDs 1-5
Calls: 10+ calls per type with varying durations
›
Output:
| first_name | last_name | type | duration |
|------------|-----------|----------|----------|
| Contact5 | Last5 | outgoing | 02:30:15 |
| Contact3 | Last3 | outgoing | 02:15:30 |
| Contact1 | Last1 | outgoing | 01:45:20 |
| Contact4 | Last4 | incoming | 03:20:45 |
| Contact2 | Last2 | incoming | 02:55:10 |
| Contact5 | Last5 | incoming | 02:10:35 |
💡 Note:
Even with many calls, only the top 3 longest calls from each type are returned, demonstrating the LIMIT 3 per partition functionality.
Constraints
- 1 ≤ number of contacts ≤ 1000
- 1 ≤ number of calls ≤ 104
- Duration is always positive integer (seconds)
- Each contact has at least one call record
- Call type is always 'incoming' or 'outgoing'
Visualization
Tap to expand
Understanding the Visualization
1
Join the Data
Combine customer info with call records like merging contact book with call history
2
Create Partitions
Separate incoming and outgoing calls into distinct groups for independent ranking
3
Rank Within Groups
Assign row numbers based on duration (longest first) and name (Z to A for ties)
4
Select Top 3
Filter to keep only ranks 1, 2, and 3 from each call type partition
Key Takeaway
🎯 Key Insight: Window functions eliminate the need for complex subqueries by allowing us to rank data within partitions in a single, efficient pass through the dataset.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code