w3resource

AdventureWorks Database: Retrieve the salesperson for each PostalCode

SQL Query - AdventureWorks: Exercise-21 with Solution

21. From the following tables write a query in SQL to retrieve the salesperson for each PostalCode who belongs to a territory and SalesYTD is not zero. Return row numbers of each group of PostalCode, last name, salesytd, postalcode column. Sort the salesytd of each postalcode group in descending order. Shorts the postalcode in ascending order.

Sample table: Sales.SalesPerson
businessentityid|territoryid|salesquota|bonus|commissionpct|salesytd    |saleslastyear|rowguid                             |modifieddate           |
----------------+-----------+----------+-----+-------------+------------+-------------+------------------------------------+-----------------------+
             274|           |          |    0|            0| 559697.5639|            0|48754992-9ee0-4c0e-8c94-9451604e3e02|2010-12-28 00:00:00.000|
             275|          2|    300000| 4100|        0.012|3763178.1787| 1750406.4785|1e0a7274-3064-4f58-88ee-4c6586c87169|2011-05-24 00:00:00.000|
             276|          4|    250000| 2000|        0.015|4251368.5497| 1439156.0291|4dd9eee4-8e81-4f8c-af97-683394c1f7c0|2011-05-24 00:00:00.000|
             277|          3|    250000| 2500|        0.015|3189418.3662| 1997186.2037|39012928-bfec-4242-874d-423162c3f567|2011-05-24 00:00:00.000|
             278|          6|    250000|  500|         0.01|1453719.4653| 1620276.8966|7a0ae1ab-b283-40f9-91d1-167abf06d720|2011-05-24 00:00:00.000|
             279|          5|    300000| 6700|         0.01| 2315185.611| 1849640.9418|52a5179d-3239-4157-ae29-17e868296dc0|2011-05-24 00:00:00.000|
             280|          1|    250000| 5000|         0.01|1352577.1325|  1927059.178|be941a4a-fb50-4947-bda4-bb8972365b08|2011-05-24 00:00:00.000|
             281|          4|    250000| 3550|         0.01|2458535.6169| 2073505.9999|35326ddb-7278-4fef-b3ba-ea137b69094e|2011-05-24 00:00:00.000|
             282|          6|    250000| 5000|        0.015|2604540.7172| 2038234.6549|31fd7fc1-dc84-4f05-b9a0-762519eacacc|2011-05-24 00:00:00.000|
             283|          1|    250000| 3500|        0.012|1573012.9383| 1371635.3158|6bac15b2-8ffb-45a9-b6d5-040e16c2073f|2011-05-24 00:00:00.000|
             284|          1|    300000| 3900|        0.019|1576562.1966|            0|ac94ec04-a2dc-43e3-8654-dd0c546abc17|2012-09-23 00:00:00.000|
             285|           |          |    0|            0| 172524.4512|            0|cfdbef27-b1f7-4a56-a878-0221c73bae67|2013-03-07 00:00:00.000|
             286|          9|    250000| 5650|        0.018|1421810.9242| 2278548.9776|9b968777-75dc-45bd-a8df-9cdaa72839e1|2013-05-23 00:00:00.000|
             287|           |          |    0|            0|  519905.932|            0|1dd1f689-df74-4149-8600-59555eef154b|2012-04-09 00:00:00.000|
             288|          8|    250000|   75|        0.018|1827066.7118| 1307949.7917|224bb25a-62e3-493e-acaf-4f8f5c72396a|2013-05-23 00:00:00.000|
             289|         10|    250000| 5150|         0.02|4116871.2277| 1635823.3967|25f6838d-9db4-4833-9ddc-7a24283af1ba|2012-05-23 00:00:00.000|
             290|          7|    250000|  985|        0.016|3121616.3202| 2396539.7601|f509e3d4-76c8-42aa-b353-90b7b8db08de|2012-05-23 00:00:00.000|

Click to view Full table

