w3resource

AdventureWorks Database: Retrieve name and associated salesorders

SQL Query - AdventureWorks: Exercise-38 with Solution

38. Create a SQL query from the SalesOrderDetail table to retrieve the product name and any associated sales orders. Additionally, it returns any sales orders that don't have any items mentioned in the Product table as well as any products that have sales orders other than those that are listed there. Return product name, salesorderid. Sort 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  
FULL 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: List all the products that are red or blue.
Next: List both ordered and unordered products.

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