SQL Interview Questions – Find Total Time Spent by Each Employee

Spread the love

Problem Description –

Write an SQL query to calculate the total time in minutes spent by each employee on each day at the office. Note that within one day, an employee can enter and leave more than once. The time spent in the office for a single entry is out_time - in_time.

Return the result table in any order.

The query result format is in the following example.

Problem Link – Total time spent

Difficulty Level – Easy

Solution –

SELECT
    event_day as day,
    emp_id,
    SUM(out_time) - SUM(in_time) as total_time
FROM Employees 
GROUP BY 1, 2

Rating: 1 out of 5.

Leave a Reply