home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / sql / appendix.shr next >
Text File  |  1988-02-03  |  11KB  |  307 lines

  1.  
  2.  
  3.          APPENDIX A
  4.  
  5.          The following queries will give you an idea of the power of SQL.  
  6.          Registered users not only receive the answers, they also receive 
  7.          detailed explanations of how to arrive at the answers.  In some 
  8.          cases there is more than one solution.
  9.          
  10.          Most of the answers require knowledge of joining tables and 
  11.          subqueries.
  12.  
  13.          The information is divided into six tables.  For those of you 
  14.          not familiar with data normalization, it may seem like it would 
  15.          be better to combine the tables.  That would produce more 
  16.          problems than it would solve.
  17.  
  18.          SALES TABLE
  19.          date = Date of sale - YYMMDD format 
  20.          bc   = Branch Code        - corresponds to code in the Branch table
  21.          cc   = Customer Code      - corresponds to code in the Cust table
  22.          sn   = Salesperson number - corresponds to enum in the Emp table
  23.          pc   = Product Code       - corresponds to code in the Prod table
  24.          qty  = Quantity purchased
  25.          Primary key is date,bc,cc,sn,pc
  26.          We are assuming that a customer will not purchase the same 
  27.          product from the same branch from the same salesperson on the 
  28.          same day! 
  29.  
  30.          BRANCH TABLE
  31.          code   = Branch code
  32.          st     = Branch state
  33.          MgrNum = Employee number of manager - corresponds to enum in the 
  34.                   Emp table
  35.          Primary key is code
  36.  
  37.          EMP TABLE
  38.          enum = Employee number 
  39.          name = Employee name
  40.          mgrn = Employee number of employee's manager - corresponds to 
  41.                 enum
  42.          Primary key is enum
  43.  
  44.          PROD TABLE
  45.          code = Product code
  46.          desc = Product description
  47.          Primary key is code
  48.  
  49.          MANU TABLE (Manufacturing information)
  50.          code    = Product code - corresponds to code in the Prod table
  51.          mst     = State where the product is manufactured
  52.          defects = percent of defects last time the product was 
  53.                    manufactured
  54.          Primary key is code
  55.  
  56.          CUSTOMER TABLE
  57.          code   = Customer code
  58.          name   = Customer name
  59.          st     = Customer state
  60.          rating = Customer rating
  61.          Primary key is code
  62.  
  63.                                        APP-1
  64.          
  65.  
  66.  
  67.          The tables should already be on your disk with the following 
  68.          data:
  69.  
  70.          SALES                        BRANCH
  71.          date   bc cc sn pc qty       code st MgrNum 
  72.          ------ -- -- -- -- ---       ---- -- ------ 
  73.          870401 b1 c3 11 AB  23       b1   AZ     10 
  74.          870401 b1 c3 11 DD  34       b2   CA     20 
  75.          870408 b1 c4 12 AB   2       b3   OR     30 
  76.          870420 b1 c5 13 AC 450       b4   WA     40 
  77.          870421 b1 c6 11 FA  87       b5   NV     50 
  78.          870422 b1 c6 12 EC  12       b6   AZ     60 
  79.          870402 b2 c5 24 EC  55 
  80.          870408 b2 c5 24 DZ  81       EMP
  81.          870410 b2 c2 27 DA   3       enum name             mgrn 
  82.          870412 b2 c2 27 AB  41       ---- ---------------- ---- 
  83.          870415 b3 c2 33 AC  33          1 Harvey Bigcheese      
  84.          870418 b3 c1 35 DZ 125         10 Mary Hizzle         1 
  85.          870420 b3 c1 35 DA 875         11 Marty Nogglater    12 
  86.          870425 b3 c5 32 AB 674         12 Gary Hoppertalk    10 
  87.          870430 b3 c5 32 AC 675         13 Kelly Unlucky      12 
  88.          870401 b4 c1 41 EA 947         14 Bobo Butters       12 
  89.          870408 b4 c1 41 EC 452         20 Nancy Umlat         1 
  90.          870415 b4 c1 41 FA  32         22 Larry Moe          20 
  91.          870402 b5 c6 52 AB 342         24 Curley Moe         22 
  92.          870408 b5 c4 56 AC 845         27 Quarkly Poslak     20 
  93.          870420 b5 c4 56 DD 654         30 Vernal Equinox      1 
  94.          870401 b6 c3 63 DA  45         32 Otto Otter         35 
  95.          870408 b6 c3 63 DZ  73         33 Yarquark Moon      35 
  96.                                         35 Joe Snow           30 
  97.                                         37 Iflab Wasmal       35 
  98.          PROD                           40 Telan Rowaq         1 
  99.          code desc                      41 Robby Marslat      43 
  100.          ---- --------------            43 Mongus Wombat      40 
  101.          AB   Megawamp                  50 Kanga Rat           1 
  102.          AC   Gigasnarf                 52 Elark Kaboom       50 
  103.          DD   Decawidgit                54 Agarth Boavlam     50 
  104.          DA   Technowidgit              56 Finster Funankle   52 
  105.          DZ   Electrowidgit             60 Xero Xanadu         1 
  106.          EA   Nanomouse                 62 Duncan Donut       60 
  107.          EC   RGBMouse                  63 Duncan Bagel       60 
  108.          FA   Dynamic Disk              64 Ticia Splatt       60 
  109.          
  110.          
  111.          MANU                        CUSTOMER
  112.          code mst defects            code name            st rating 
  113.          ---- --- -------            ---- --------------- -- ------ 
  114.          AB   WA        3            c1   Compugorp       WA     20 
  115.          AC   AZ        0            c2   Techoharps      OR     15 
  116.          DD   WA        3            c3   Organomice      AZ     34 
  117.          DA   OR       10            c4   QuarkCo         AZ     10 
  118.          DZ   AZ                     c5   Marswarp        CA     30 
  119.          EA   AZ       12            c6   Multicrud       NV      2 
  120.          EC   ID        2 
  121.          FA   NV        3 
  122.          AB   ID        5 
  123.  
  124.                                        APP-2
  125.          
  126.          
  127.          
  128.           
  129.          1.  What is the total number of different products sold to 
  130.          customer c1?
  131.  
  132.          2.  Get product numbers for all products supplied to all 
  133.          customers in California.
  134.  
  135.          3.  Get customer numbers for customers who have purchased from 
  136.          any branch which sells products manufactured in Arizona.
  137.          
  138.          4.  Get the employee name and corresponding manager name for 
  139.          every employee who has a manager.
  140.  
  141.          5.  Get the employee name, corresponding manager name, and the 
  142.          manager's manager name for all instances where the three exist.  
  143.          For example, the following would be a row from the result:
  144.          Duncan Donut  Xero Xanadu  Harvey Bigcheese
  145.          This is because Duncan works for Xero and Xero works for 
  146.          Harvey.  
  147.  
  148.          6.  For sales from 870410 to 870420, print the date, customer 
  149.          name and salesperson name, product name and qty for each sale. 
  150.          
  151.          7. Get customer names and product names for customers who have 
  152.          purchased items not manufactured in their own state. Sort it by 
  153.          product description within customer name.
  154.  
  155.          8. Get all columns from the sales table for the customer with 
  156.          the highest rating.
  157.  
  158.          9. Get branch code, customer names and product names for who 
  159.          purchase products manufactured in their own state from a branch 
  160.          in their own state.
  161.  
  162.          10. Get branch numbers for branches who sell to both c1 and c2.
  163.  
  164.          11. Get product codes for products sold to any customer in 
  165.          California.
  166.  
  167.          13.  Get product numbers and corresponding customer names for 
  168.          products manufactured in the same state as a customer. 
  169.          
  170.          14.  Get customer numbers for customers sold at least one 
  171.          product from a branch not in the same state.
  172.  
  173.          15.  Get branch codes for branches which sell at least one 
  174.          product sold by at least one branch which sells product DD. 
  175.          
  176.          16.  Get all pairs of state values such that the branch in the 
  177.          first state sells to a customer in a second state. 
  178.          
  179.          17.  Get all [State, product code, State] where the branch in 
  180.          the first state sells the product to the customer in the second 
  181.          state.  Exclude rows where the states are the same. 
  182.  
  183.                                        APP-3
  184.          
  185.          
  186.          
  187.                   
  188.          18.  What is the average quantity for a purchase of a product that 
  189.          ends in 'A'?
  190.  
  191.          19.  Did Yarquark Moon's manager sell anything to customer c1?
  192.  
  193.          20.  What are the names of managers who actually sold something?
  194.  
  195.          21.  In order to determine the significance of the customer 
  196.          rating, what is the average quantity for each rating, sorted by 
  197.          rating.  Just list the rating and the average quantity.
  198.  
  199.          22.  How many purchases of Megawamps were sold by a branch in AZ 
  200.          and what was the average quantity sold?
  201.  
  202.          23.  What are the names of managers whose salespeople have sold 
  203.          products to Organomice?
  204.  
  205.          24.  Get the manager name and the total quantity sold by that
  206.          manager's immediate subordinates.
  207.  
  208.          25.  Get the defects and the average quantity sold for each level 
  209.          of defects.  Only include products for which we track defects.
  210.  
  211.          26.  Who of Xero Xanadu's subordinates sold Technowidgits and what 
  212.          were the quantities sold?
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.          
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.                                      APP-4
  243.          
  244.  
  245.  
  246.          APPENDIX B
  247.          
  248.          Answers to the questions in the tutorial:
  249.          
  250.          1. List all the data in the table.
  251.          select * 
  252.          from cust;
  253.          
  254.          code name            st rating 
  255.          ---- --------------- -- ------ 
  256.          c1   Compugorp       WA     20 
  257.          c2   Techoharps      OR        
  258.          c3   Organomice      AZ     36 
  259.                                         
  260.          3 rows selected                
  261.  
  262.          2. List the customer name and rating for all customers in AZ.
  263.          select name, rating 
  264.          from cust 
  265.          where st = 'AZ';
  266.  
  267.          name            rating         
  268.          --------------- ------         
  269.          Organomice          36         
  270.                                         
  271.          1 row selected                 
  272.  
  273.          3. List all the data for ratings from 10 to 35.
  274.          select * 
  275.          from cust 
  276.          where rating between 10 and 35;
  277.  
  278.          code name            st rating 
  279.          ---- --------------- -- ------ 
  280.          c1   Compugorp       WA     20 
  281.                                         
  282.          1 row selected                 
  283.  
  284.          4. List the average rating.
  285.          select avg(rating) 
  286.          from cust;
  287.          
  288.          AVG(rating)    
  289.          -----------    
  290.                   28    
  291.                         
  292.          3 rows selected
  293.  
  294.          5. How many customers have a rating of less than 25?
  295.          select count(code) 
  296.          from cust 
  297.          where rating < 25;
  298.  
  299.          CNT(code)     
  300.          ---------     
  301.                1       
  302.                        
  303.          1 row selected
  304.  
  305.                                      APP-5
  306.          
  307.