Sample table: Person.Person
businessentityid|persontype|namestyle|title|firstname               |middlename      |lastname              |suffix|emailpromotion|additionalcontactinfo|demographics|rowguid                             |modifieddate           |
----------------+----------+---------+-----+------------------------+----------------+----------------------+------+--------------+---------------------+------------+------------------------------------+-----------------------+
               1|EM        |false    |     |Ken                     |J               |Sánchez               |      |             0|                     |[XML]       |92c4279f-1207-48a3-8448-4636514eb7e2|2009-01-07 00:00:00.000|
               2|EM        |false    |     |Terri                   |Lee             |Duffy                 |      |             1|                     |[XML]       |d8763459-8aa8-47cc-aff7-c9079af79033|2008-01-24 00:00:00.000|
               3|EM        |false    |     |Roberto                 |                |Tamburello            |      |             0|                     |[XML]       |e1a2555e-0828-434b-a33b-6f38136a37de|2007-11-04 00:00:00.000|
               4|EM        |false    |     |Rob                     |                |Walters               |      |             0|                     |[XML]       |f2d7ce06-38b3-4357-805b-f4b6b71c01ff|2007-11-28 00:00:00.000|
               5|EM        |false    |Ms.  |Gail                    |A               |Erickson              |      |             0|                     |[XML]       |f3a3f6b4-ae3b-430c-a754-9f2231ba6fef|2007-12-30 00:00:00.000|
               6|EM        |false    |Mr.  |Jossef                  |H               |Goldberg              |      |             0|                     |[XML]       |0dea28fd-effe-482a-afd3-b7e8f199d56f|2013-12-16 00:00:00.000|
               7|EM        |false    |     |Dylan                   |A               |Miller                |      |             2|                     |[XML]       |c45e8ab8-01be-4b76-b215-820c8368181a|2009-02-01 00:00:00.000|
               8|EM        |false    |     |Diane                   |L               |Margheim              |      |             0|                     |[XML]       |a948e590-4a56-45a9-bc9a-160a1cc9d990|2008-12-22 00:00:00.000|
               9|EM        |false    |     |Gigi                    |N               |Matthew               |      |             0|                     |[XML]       |5fc28c0e-6d36-4252-9846-05caa0b1f6c5|2009-01-09 00:00:00.000|
			   -- more --

Click to view Full table

Sample table: Person.Address
|addressid|addressline1              |addressline2|city         |stateprovinceid|postalcode|spatiallocation                             |rowguid                             |modifieddate           |
|---------|--------------------------|------------|-------------|---------------|----------|--------------------------------------------|------------------------------------|-----------------------|
|1        |1970 Napa Ct.             |            |Bothell      |79             |98011     |E6100000010CAE8BFC28BCE4474067A89189898A5EC0|9aadcb0d-36cf-483f-84d8-585c2d4ec6e9|2007-12-04 00:00:00.000|
|2        |9833 Mt. Dias Blv.        |            |Bothell      |79             |98011     |E6100000010CD6FA851AE6D74740BC262A0A03905EC0|32a54b9e-e034-4bfb-b573-a71cde60d8c0|2008-11-30 00:00:00.000|
|3        |7484 Roundtree Drive      |            |Bothell      |79             |98011     |E6100000010C18E304C4ADE14740DA930C7893915EC0|4c506923-6d1b-452c-a07c-baa6f5b142a4|2013-03-07 00:00:00.000|
|4        |9539 Glenside Dr          |            |Bothell      |79             |98011     |E6100000010C813A0D5F9FDE474011A5C28A7C955EC0|e5946c78-4bcc-477f-9fa1-cc09de16a880|2009-02-03 00:00:00.000|
|5        |1226 Shoe St.             |            |Bothell      |79             |98011     |E6100000010C61C64D8ABBD94740C460EA3FD8855EC0|fbaff937-4a97-4af0-81fd-b849900e9bb0|2008-12-19 00:00:00.000|
|6        |1399 Firestone Drive      |            |Bothell      |79             |98011     |E6100000010CE0B4E50458DA47402F12A5F80C975EC0|febf8191-9804-44c8-877a-33fde94f0075|2009-02-13 00:00:00.000|
|7        |5672 Hale Dr.             |            |Bothell      |79             |98011     |E6100000010C18E304C4ADE1474011A5C28A7C955EC0|0175a174-6c34-4d41-b3c1-4419cd6a0446|2009-12-11 00:00:00.000|
|8        |6387 Scenic Avenue        |            |Bothell      |79             |98011     |E6100000010C0029A5D93BDF4740E248962FD5975EC0|3715e813-4dca-49e0-8f1c-31857d21f269|2008-12-17 00:00:00.000|
|9        |8713 Yosemite Ct.         |            |Bothell      |79             |98011     |E6100000010C6A80AD742DDC4740851574F7198C5EC0|268af621-76d7-4c78-9441-144fd139821a|2012-05-30 00:00:00.000|
|10       |250 Race Court            |            |Bothell      |79             |98011     |E6100000010C219D64AE1FE4474040862564B7825EC0|0b6b739d-8eb6-4378-8d55-fe196af34c04|2008-12-02 00:00:00.000|
-- more --

