SQL
All Top 50 SQL Interview Questions#18
Calculate Process Duration with Activity Pair Joins in SQL
MediumInterview Question #18
Given an `activity` table (machine_id, process_id, activity_type, timestamp), find the average processing time for each `machine_id` rounded to 3 decimal places.
Input Table: activity
4 rows preview| machine_id | process_id | activity_type | timestamp |
|---|---|---|---|
| 0 | 0 | start | 0.712 |
| 0 | 0 | end | 1.52 |
| 0 | 1 | start | 3.14 |
| 0 | 1 | end | 4.12 |
Expected Output Structure1 rows
| machine_id | processing_time |
|---|---|
| 0 | 0.894 |
Interview Context
Asked frequently in data analyst and business analyst technical rounds. Focus on clean filtering, optimal indexing usage, and unambiguous column selection.
PostgreSQL 15
Ready to execute
Click "Run Query" or press Ctrl + Enter to test your query.
Output will be verified against the test dataset.