Primary Department for Each Employee - Problem
๐ข Primary Department for Each Employee
You work for a large corporation where employees can belong to multiple departments simultaneously. Each employee must designate exactly one department as their primary department for reporting purposes.
Given an Employee table that tracks department assignments, write a SQL query to find each employee's primary department. Here's the catch: when an employee belongs to only one department, that department is automatically their primary department, even though the flag might be 'N'.
๐ Table Structure: Employee
| Column Name | Type |
|---|---|
| employee_id | int |
| department_id | int |
| primary_flag | varchar |
Key Points:
- ๐ Primary key:
(employee_id, department_id) - ๐
primary_flag: 'Y' = primary department, 'N' = not primary - โ ๏ธ Special case: Employees with only one department have flag = 'N', but that department is still their primary
Goal: Return all employees with their primary department ID, handling both multi-department employees (flag='Y') and single-department employees (automatically primary).
Input & Output
example_1.sql โ Basic Multi-Department Case
$
Input:
Employee table:
+-------------+---------------+--------------+
| employee_id | department_id | primary_flag |
+-------------+---------------+--------------+
| 1 | 1 | N |
| 1 | 2 | Y |
| 2 | 3 | N |
| 3 | 3 | N |
| 3 | 4 | Y |
+-------------+---------------+--------------+
โบ
Output:
+-------------+---------------+
| employee_id | department_id |
+-------------+---------------+
| 1 | 2 |
| 2 | 3 |
| 3 | 4 |
+-------------+---------------+
๐ก Note:
Employee 1 has 2 departments, primary is department 2 (flag='Y'). Employee 2 has only 1 department, so department 3 is primary by default. Employee 3 has 2 departments, primary is department 4 (flag='Y').
example_2.sql โ Single Department Employees
$
Input:
Employee table:
+-------------+---------------+--------------+
| employee_id | department_id | primary_flag |
+-------------+---------------+--------------+
| 10 | 100 | N |
| 20 | 200 | N |
| 30 | 300 | N |
+-------------+---------------+--------------+
โบ
Output:
+-------------+---------------+
| employee_id | department_id |
+-------------+---------------+
| 10 | 100 |
| 20 | 200 |
| 30 | 300 |
+-------------+---------------+
๐ก Note:
All employees have only one department each, so their single departments are automatically their primary departments, even though primary_flag is 'N'.
example_3.sql โ Mixed Case Scenario
$
Input:
Employee table:
+-------------+---------------+--------------+
| employee_id | department_id | primary_flag |
+-------------+---------------+--------------+
| 1 | 10 | Y |
| 1 | 20 | N |
| 1 | 30 | N |
| 2 | 40 | N |
| 3 | 50 | N |
| 3 | 60 | Y |
+-------------+---------------+--------------+
โบ
Output:
+-------------+---------------+
| employee_id | department_id |
+-------------+---------------+
| 1 | 10 |
| 2 | 40 |
| 3 | 60 |
+-------------+---------------+
๐ก Note:
Employee 1 has 3 departments with primary=10. Employee 2 has 1 department, so 40 is primary by default. Employee 3 has 2 departments with primary=60.
Constraints
- 1 โค employee_id โค 104
- 1 โค department_id โค 104
- primary_flag โ {'Y', 'N'}
- Each employee has at least one department
- If an employee has multiple departments, exactly one has primary_flag = 'Y'
Visualization
Tap to expand
Understanding the Visualization
1
Group by Employee
Organize all department assignments by employee ID
2
Count Departments
For each employee, count how many departments they belong to
3
Apply Logic
If count=1, that's the primary. If count>1, find where primary_flag='Y'
4
Extract Results
Return the employee-primary department pairs
Key Takeaway
๐ฏ Key Insight: Window functions enable elegant single-pass solutions by allowing aggregation (counting departments) alongside row-level data access in the same query, eliminating the need for multiple table scans or complex subqueries.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code