Click to view Full table

Sample Solution:

-- Selecting row numbers, last names, sales year-to-date, and postal codes from multiple tables with window function usage
SELECT 
    -- Using the ROW_NUMBER() window function to assign a unique row number to each row within a partition
    ROW_NUMBER() OVER win AS "Row Number",
    -- Selecting the last names of salespersons
    pp.LastName, 
    -- Selecting the year-to-date sales amount
    sp.SalesYTD, 
    -- Selecting the postal codes associated with the addresses
    pa.PostalCode
-- Joining Sales.SalesPerson table with Person.Person and Person.Address tables based on specific conditions
FROM Sales.SalesPerson AS sp
    INNER JOIN Person.Person AS pp
        ON sp.BusinessEntityID = pp.BusinessEntityID
    INNER JOIN Person.Address AS pa
        ON pa.AddressID = pp.BusinessEntityID
-- Filtering the rows based on certain conditions
WHERE TerritoryID IS NOT NULL
    AND SalesYTD <> 0
-- Defining a window for the window function
WINDOW win AS (PARTITION BY PostalCode ORDER BY SalesYTD DESC)
-- Sorting the result set by postal code
ORDER BY PostalCode;

or

SELECT ROW_NUMBER() OVER (PARTITION BY PostalCode ORDER BY SalesYTD DESC) AS "Row Number",
pp.LastName, sp.SalesYTD, pa.PostalCode
FROM Sales.SalesPerson AS sp
    INNER JOIN Person.Person AS pp
        ON sp.BusinessEntityID = pp.BusinessEntityID
    INNER JOIN Person.Address AS pa
        ON pa.AddressID = pp.BusinessEntityID
WHERE TerritoryID IS NOT NULL
    AND SalesYTD <> 0
ORDER BY PostalCode;

Explanation:

  • The SELECT statement retrieves data from multiple tables.
  • ROW_NUMBER() OVER win AS "Row Number": Generates a unique row number for each row within a partition defined by the window function.
  • pp.LastName, sp.SalesYTD, pa.PostalCode: Specifies the columns to be retrieved.
  • The FROM clause specifies the source tables for the data, including Sales.SalesPerson (aliased as sp), Person.Person (aliased as pp), and Person.Address (aliased as pa).
  • The INNER JOIN clauses join the tables based on specific conditions (sp.BusinessEntityID = pp.BusinessEntityID and pa.AddressID = pp.BusinessEntityID).
  • The WHERE clause filters the rows based on certain conditions (TerritoryID IS NOT NULL and SalesYTD <> 0).
  • The WINDOW clause defines a window named "win" for the window function, partitioning the data by PostalCode and ordering it by SalesYTD in descending order.
  • ORDER BY PostalCode: Sorts the result set by postal code.

Sample Output:

Row Number|lastname         |salesytd    |postalcode|
----------+-----------------+------------+----------+
         1|Mitchell         |4251368.5497|98027     |
         2|Blythe           |3763178.1787|98027     |
         3|Carson           |3189418.3662|98027     |
         4|Reiter           | 2315185.611|98027     |
         5|Vargas           |1453719.4653|98027     |
         6|Ansman-Wolfe     |1352577.1325|98027     |
         1|Pak              |4116871.2277|98055     |
         2|Varkey Chudukatil|3121616.3202|98055     |
         3|Saraiva          |2604540.7172|98055     |
         4|Ito              |2458535.6169|98055     |
         5|Valdez           |1827066.7118|98055     |
         6|Mensa-Annan      |1576562.1966|98055     |
         7|Campbell         |1573012.9383|98055     |
         8|Tsoflias         |1421810.9242|98055     |

SQL AdventureWorks Editor:

Practice Online


Contribute your code and comments through Disqus.

Previous: List contacts who are Purchasing Manager.
Next: Number of contacts for each type and name.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.