Leetcode Problem 1789. Primary Department for Each Employee

1789. Primary Department for Each Employee

Leetcode Solutions

Determining Primary Department for Employees

  1. Select employees with primary_flag set to 'Y'.
  2. Group employees by employee_id and select those who appear exactly once (implying they belong to only one department).
  3. Use UNION to combine the results from steps 1 and 2, ensuring no duplicates and that each employee's primary department is represented.
  4. Return the final result set containing employee_id and department_id for the primary department of each employee.

erDiagram
    Employee {
        int employee_id
        int department_id
        varchar primary_flag
        primary_key (employee_id, department_id)
    }

Using Window Functions to Identify Primary Departments

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...