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.

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:

Constraints

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

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:

  1. Start with the managers: SELECT DISTINCT manager_id FROM employees. Who is NOT in that list?
  2. NOT IN against a list that contains NULL returns nothing. Add WHERE manager_id IS NOT NULL, or use NOT EXISTS.
  3. An employee has one row per department assignment: GROUP BY employee_id, name.
  4. 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

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.

Solve this challenge free on PySpark.in