AdventureWorks Database: Calculate the total freight paid by each customer

SQL Query - AdventureWorks: Exercise-7 with Solution

7. From the following table write a query in SQL to calculate the total freight paid by each customer. Return customerid and total freight. Sort the output in ascending order on customerid.

Sample table: sales.salesorderheader

Click to view Full table

Sample Solution:

SELECT customerid,sum(freight) as total_freight 
FROM sales.salesorderheader
group by customerid
ORDER BY customerid ASC;

Sample Output:

     11000|     206.2249|
     11001|     159.5971|
     11002|     202.8511|
     11003|     203.4823|
     11004|     204.9003|
     11005|     203.0334|
     11006|     202.9759|
     11007|     205.2751|
     11008|     202.6578|
     11009|     202.2833|
     11010|     202.2011|
     11011|     203.3261|
     11012|       2.0315|
     11013|       2.8490|
     11014|       3.4613|
     11015|      62.5243|

SQL AdventureWorks Editor:

Practice Online

Contribute your code and comments through Disqus.

Previous: Create a list of unique jobtitles.
Next: Find the average and sum of the subtotal.

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