Employees With Deductions - Problem

Imagine you're working as a payroll manager at a company where employees must meet specific monthly work hour requirements to receive their full salary. Your task is to identify which employees should face salary deductions due to insufficient work hours.

You have access to two key pieces of information:

  • Employee Requirements: Each employee has a minimum number of hours they must work monthly
  • Work Session Logs: Detailed timestamps of when employees clock in and out

Here's the twist: work sessions are rounded up to the nearest minute. So if an employee works 51 minutes and 2 seconds, it counts as 52 minutes. Sessions can even span midnight!

Your Goal: Return the employee_id of all employees who didn't meet their required work hours in October 2022.

Example: If Employee 1 needs 100 hours but only worked 95.5 hours total across all sessions, they should be flagged for deduction.

Input & Output

example_1.sql โ€” Basic Case
$ Input: Employees table: +-------------+--------------+ | employee_id | needed_hours | +-------------+--------------+ | 1 | 20 | | 2 | 12 | | 3 | 2 | +-------------+--------------+ Logs table: +-------------+---------------------+---------------------+ | employee_id | in_time | out_time | +-------------+---------------------+---------------------+ | 1 | 2022-10-01 09:00:00 | 2022-10-01 17:00:00 | | 1 | 2022-10-02 09:00:00 | 2022-10-02 17:00:00 | | 2 | 2022-10-01 12:00:00 | 2022-10-01 17:00:00 | | 3 | 2022-10-01 07:00:00 | 2022-10-01 09:00:00 | +-------------+---------------------+---------------------+
โ€บ Output: +-------------+ | employee_id | +-------------+ | 2 | | 3 | +-------------+
๐Ÿ’ก Note: Employee 1: worked 8+8=16 hours, needs 20 hours. Worked enough (16 >= 20 is false, so deducted). Employee 2: worked 5 hours, needs 12 hours. Not enough (5 < 12). Employee 3: worked 2 hours, needs 2 hours. Exactly enough (2 >= 2 is true, so not deducted). Wait, let me recalculate: Employee 1 worked 16 hours but needs 20, so gets deducted. Employee 2 worked 5 hours but needs 12, so gets deducted. Employee 3 worked 2 hours and needs 2, so no deduction.
example_2.sql โ€” Rounding Up Minutes
$ Input: Employees table: +-------------+--------------+ | employee_id | needed_hours | +-------------+--------------+ | 1 | 1 | +-------------+--------------+ Logs table: +-------------+---------------------+---------------------+ | employee_id | in_time | out_time | +-------------+---------------------+---------------------+ | 1 | 2022-10-01 09:00:00 | 2022-10-01 09:59:30 | +-------------+---------------------+---------------------+
โ€บ Output: +-------------+ | employee_id | +-------------+ +-------------+
๐Ÿ’ก Note: Employee 1 worked for 59 minutes and 30 seconds. Since we round up, this becomes 60 minutes = 1 hour. They needed 1 hour, so 1 >= 1 is true. No deduction needed.
example_3.sql โ€” Employee With No Logs
$ Input: Employees table: +-------------+--------------+ | employee_id | needed_hours | +-------------+--------------+ | 1 | 8 | | 2 | 10 | +-------------+--------------+ Logs table: +-------------+---------------------+---------------------+ | employee_id | in_time | out_time | +-------------+---------------------+---------------------+ | 1 | 2022-10-01 09:00:00 | 2022-10-01 17:00:00 | +-------------+---------------------+---------------------+
โ€บ Output: +-------------+ | employee_id | +-------------+ | 2 | +-------------+
๐Ÿ’ก Note: Employee 1 worked 8 hours and needs 8 hours, so no deduction. Employee 2 has no log entries, so worked 0 hours but needs 10 hours, resulting in deduction.

Constraints

  • 1 โ‰ค employee_id โ‰ค 104
  • 1 โ‰ค needed_hours โ‰ค 50
  • 1 โ‰ค Number of employees โ‰ค 500
  • 1 โ‰ค Number of log entries โ‰ค 104
  • All timestamps are in October 2022
  • out_time can be up to one day after in_time (overnight shifts)
  • Each session duration is at least 1 minute

Visualization

Tap to expand
Employee 1Quota: 40hrsWorked: 42hrsโœ“ SAFEEmployee 2Quota: 35hrsWorked: 28hrsโœ— DEDUCTEmployee 3Quota: 30hrsWorked: 30hrsโœ“ SAFEEmployee 4Quota: 25hrsWorked: 0hrsโœ— DEDUCTTime Card ProcessingSession 1: 8.2hrsSession 2: 7.9hrsSession 3: 8.1hrsSession 4: 7.8hrsTotal: 32.0 hours (all sessions rounded up)Final Result: [2, 4]Employees requiring salary deductions
Understanding the Visualization
1
Collect Time Cards
Gather all employee punch-in/punch-out records for the month
2
Calculate Session Times
For each session, calculate duration and round UP to nearest minute
3
Aggregate by Employee
Sum up all rounded session times for each employee
4
Compare with Quotas
Check each employee's total against their required monthly hours
5
Flag for Deduction
Identify employees who fell short of their requirements
Key Takeaway
๐ŸŽฏ Key Insight: Use SQL aggregation with proper rounding and LEFT JOIN to handle all edge cases (no logs, overnight shifts) in a single efficient query.
Asked in
Amazon 35 Microsoft 28 Google 22 Meta 18
23.7K Views
Medium Frequency
~18 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen