Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ Useful for preparing for technical interviews and improving your SQL skills.
- [3580. Find Consistently Improving Employees](./leetcode/medium/3580.%20Find%20Consistently%20Improving%20Employees.sql)
- [3586. Find COVID Recovery Patients](./leetcode/medium/3586.%20Find%20COVID%20Recovery%20Patients.sql)
- [3601. Find Drivers with Improved Fuel Efficiency](./leetcode/medium/3601.%20Find%20Drivers%20with%20Improved%20Fuel%20Efficiency.sql)
- [3611. Find Overbooked Employees](./leetcode/medium/3611.%20Find%20Overbooked%20Employees.sql)
3. [Hard](./leetcode/hard/)
- [185. Department Top Three Salaries](./leetcode/hard/185.%20Department%20Top%20Three%20Salaries.sql)
- [262. Trips and Users](./leetcode/hard/262.%20Trips%20and%20Users.sql)
Expand Down
60 changes: 60 additions & 0 deletions leetcode/medium/3611. Find Overbooked Employees.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Question 3611. Find Overbooked Employees
Link: https://leetcode.com/problems/find-overbooked-employees/description/?envType=problem-list-v2&envId=database

Table: employees

+---------------+---------+
| Column Name | Type |
+---------------+---------+
| employee_id | int |
| employee_name | varchar |
| department | varchar |
+---------------+---------+
employee_id is the unique identifier for this table.
Each row contains information about an employee and their department.
Table: meetings

+---------------+---------+
| Column Name | Type |
+---------------+---------+
| meeting_id | int |
| employee_id | int |
| meeting_date | date |
| meeting_type | varchar |
| duration_hours| decimal |
+---------------+---------+
meeting_id is the unique identifier for this table.
Each row represents a meeting attended by an employee. meeting_type can be 'Team', 'Client', or 'Training'.
Write a solution to find employees who are meeting-heavy - employees who spend more than 50% of their working time in meetings during any given week.

Assume a standard work week is 40 hours
Calculate total meeting hours per employee per week (Monday to Sunday)
An employee is meeting-heavy if their weekly meeting hours > 20 hours (50% of 40 hours)
Count how many weeks each employee was meeting-heavy
Only include employees who were meeting-heavy for at least 2 weeks
Return the result table ordered by the number of meeting-heavy weeks in descending order, then by employee name in ascending order.
*/

WITH weeks_stats AS (
SELECT
employee_id,
EXTRACT(WEEK FROM meeting_date) AS week, --noqa: RF04
SUM(duration_hours) AS meet
FROM meetings
GROUP BY employee_id, week
HAVING SUM(duration_hours) > 20
)

SELECT
e.employee_id,
e.employee_name,
e.department,
COUNT(e.employee_id) AS meeting_heavy_weeks
FROM weeks_stats AS ws
LEFT JOIN
employees AS e
ON ws.employee_id = e.employee_id
GROUP BY e.employee_id, e.employee_name, e.department
HAVING COUNT(e.employee_id) >= 2
ORDER BY meeting_heavy_weeks DESC, e.employee_name ASC