Amazon: Non-Managers Across Multiple Departments
SQL coding challenge · Difficulty: medium · Topic: Joins · +100 XP
Problem
*Amazon-style interview practice.*
Topic: Joins · anti-join (NOT EXISTS / NOT IN / left anti join) · COUNT(DISTINCT ...) · GROUP BY / HAVING
Amazon's HR analytics team wants to find individual contributors with cross-team experience: employees who have never been a manager of anyone and who have worked in more than one department.
Write a query that returns those employees.
- Each row is one department assignment. An employee who moved between departments appears once for every department they worked in, with the manager they reported to there.
- An employee has been a manager if their
employee_idappears as themanager_idof any row, in any department. manager_idis NULL for employees with no manager (the top of the hierarchy).- An employee worked in more than one department if their rows contain at least two different
department_idvalues. Two rows with the same department count once. - Return each qualifying employee once, with their
employee_idandname.
Return the columns employee_id and name, one row per employee (the grader checks the rows, not their order).
Schema
Table: employees
| Column | Type | Meaning | | --- | --- | --- | | employee_id | INT | The employee (repeats once per department assignment) | | name | VARCHAR | The employee's name | | manager_id | INT | The `employee_id` of the manager in this assignment; NULL if none | | department_id | INT | The department of this assignment |
Example 1
Input: employees
| employee_id | name | manager_id | department_id | | --- | --- | --- | --- | | 1 | Asha | NULL | 10 | | 2 | Ravi | 1 | 10 | | 2 | Ravi | 1 | 20 | | 3 | Meera | 2 | 10 | | 3 | Meera | 1 | 30 | | 4 | Karan | 2 | 20 | | 4 | Karan | 2 | 20 | | 5 | Neha | 2 | 20 | | 6 | Vikram | 1 | 30 | | 6 | Vikram | 1 | 40 | | 7 | Pooja | 6 | 40 | | 8 | Imran | 6 | 30 | | 8 | Imran | 2 | 20 |
Output:
| employee_id | name | | --- | --- | | 3 | Meera | | 8 | Imran |
Explanation:
- Asha (1) has no manager (
manager_idis NULL) and is the manager of Ravi, Meera and Vikram: excluded. - Ravi (2) worked in departments 10 and 20, but Meera, Karan, Neha and Imran report to him: excluded.
- Meera (3) worked in departments 10 and 30 and nobody reports to her: included.
- Karan (4) has two rows, but both are department 20: only one department, excluded.
- Neha (5) and Pooja (7) each worked in one department: excluded.
- Vikram (6) worked in departments 30 and 40, but Pooja and Imran report to him: excluded.
- Imran (8) worked in departments 30 and 20 and nobody reports to him: included.
Constraints
employee_id,department_idare never NULL;manager_idmay be NULL.- An employee's
nameis the same on all of their rows. Two different employees may share a name, so identify people byemployee_id. - A
manager_idalways refers to anemployee_id(it may belong to someone who has since left the table). - Watch the NULLs:
x NOT IN (subquery)returns no rows at all when the subquery contains a NULL.NOT EXISTS, or removing the NULLs first, avoids it. - The query runs on MySQL 8.
What this SQL challenge teaches you
“Amazon: Non-Managers Across Multiple Departments” is a medium-level SQL challenge focused on Joins. Working through it gives you hands-on practice with Anti-join, NOT EXISTS, NOT IN and NULL, COUNT DISTINCT, HAVING, Self join — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.
Concepts covered
- Anti-join
- NOT EXISTS
- NOT IN and NULL
- COUNT DISTINCT
- HAVING
- Self join
- HR analytics
How to approach it
If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:
- Start with the managers: SELECT DISTINCT manager_id FROM employees. Who is NOT in that list?
- NOT IN against a list that contains NULL returns nothing. Add WHERE manager_id IS NOT NULL, or use NOT EXISTS.
- An employee has one row per department assignment: GROUP BY employee_id, name.
- HAVING COUNT(DISTINCT department_id) > 1 counts different departments, not rows.
Interview practice
This problem is good practice for data engineering interviews at companies such as Amazon. Interviewers use problems like it to check whether you can express the logic cleanly and reason about correctness on edge cases such as ties, nulls and empty groups.
How to practise it on PySpark.in
Open the challenge, write your SQL query in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.
Related SQL challenges
- Find Customers Missing from New Table
- Customers With at Least One Order
- Customers Who Have Never Placed an Order
- Count Orders Per Customer Including Zero
- Total Amount Spent Per Customer (With Zero)
- Find Employees in the HR Department
Helpful resources
Frequently asked questions
Do I need to install Spark or a database to solve this?
No. The SQL environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.
Is this challenge free?
Yes - the problem, the sample dataset, the hints and unlimited test runs are free.
What level is it?
It is rated medium and covers Joins.