w3resource

AdventureWorks Database: Calculate the percentage of tax on subtotal

SQL Query - AdventureWorks: Exercise-5 with Solution

5. From the following table write a query in SQL to return all rows from the salesorderheader table in Adventureworks database and calculate the percentage of tax on the subtotal have decided. Return salesorderid, customerid, orderdate, subtotal, percentage of tax column. Arranged the result set in ascending order on subtotal.

Sample table: sales.salesorderheader


Click to view Full table

Sample Solution:

SELECT salesorderid,customerid,orderdate,subtotal,
(taxamt*100)/subtotal AS Tax_percent
FROM sales.salesorderheader
ORDER BY subtotal desc;

Sample Output:

salesorderid|customerid|orderdate              |subtotal   |tax_percent        |
------------+----------+-----------------------+-----------+-------------------+
       51131|     29641|2013-05-30 00:00:00.000|163930.3943|10.9488656308319512|
       55282|     29641|2013-08-30 00:00:00.000|160378.3913|10.2805612815745958|
       46616|     29614|2012-05-30 00:00:00.000|150837.4387| 9.9382830477616695|
       46981|     30103|2012-06-30 00:00:00.000|147390.9328| 9.8971768635146327|
       47395|     29701|2012-07-31 00:00:00.000|146154.5653| 9.8391246078989227|
       47369|     29998|2012-07-31 00:00:00.000|140078.3959| 9.7785509407021986|
       47355|     29957|2012-07-31 00:00:00.000| 129261.254| 9.7141627606366870|
       51822|     29913|2013-06-30 00:00:00.000|128873.2206| 9.8028539530422816|
       44518|     29624|2011-10-01 00:00:00.000|126198.3362| 9.7285313496866847|
       57150|     29923|2013-09-30 00:00:00.000| 122285.724| 9.6171868762047809|
       51858|     29940|2013-06-30 00:00:00.000|122284.4578|11.0640782511610400|
       43875|     29624|2011-07-01 00:00:00.000|121761.9396| 9.7497652706576957|
       46607|     29994|2012-05-30 00:00:00.000| 120182.185| 9.7784823932099421|
       46660|     29646|2012-05-30 00:00:00.000|117274.3453|10.0397911153378232|
	   ...

SQL AdventureWorks Editor:

Practice Online


Contribute your code and comments through Disqus.

Previous: Retrieve only the rows for product.
Next: Create a list of unique jobtitles.

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