Leetcode Problem 2394. Employees With Deductions

2394. Employees With Deductions

Leetcode Solutions

Calculating Work Hours and Identifying Employees with Insufficient Hours

  1. Join the Employees table with the Logs table using a LEFT JOIN to include all employees, even those without any logs.
  2. Group the results by employee_id to prepare for aggregation.
  3. Use the TIMESTAMPDIFF function to calculate the difference in seconds between in_time and out_time for each log entry.
  4. Apply the CEILING function to round up the time difference to the nearest minute.
  5. Sum the rounded minutes for each employee to get the total minutes worked.
  6. Convert the total minutes to hours by dividing by 60.
  7. Use the IFNULL function to handle cases where there are no log entries for an employee, defaulting to 0.
  8. Compare the total hours worked to the needed_hours for each employee.
  9. Use the HAVING clause to filter out employees who have met or exceeded the required hours.
  10. Select the employee_id of employees who have not worked the required hours.
erDiagram
    Employees {
        int employee_id
        int needed_hours
    }
    Logs {
        int employee_id
        datetime in_time
        datetime out_time
    }
    Employees ||--o{ Logs : "has"

Aggregating Worked Minutes and Comparing with Required Hours

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...