Return the Marked Primary Department or an Employee’s Sole Department

Quick Overview

Find the primary department for each employee and keep the only department for single-department employees.

Return the Marked Primary Department or an Employee’s Sole Department

Company: Amazon

Role: Software Engineer

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

### Problem Return employee_id and department_id for each employee’s primary department. Employees in several departments have exactly one row marked Y. Return that department; for an employee in exactly one department, return that sole department even though its flag is N. Row order is unrestricted. ### Tables Table: `Employee` ```text +---------------+---------+ | Column Name | Type | +---------------+---------+ | employee_id | int | | department_id | int | | primary_flag | varchar | +---------------+---------+ (employee_id, department_id) is the primary key (combination of columns with unique values) for this table. employee_id is the id of the employee. department_id is the id of the department to which the employee belongs. primary_flag is an ENUM (category) of type ('Y', 'N'). If the flag is 'Y', the department is the primary department for the employee. If the flag is 'N', the department is not the primary. ``` ### Example ```text Employee (employee_id,department_id,primary_flag): (1,8,N), (2,8,N), (2,9,Y), (3,7,Y), (3,8,N) Output (employee_id,department_id): (1,8), (2,9), (3,7) ``` ### What a Strong Answer Covers - Return the marked primary department for employees with multiple assignments. - Return the sole department for a single assignment and keep the requested columns. ### Follow-up Questions - How would you flag employees whose department records have no primary assignment? - How would you include the number of departments for each employee?

Overview: Find the primary department for each employee and keep the only department for single-department employees.

Amazon logo
Amazon
easySoftware EngineerOtherData Manipulation (SQL/Python)
0
0

Problem

Return employee_id and department_id for each employee’s primary department. Employees in several departments have exactly one row marked Y. Return that department; for an employee in exactly one department, return that sole department even though its flag is N. Row order is unrestricted.

Tables

Table: Employee

+---------------+---------+
| Column Name   |  Type   |
+---------------+---------+
| employee_id   | int     |
| department_id | int     |
| primary_flag  | varchar |
+---------------+---------+
(employee_id, department_id) is the primary key (combination of columns with unique values) for this table.
employee_id is the id of the employee.
department_id is the id of the department to which the employee belongs.
primary_flag is an ENUM (category) of type ('Y', 'N'). If the flag is 'Y', the department is the primary department for the employee. If the flag is 'N', the department is not the primary.

Example

Employee (employee_id,department_id,primary_flag): (1,8,N), (2,8,N), (2,9,Y), (3,7,Y), (3,8,N)
Output (employee_id,department_id): (1,8), (2,9), (3,7)

What a Strong Answer Covers Guidance

  • Return the marked primary department for employees with multiple assignments.
  • Return the sole department for a single assignment and keep the requested columns.

Follow-up Questions Guidance

  • How would you flag employees whose department records have no primary assignment?
  • How would you include the number of departments for each employee?