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 NameType
employee_idint
department_idint
primary_flagvarchar

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
๐Ÿข Company Employee Department SystemEach employee can have multiple department badges, but only one primary badgeEMP 1Multi-DeptA(N)B(Y)EMP 2Single-DeptC(N)*EMP 3Multi-DeptD(Y)E(N)๐Ÿ” Window Function LogicCOUNT(*) OVER (PARTITION BY employee_id)IF count = 1 OR primary_flag = 'Y' THEN primarySingle Query โœจ O(n) Performance๐Ÿ“‹ Final Results(Emp 1, Dept B)(Emp 2, Dept C*)(Emp 3, Dept D)*Auto-primary (single department case)
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.
Asked in
Amazon 45 Microsoft 38 Google 32 Meta 28
58.2K Views
High Frequency
~15 min Avg. Time
1.8K 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