MySQL Solution small optimization beats ~96
\nRuntime: 401 ms, faster than 96.35% of MySQL online submissions for Find Total Time Spent by Each Employee.\nMemory Usage: 0B, less than 100.00% of MySQL onli
```\nRuntime: 401 ms, faster than 96.35% of MySQL online submissions for Find Total Time Spent by Each Employee.\nMemory Usage: 0B, less than 100.00% of MySQL online submissions for Find Total Time Spent by Each Employee.\n```\nSame Operation, Different Orders.\n\n**Approach-1:**  Simplest way comes to mind. Get the time for each entry and then sum it by the Grouping of id and date.\nHowever, Here we are calculating n row for subtractions first. [Which is little bit more intensive]. Column summation is optimized by most of the Query Engine. :  [[Refer](https://people.brandeis.edu/~nga/papers/VLDB05.pdf)].\n\n```\nSELECT event_day AS day, emp_id, SUM(out_time-in_time) AS total_time FROM Employees\nGROUP by event_day,emp_id\n```\n\n**Approach-2:** Here we are summing all the Column values by grouping first so at the end we will have 1 entry per (emp_id,date) rather than k (emp_id,date) duration. This little optimization of summing first results faster than 96% solution. [I have ran it for 5 time to get the idea of averaging out. and why it behaves like this.]\n[To get the sum of the column the computer only needs to go to one disk and sum all the values inside of it. No extra memory needs to be pulled in, and it accesses a minimal number of disks.] [Refer](https://dataschool.com/data-modeling-101/row-vs-column-oriented-databases/)\n```\nRuntime: 562 ms, faster than 36.75% of MySQL online submissions for Find Total Time Spent by Each Employee.\n```\n```\nSELECT event_day AS day, emp_id, (SUM(out_time)-SUM(in_time)) AS total_time FROM Employees\nGROUP by event_day,emp_id\n```
[]