The Find Total Time Spent by Each Employee interview question is a SQL aggregation task. You are given a Employees table that logs every entry and exit of an employee, including the day, their ID, and the time they checked in and out. Your task is to calculate the total time each employee spent in the office on each unique day.
Companies like Amazon and Google use the Find Total Time Spent coding problem to assess basic knowledge of Grouping and Multi-column Aggregation. It tests whether you can group data by two distinct keys (day and employee) and perform arithmetic on columns within an aggregate function (SUM(out - in)).
This problem follows the Multi-group Aggregation pattern.
event_day and emp_id.out_time - in_time.total_time).Log:
(Day 1, 1, 25), (Day 1, 2, 50).GROUP BY clause.Always look at the "unique keys" in the requested output. If the output asks for "each employee on each day," that tells you exactly what columns need to be in your GROUP BY statement. This is a fundamental Database interview pattern.