Skip to content

Practice Primary Department for Each Employee

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#1789
LeetCode Problem

Primary Department for Each Employee

Problem Statement

Tables: Employee(employee_id INT, department_id INT, primary_flag VARCHAR). Employee has key (employee_id, department_id). primary_flag uses Y and N. An employee belonging to only one department has flag N. Return employee_id and their primary department_id. Choose the Y row for an employee with several departments, or the sole department when the employee has only one. Output order is unrestricted.

Source: Primary Department for Each Employee on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
Employee table: +-------------+---------------+--------------+ | employee_id | department_id | primary_flag | +-------------+---------------+--------------+ | 1           | 1             | N            | | 2           | 1             | Y            | | 2           | 2             | N            | | 3           | 3             | N            | | 4           | 2             | N            | | 4           | 3             | Y            | | 4           | 4             | N            | +-------------+---------------+--------------+
Output
+-------------+---------------+ | employee_id | department_id | +-------------+---------------+ | 1           | 1             | | 2           | 1             | | 3           | 3             | | 4           | 3             | +-------------+---------------+

Related LeetCode Problems

Practice more problems similar to Primary Department for Each Employee.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.