Let’s say I have three tables:
LOCATIONS – a store can belong to multiple locations and a location can have multiple stores
+-------------+----------+ | LOCATION_ID | STORE_ID | +-------------+----------+
STORES – only one row per store, every store has only one manager ID
+----------+------------+ | STORE_ID | MANAGER_ID | +----------+------------+
EMPLOYEES – a manager can have multiple employees, and employees can belong to more than one manager
+-------------+-------------+ | MANAGER_ID | EMPLOYEE_ID | +-------------+-------------+
And for a given location (e.g. LOCATION_ID = 999), I want to get all the employees managed at stores at that location.
This gets me the list of managers that belong to stores in that location:
SELECT s.MANAGER_ID FROM LOCATIONS l
INNER JOIN STORES s
ON s.STORE_ID = l.STORE_ID
WHERE l.LOCATION_ID = 999;
What I actually want is ALL the distinct EMPLOYEE_IDs that are linked to the managers that query spits out.
What additional join can I add in the same query to get that?
1 Answer