w3resource

AdventureWorks Database: Find total cost of each order exceeds 100000

SQL Query - AdventureWorks: Exercise-27 with Solution

27. From the following table write a query in SQL to retrieve the total cost of each salesorderID that exceeds 100000. Return SalesOrderID, total cost.

Sample table: Sales.SalesOrderDetail


Click to view Full table

Sample Solution:

SELECT SalesOrderID, SUM(orderqty*unitprice) AS OrderIDCost  
FROM Sales.SalesOrderDetail  
GROUP BY SalesOrderID  
HAVING SUM(orderqty*unitprice) > 100000.00  
ORDER BY SalesOrderID;

Sample Output:

salesorderid|orderidcost|
------------+-----------+
       43875|122744.1667|
       43884|116248.4897|
       44518|127099.9957|
       44528|108783.5872|
       44530|104960.0101|
       44795|104588.7130|
       46066|100378.9078|
       46067|101857.2130|
       46607|121037.4456|
       46616|153432.0611|
       46643|110830.3608|
       46645|101373.1246|
       46660|119415.1966|
       46981|149533.7021|
       47018|107991.0123|
       47027|105375.2251|
       47355|130185.6757|
	   ...

SQL AdventureWorks Editor:

Practice Online


Contribute your code and comments through Disqus.

Previous: Find sum, avg, OrderQty for specific orders.
Next: Products whose names start with Lock Washer.

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