w3resource

AdventureWorks Database: List both ordered and unordered products

SQL Query - AdventureWorks: Exercise-39 with Solution

39. From the following table write a SQL query to retrieve the product name and salesorderid. Both ordered and unordered products are included in the result set.

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  
LEFT OUTER JOIN Sales.SalesOrderDetail AS sod  
ON p.ProductID = sod.ProductID  
ORDER BY p.Name ;

Sample Output:

name                            |salesorderid|
--------------------------------+------------+
Adjustable Race                 |            |
All-Purpose Bike Stand          |       73803|
All-Purpose Bike Stand          |       74760|
All-Purpose Bike Stand          |       74086|
All-Purpose Bike Stand          |       74310|
All-Purpose Bike Stand          |       74845|
All-Purpose Bike Stand          |       74580|
All-Purpose Bike Stand          |       74516|
All-Purpose Bike Stand          |       75123|
All-Purpose Bike Stand          |       74525|
All-Purpose Bike Stand          |       74130|
All-Purpose Bike Stand          |       74960|
All-Purpose Bike Stand          |       74852|
All-Purpose Bike Stand          |       74879|
...

SQL AdventureWorks Editor:

Practice Online


Contribute your code and comments through Disqus.

Previous: Retrieve name and associated salesorders.
Next: Fetch product names, salesorderIDs using INNER JOIN.

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