Easiest and Fastest Solution | Complete Beginner Explanation
For this problem, we want to get the total time spent in the office for each employee, for each day. \n\nTo find the total time spent, we need to add up all the
For this problem, we want to get the __total time spent in the office for each employee, for each day__. \n\nTo find the total time spent, we need to add up all the durations where the employee was in the office. We are told to calculate each duration using `out_time - in_time`, and then we can add them up using the `SUM` aggregate function. \n\nAggregate functions in SQL are functions that can be applied over groups of data, so the natural questions is _Over which groups are we applying this function?_ For this problem, we want the sum of durations for each employee, for each day, so we will need to group, or aggregate, our data by `emp_id` and `event_day`. We can do this using the `GROUP BY` clause __after__ our `FROM` and `WHERE` clauses. However, we do not need a `WHERE` clause here because we do not need to filter the data.\n\n\n```\nSELECT\n    event_day AS \'day\',     -- note: we should put the alias "day" in single-quotes, since "day" is a reserved word in MySQL\n    emp_id,\n    SUM(out_time - in_time) AS total_time     -- add up all durations\nFROM employees e\nGROUP BY     -- group the total_time by employee and day. Notice, the order of group by columns doesn\'t matter.\n    event_day, \n    emp_id\n;\n```
['MySQL']