AdventureWorks Database: Retrieve the territory name and BusinessEntityID

SQL Query - AdventureWorks: Exercise-41 with Solution

41. From the following tables write a SQL query to retrieve the territory name and BusinessEntityID. The result set includes all salespeople, regardless of whether or not they are assigned a territory.

Sample table: Sales.SalesTerritory

Click to view Full table

Sample table: Sales.SalesPerson

Click to view Full table

Sample Solution:

SELECT st.Name AS Territory, sp.BusinessEntityID  
FROM Sales.SalesTerritory AS st   
RIGHT OUTER JOIN Sales.SalesPerson AS sp  
ON st.TerritoryID = sp.TerritoryID ;

Sample Output:

territory     |businessentityid|
              |             274|
Northeast     |             275|
Southwest     |             276|
Central       |             277|
Canada        |             278|
Southeast     |             279|
Northwest     |             280|
Southwest     |             281|
Canada        |             282|
Northwest     |             283|
Northwest     |             284|
              |             285|
Australia     |             286|
              |             287|
Germany       |             288|
United Kingdom|             289|
France        |             290|

SQL AdventureWorks Editor:

Practice Online

Contribute your code and comments through Disqus.

Previous: Fetch product names, salesorderIDs using INNER JOIN.
Next: Retrieve name and city of the employees.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Follow us on Facebook and Twitter for latest update.