I have three tables:
SN | Table Name | Primary Key | Foreign Key | Attribute
-----------------------------------------------------------------
1 | salesArea | salesAreaID | | areaDescription
2 | store | storeID | salesAreaID |
3 | salesPerson | salesPersonID | storeID | salesPersonName
I am trying to get the salesPersonName(s) of anyone that is in the same salesArea as a certain salesPerson.
For instance, a salesPerson named David works in storeID 23, which is in the NE salesArea.
There is another salesPerson named Bob working in storeID 34, which is also in the NE salesArea.
My query so far is…
SELECT salesPersonName,
salesPersonID,
st.salesAreaID,
areaDescription,
sp.storeId
FROM salesperson as sp, salesArea as sa, store as st
I am confused on how to get it get the storeID of ‘David’ and then retrieve what the salesArea is. Then with that salesArea, retrieve all salesPersons with that salesArea.
Salesperson table only gives the storeID, but then store gives the salesArea for the storeIDs.
Point in the right direction would be nice, join clause? some fancy where with a group by?
I am assuming:
So, we first want to find the area ID of the salesperson passed:
Now, we could use the results of this query to then execute the second part of the question, which is “get the other people in a given salesarea”. If, for example, the above query returned SalesAreaID = 23 and SalesPersonID = 10, our second query would look like the following:
But, we can combine the two queries to form a single one that does the same thing:
Et voilà, we have a query giving all other salespersons in a the same area as a passed salesperson.