Knowledge Guide
HomeDatabasesSQL Practice Problems

Average Time of Process

Problem

Table: Activity

+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| machine_id     | int     |
| process_id     | int     |
| activity_type  | enum    |
| timestamp      | float   |
+----------------+---------+
The table shows the user activities for a factory website.
(machine_id, process_id, activity_type) is the primary key (combination of columns with unique values) of this table.
machine_id is the ID of a machine.
process_id is the ID of a process running on the machine with ID machine_id.
activity_type is an ENUM (category) of type ('start', 'end').
timestamp is a float representing the current time in seconds.
'start' means the machine starts the process at the given timestamp and 'end' means the machine ends the process at the given timestamp.
The 'start' timestamp will always be before the 'end' timestamp for every (machine_id, process_id) pair.

Problem Definition

There is a factory website that has several machines, each running the same number of processes. Write a solution to find the average time each machine takes to complete a process.

The time to complete a process is the 'end' timestamp minus the 'start' timestamp. The average time is calculated by the total time to complete every process on the machine divided by the number of processes that were run.

The resulting table should have the machine_id along with the average time as processing_time, which should be rounded to 3 decimal places.

Return the result table in any order.

Example

Image
Image

Output

Image
Image

Try It Yourself

java
-- TODO: Write your user queries here

Solution

To solve this problem, the approach involves using SQL queries to calculate the average time each machine takes to complete a process based on the given Activity table. The table contains information about machine activities, including the machine ID, process ID, activity type ('start' or 'end'), and a timestamp indicating when the activity occurred.

The solution utilizes the JOIN clause to combine the Activity table (a1) with itself (a2). The ON clause specifies the conditions for joining, including that both activities should have the same machine_id, process_id, and the first activity (a1) should be of type 'start', while the second activity (a2) should be of type 'end'. This ensures that the start and end activities for the same process on the same machine are paired together.

The calculation of the time taken to complete a process is achieved by subtracting the timestamp of the 'start' activity from the timestamp of the 'end' activity. The Round function is then applied to round the average processing time to three decimal places.

The results are grouped by machine_id using the GROUP BY clause, ensuring that the calculations are performed for each unique machine. The final query provides a clear and organized result table with machine_id and the calculated average processing time.

SELECT a1.machine_id, Round(Avg(a2.timestamp - a1.timestamp), 3) AS processing_time FROM Activity a1 JOIN Activity a2 ON a1.machine_id = a2.machine_id AND a1.process_id = a2.process_id AND a1.activity_type = 'start' AND a2.activity_type = 'end' GROUP BY a1.machine_id

Let's break down the query step by step:

Step 1: Joining the Activity table with itself

We are joining the Activity table with itself, aliased as a1 and a2, to match the start and end activities for each (machine_id, process_id) pair.

SELECT a1.machine_id, Round(Avg(a2.timestamp - a1.timestamp), 3) AS processing_time FROM Activity a1 JOIN Activity a2 ON a1.machine_id = a2.machine_id AND a1.process_id = a2.process_id AND a1.activity_type = 'start' AND a2.activity_type = 'end'

Output After Step 1:

+------------+-----------------+ | machine_id | processing_time | +------------+-----------------+ | 0 | ... | | 1 | ... | | 2 | ... | +------------+-----------------+

Step 2: Grouping by machine_id

We group the results by machine_id to calculate the average processing time for each machine.

GROUP BY a1.machine_id

Output After Step 2:

+------------+-------------------+ | machine_id | processing_time | +------------+-------------------+ | 0 | 0.89478 | | 1 | 0.99538 | | 2 | 1.45656 | +------------+-------------------+

Now, let's calculate the average processing time for each machine_id:

Step 3: Calculate average processing time

We use the AVG function to calculate the average processing time, and ROUND to round it to 3 decimal places.

Round(Avg(a2.timestamp - a1.timestamp), 3) AS processing_time

Final Output:

+------------+-----------------+ | machine_id | processing_time | +------------+-----------------+ | 0 | 0.894 | | 1 | 0.995 | | 2 | 1.456 | +------------+-----------------+
🤖 Don't fully get this? Learn it with Claude

Stuck on Average Time of Process? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Average Time of Process** (Databases) and want to truly understand it. Explain Average Time of Process from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Average Time of Process** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Average Time of Process** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Average Time of Process** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes