w3resource

AdventureWorks Database: Orders with orderqtys, unitpricediscount, totaldues

SQL Query - AdventureWorks: Exercise-51 with Solution

51. From the following tables write a SQL query to retrieve the orders with orderqtys greater than 5 or unitpricediscount less than 1000, and totaldues greater than 100. Return all the columns from the tables.

Sample table: Sales.SalesOrderHeader


Click to view Full table

Sample table: Sales.SalesOrderDetail


Click to view Full table

Sample Solution:

SELECT *  
FROM Sales.SalesOrderHeader AS h  
INNER JOIN Sales.SalesOrderDetail AS d 
    ON h.SalesOrderID = d.SalesOrderID   
WHERE h.TotalDue > 100  
AND (d.OrderQty > 5 OR d.unitpricediscount < 1000.00);

Sample Output:

salesorderid|revisionnumber|orderdate              |duedate                |shipdate               |status|onlineorderflag|purchaseordernumber|accountnumber |customerid|salespersonid|territoryid|billtoaddressid|shiptoaddressid|shipmethodid|creditcardid|creditcardapprovalcode|currencyrateid|subtotal  |taxamt   |freight  |totaldue  |comment|rowguid                             |modifieddate           |salesorderid|salesorderdetailid|carriertrackingnumber|orderqty|productid|specialofferid|unitprice|unitpricediscount|rowguid                             |modifieddate           |
------------+--------------+-----------------------+-----------------------+-----------------------+------+---------------+-------------------+--------------+----------+-------------+-----------+---------------+---------------+------------+------------+----------------------+--------------+----------+---------+---------+----------+-------+------------------------------------+-----------------------+------------+------------------+---------------------+--------+---------+--------------+---------+-----------------+------------------------------------+-----------------------+
       43662|             8|2011-05-31 00:00:00.000|2011-06-12 00:00:00.000|2011-06-07 00:00:00.000|     5|false          |PO18444174044      |10-4020-000227|     29994|          282|          6|            482|            482|           5|       10456|125295Vi53935         |             4|28832.5289|2775.1646| 867.2389|32474.9324|       |4a1ecfc0-cc3a-4740-b028-1c50bb48711c|2011-06-07 00:00:00.000|       43662|                30|2E53-4802-85         |       3|      764|             1| 419.4589|                0|2dfd4d76-e020-4b80-a0a5-6463d0c2df73|2011-05-31 00:00:00.000|
       43662|             8|2011-05-31 00:00:00.000|2011-06-12 00:00:00.000|2011-06-07 00:00:00.000|     5|false          |PO18444174044      |10-4020-000227|     29994|          282|          6|            482|            482|           5|       10456|125295Vi53935         |             4|28832.5289|2775.1646| 867.2389|32474.9324|       |4a1ecfc0-cc3a-4740-b028-1c50bb48711c|2011-06-07 00:00:00.000|       43662|                31|2E53-4802-85         |       5|      770|             1| 419.4589|                0|1f1aa6dc-04a2-48e2-af9f-e46d8f59aa55|2011-05-31 00:00:00.000|
       43662|             8|2011-05-31 00:00:00.000|2011-06-12 00:00:00.000|2011-06-07 00:00:00.000|     5|false          |PO18444174044      |10-4020-000227|     29994|          282|          6|            482|            482|           5|       10456|125295Vi53935         |             4|28832.5289|2775.1646| 867.2389|32474.9324|       |4a1ecfc0-cc3a-4740-b028-1c50bb48711c|2011-06-07 00:00:00.000|       43662|                32|2E53-4802-85         |       2|      730|             1| 183.9382|                0|5ace6cb5-7341-4d88-9425-56ab7ae512e7|2011-05-31 00:00:00.000|
       43662|             8|2011-05-31 00:00:00.000|2011-06-12 00:00:00.000|2011-06-07 00:00:00.000|     5|false          |PO18444174044      |10-4020-000227|     29994|          282|          6|            482|            482|           5|       10456|125295Vi53935         |             4|28832.5289|2775.1646| 867.2389|32474.9324|       |4a1ecfc0-cc3a-4740-b028-1c50bb48711c|2011-06-07 00:00:00.000|       43662|                33|2E53-4802-85         |       4|      754|             1|  874.794|                0|dff2a7f2-37f2-4016-bedf-4dbb8c88b631|2011-05-31 00:00:00.000|
       43662|             8|2011-05-31 00:00:00.000|2011-06-12 00:00:00.000|2011-06-07 00:00:00.000|     5|false          |PO18444174044      |10-4020-000227|     29994|          282|          6|            482|            482|           5|       10456|125295Vi53935         |             4|28832.5289|2775.1646| 867.2389|32474.9324|       |4a1ecfc0-cc3a-4740-b028-1c50bb48711c|2011-06-07 00:00:00.000|       43662|                34|2E53-4802-85         |       3|      725|             1| 183.9382|                0|92897ee9-72a6-49e6-a421-c243b531aff2|2011-05-31 00:00:00.000|
       43662|             8|2011-05-31 00:00:00.000|2011-06-12 00:00:00.000|2011-06-07 00:00:00.000|     5|false          |PO18444174044      |10-4020-000227|     29994|          282|          6|            482|            482|           5|       10456|125295Vi53935         |             4|28832.5289|2775.1646| 867.2389|32474.9324|       |4a1ecfc0-cc3a-4740-b028-1c50bb48711c|2011-06-07 00:00:00.000|       43662|                35|2E53-4802-85         |       5|      762|             1| 419.4589|                0|1ce18faa-047d-41b1-8cf6-5191c092ab3c|2011-05-31 00:00:00.000|
       43662|             8|2011-05-31 00:00:00.000|2011-06-12 00:00:00.000|2011-06-07 00:00:00.000|     5|false          |PO18444174044      |10-4020-000227|     29994|          282|          6|            482|            482|           5|       10456|125295Vi53935         |             4|28832.5289|2775.1646| 867.2389|32474.9324|       |4a1ecfc0-cc3a-4740-b028-1c50bb48711c|2011-06-07 00:00:00.000|       43662|                36|2E53-4802-85         |       3|      765|             1| 419.4589|                0|f833cb4a-d6e3-40ac-b383-1c4e7220afef|2011-05-31 00:00:00.000|
       43662|             8|2011-05-31 00:00:00.000|2011-06-12 00:00:00.000|2011-06-07 00:00:00.000|     5|false          |PO18444174044      |10-4020-000227|     29994|          282|          6|            482|            482|           5|       10456|125295Vi53935         |             4|28832.5289|2775.1646| 867.2389|32474.9324|       |4a1ecfc0-cc3a-4740-b028-1c50bb48711c|2011-06-07 00:00:00.000|       43662|                37|2E53-4802-85         |       2|      768|             1| 419.4589|                0|d8e84dbd-a1ed-41e4-a9a1-0ad95fb9b323|2011-05-31 00:00:00.000|
       43662|             8|2011-05-31 00:00:00.000|2011-06-12 00:00:00.000|2011-06-07 00:00:00.000|     5|false          |PO18444174044      |10-4020-000227|     29994|          282|          6|            482|            482|           5|       10456|125295Vi53935         |             4|28832.5289|2775.1646| 867.2389|32474.9324|       |4a1ecfc0-cc3a-4740-b028-1c50bb48711c|2011-06-07 00:00:00.000|       43662|                38|2E53-4802-85         |       1|      753|             1| 2146.962|                0|491c561f-6b1e-47d0-95d5-6519654a5f47|2011-05-31 00:00:00.000|
       43662|             8|2011-05-31 00:00:00.000|2011-06-12 00:00:00.000|2011-06-07 00:00:00.000|     5|false          |PO18444174044      |10-4020-000227|     29994|          282|          6|            482|            482|           5|       10456|125295Vi53935         |             4|28832.5289|2775.1646| 867.2389|32474.9324|       |4a1ecfc0-cc3a-4740-b028-1c50bb48711c|2011-06-07 00:00:00.000|       43662|                39|2E53-4802-85         |       1|      756|             1|  874.794|                0|2ba4cc58-b832-47c0-aef4-d15d029cc083|2011-05-31 00:00:00.000|
       43662|             8|2011-05-31 00:00:00.000|2011-06-12 00:00:00.000|2011-06-07 00:00:00.000|     5|false          |PO18444174044      |10-4020-000227|     29994|          282|          6|            482|            482|           5|       10456|125295Vi53935         |             4|28832.5289|2775.1646| 867.2389|32474.9324|       |4a1ecfc0-cc3a-4740-b028-1c50bb48711c|2011-06-07 00:00:00.000|       43662|                40|2E53-4802-85         |       3|      763|             1| 419.4589|                0|d5c51a6a-6d12-4ceb-aed4-842e78baf142|2011-05-31 00:00:00.000|
       43662|             8|2011-05-31 00:00:00.000|2011-06-12 00:00:00.000|2011-06-07 00:00:00.000|     5|false          |PO18444174044      |10-4020-000227|     29994|          282|          6|            482|            482|           5|       10456|125295Vi53935         |             4|28832.5289|2775.1646| 867.2389|32474.9324|       |4a1ecfc0-cc3a-4740-b028-1c50bb48711c|2011-06-07 00:00:00.000|       43662|                41|2E53-4802-85         |       1|      732|             1|  356.898|                0|48420c59-4fbd-4817-a1df-613dce40b18b|2011-05-31 00:00:00.000|
       43662|             8|2011-05-31 00:00:00.000|2011-06-12 00:00:00.000|2011-06-07 00:00:00.000|     5|false          |PO18444174044      |10-4020-000227|     29994|          282|          6|            482|            482|           5|       10456|125295Vi53935         |             4|28832.5289|2775.1646| 867.2389|32474.9324|       |4a1ecfc0-cc3a-4740-b028-1c50bb48711c|2011-06-07 00:00:00.000|       43662|                42|2E53-4802-85         |       6|      758|             1|  874.794|                0|2c4f704d-c794-4613-9c8b-258619fa20fa|2011-05-31 00:00:00.000|
	   ...

Contribute your code and comments through Disqus.

Previous: Fetch first twenty rows from the table.
Next: Searches for the word red in the name column.

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