w3resource

AdventureWorks Database: Fetch product names, salesorderIDs using INNER JOIN

SQL Query - AdventureWorks: Exercise-40 with Solution

40. From the following tables write a SQL query to get all product names and sales order IDs. Order the result set on product name column.

Sample table: Production.Product


Click to view Full table

Sample table: Sales.SalesOrderDetail


Click to view Full table

Sample Solution:

SELECT p.Name, sod.SalesOrderID  
FROM Production.Product AS p  
INNER JOIN Sales.SalesOrderDetail AS sod  
ON p.ProductID = sod.ProductID  
ORDER BY p.Name ;

Sample Output:

name                            |salesorderid|
--------------------------------+------------+
All-Purpose Bike Stand          |       51488|
All-Purpose Bike Stand          |       51558|
All-Purpose Bike Stand          |       51179|
All-Purpose Bike Stand          |       51520|
All-Purpose Bike Stand          |       61953|
All-Purpose Bike Stand          |       53295|
All-Purpose Bike Stand          |       52705|
All-Purpose Bike Stand          |       53299|
All-Purpose Bike Stand          |       52754|
All-Purpose Bike Stand          |       55513|
All-Purpose Bike Stand          |       54397|
All-Purpose Bike Stand          |       54291|
All-Purpose Bike Stand          |       54286|
All-Purpose Bike Stand          |       56866|
...

SQL AdventureWorks Editor:

Practice Online


Contribute your code and comments through Disqus.

Previous: List both ordered and unordered products.
Next: Retrieve the territory name and BusinessEntityID.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

SQL: Tips of the Day

T-SQL Cast versus Convert?

CONVERT is SQL Server specific, CAST is ANSI.

CONVERT is more flexible in that you can format dates etc. Other than that, they are pretty much the same. If you don't care about the extended features, use CAST.

Ref : https://bit.ly/3VKT5M8