Knowledge Guide
HomeDatabasesSQL Practice Problems

easy Main Office Assignment for Each Employee

Problem Statement

Table: OfficeAssignment
This table records the office assignments of employees. Each record includes an employee ID, office ID, and a main office flag indicating whether the office is the employee's main office.

+---------------+----------+
| Column Name   | Type     | 
+---------------+----------+
| employee_id   | int      |
| office_id     | int      |
| main_flag     | varchar  |
+---------------+----------+
(employee_id, office_id) is the primary key for this table.
employee_id is the ID of the employee.
office_id is the ID of the office assigned to the employee.
main_flag is an ENUM type of ('Y', 'N'). If the flag is 'Y', the office is the employee's main office. If the flag is 'N', the office is not the main one.

Employees can be assigned to multiple offices, but they can designate only one office as their main office. If an employee is assigned to only one office, that office is automatically considered their main office, regardless of the main flag.

Develop a solution to report all employees along with their main office. For employees assigned to only one office, report that office as their main office.

Return the result table in the order of employee_id.

Example

Input:

OfficeAssignment table: +-------------+-----------+----------+ | employee_id | office_id | main_flag| +-------------+-----------+----------+ | 1 | 101 | N | | 2 | 101 | Y | | 2 | 102 | N | | 3 | 103 | N | | 4 | 102 | N | | 4 | 103 | Y | | 4 | 104 | N | +-------------+-----------+----------+

Output:

+-------------+-----------+ | employee_id | office_id | +-------------+-----------+ | 1 | 101 | | 2 | 101 | | 3 | 103 | | 4 | 103 | +-------------+-----------+

Try It Yourself

java
-- TODO: Write your user queries here

Solution

To determine the main office for each employee from the OfficeAssignment table, we can follow a systematic approach that prioritizes the main office flag and handles cases where employees are assigned to only one office.

Approach Overview

SQL Query

SELECT employee_id, office_id FROM ( SELECT employee_id, office_id, ROW_NUMBER() OVER(PARTITION BY employee_id ORDER BY main_flag DESC) as rn FROM OfficeAssignment ) AS T WHERE rn = 1 ORDER BY employee_id;

Step-by-Step Approach

Step 1: Determine Main Office

Identify the main office for each employee by prioritizing offices marked with 'Y' in the main_flag. For employees with only one office assignment, that office is automatically considered the main office.

SQL Query:

SELECT employee_id, office_id FROM ( SELECT employee_id, office_id, ROW_NUMBER() OVER(PARTITION BY employee_id ORDER BY main_flag DESC) as rn FROM OfficeAssignment ) AS T WHERE rn = 1;

Explanation:

Output After Step 2:

+-------------+-----------+ | employee_id | office_id | +-------------+-----------+ | 1 | 101 | | 2 | 101 | | 3 | 103 | | 4 | 103 | +-------------+-----------+

Step 2: Order Results by Employee ID

Sort the identified main offices in ascending order based on employee_id to produce the final output.

SQL Query:

SELECT employee_id, office_id FROM ( SELECT employee_id, office_id, ROW_NUMBER() OVER(PARTITION BY employee_id ORDER BY main_flag DESC) as rn FROM OfficeAssignment ) AS T WHERE rn = 1 ORDER BY employee_id;

Explanation:

Final Output:

+-------------+-----------+ | employee_id | office_id | +-------------+-----------+ | 1 | 101 | | 2 | 101 | | 3 | 103 | | 4 | 103 | +-------------+-----------+
🤖 Don't fully get this? Learn it with Claude

Stuck on Main Office Assignment for Each Employee? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🪜 Hint ladder (no spoilers)

Progressively stronger hints — you still solve it.

I'm working on the problem **Main Office Assignment for Each Employee** (Databases). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🎨 Explain the approach visually

See the technique, not just code.

Explain the optimal approach to **Main Office Assignment for Each Employee** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔍 Review my solution

Catch bugs, edge cases, sub-optimality.

I'll paste my solution to **Main Office Assignment for Each Employee**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔁 Drill the pattern

Lock in recognition with look-alikes.

Give me 2 problems that use the SAME underlying pattern as **Main Office Assignment for Each Employee**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes