home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / sql / select.shr < prev    next >
Text File  |  1988-02-29  |  38KB  |  1,087 lines

  1.  
  2.          SELECT (Single Table)
  3.  
  4.          SYNTAX:
  5.  
  6.          SELECT [DISTINCT] column_name [,column_name ...]
  7.          FROM table_name [,table_name ...]
  8.          [WHERE search_expression]
  9.          [INTO file_name]  <-- Non-Standard
  10.          [GROUP BY column_name [,column_name ...] [HAVING criteria]
  11.          [ORDER BY column_name [,column_name ...]
  12.          
  13.          If you want all the columns in a table you can replace the list 
  14.          of column names with an asterisk (*).
  15.  
  16.          The select command produces a report in the form of a table.  
  17.          Although the basic use of the select takes a short time to learn, 
  18.          the more advanced uses take a long time to master.
  19.  
  20.          The pm table is included so you can try entering the commands 
  21.          yourself.  For those of you who know about data normalization, 
  22.          the pm table is not even in the second normal form.  I did this 
  23.          so I could more easily explain the use of all the commands using 
  24.          a single table. 
  25.  
  26.          The pm (product manufacture) table consists of:
  27.          code    - 2-character product code
  28.          desc    - 15-character product description
  29.          mst     - state in which the product was manufactured
  30.          defects - % defects of the last batch of products manufactured
  31.  
  32.          Since the product can be manufactured in more that one state, 
  33.          the combination of code and mst is the primary key.
  34.  
  35.          All the examples have each clause starting on a new line for 
  36.          purposes of clarity.  It is not necessary to have them on 
  37.          separate lines.  It is sometimes advantageous to keep the query 
  38.          on one line since if you need to make a correction, you cannot 
  39.          backup a line. 
  40.  
  41.          THE BASICS OF SELECT
  42.  
  43.          To find all the data in a table:
  44.          select *                            
  45.          from pm;                             
  46.          
  47.           code  desc             mst  defects 
  48.           ----  ---------------  ---  -------
  49.           AB    Megawamp         WA         3 
  50.           AC    Gigasnarf        AZ         0 
  51.           DD    Decawidgit       WA         3 
  52.           DA    Technowidgit     OR        10 
  53.           DZ    Electrowidgit    AZ           
  54.           EA    nanomouse        AZ        12 
  55.           EC    RGBMouse         ID         2 
  56.           FA    Dynamic Disk     NV         3 
  57.           AB    Megawamp         ID         5 
  58.          
  59.          9 rows selected                           
  60.  
  61.  
  62.                                    SELECT-1
  63.  
  64.  
  65.  
  66.  
  67.  
  68.          Instead of typing all the column names, you can use the * to 
  69.          denote all column names.  On the next line, the from pm tells us 
  70.          that we want the data to come from the table named "p" (for 
  71.          product).  All select commands must end with a semi-colon (;).
  72.  
  73.          If we just wanted the product code and the state that it was 
  74.          manufactured in you would type:
  75.  
  76.          select code, mst 
  77.          from pm;          
  78.          
  79.           code  mst          
  80.           ----  ---             
  81.           AB    WA           
  82.           AC    AZ           
  83.           DD    WA           
  84.           DA    OR           
  85.           DZ    AZ           
  86.           EA    AZ           
  87.           EC    ID           
  88.           FA    NV           
  89.           AB    ID  
  90.  
  91.           9 rows selected       
  92.  
  93.          DISTINCT
  94.  
  95.          The optional statement "distinct"  will eliminate all rows with 
  96.          duplicate columns that you have specified.  If you wanted to find 
  97.          out all the different states where products are manufactured, you 
  98.          would type:
  99.  
  100.          select distinct mst      
  101.          from pm;                
  102.          
  103.           mst                       
  104.           ---                         
  105.           WA                        
  106.           AZ                        
  107.           OR                        
  108.           ID                        
  109.           NV                        
  110.          
  111.           5 rows selected             
  112.  
  113.          As you can see only five rows were found.  This is because three 
  114.          products are manufactured in Arizona and two products are 
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.                                    SELECT-2
  123.  
  124.  
  125.  
  126.  
  127.  
  128.          manufactured in Washington.  Only the first occurance of each 
  129.          data element is retained.
  130.  
  131.          The "distinct" modifier can be used with one or more column names 
  132.          in a select statement.  You will see useful examples of this when 
  133.          we discuss more complex searches.
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.                                    SELECT-3
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.          WHERE
  190.  
  191.          The where clause of the select command is the most powerful and 
  192.          complex.  Our discussion will be broken down into various phases.  
  193.          The first phase is using the where in conjunction with a single 
  194.          table name.  Later you will see it used with multiple tables and 
  195.          you will find that you can even put another select command within 
  196.          the where clause. 
  197.  
  198.          search_expression
  199.          
  200.          =, <>, !=, >, <, >=, <=, IS NULL, IS NOT NULL, LIKE
  201.  
  202.          The simplest search expression relates a column name to a 
  203.          constant.  A constant is an exact value that you enter.  All 
  204.          column names are tested against that value. If the column name 
  205.          that you are testing is defined as a char, the constant must be 
  206.          enclosed in single quotes or double quotes. The constant you are 
  207.          searching for may not contain a quote.  If the column name you 
  208.          are testing for is defined as a num, the constant is a number 
  209.          without any quotes.
  210.  
  211.          You can relate the column name to the constant in the following 
  212.          ways:
  213.          =    equal
  214.          <>   not equal
  215.          !=   not equal
  216.          >    greater than
  217.          <    less than
  218.          >=   greater than or equal
  219.          <=   less than or equal
  220.  
  221.          If you wanted to find out which products are manufactured in 
  222.          Arizona:
  223.  
  224.          select *                               
  225.          from pm                                 
  226.          where mst = 'AZ';                      
  227.          
  228.           code  desc             mst  defects    
  229.           ----  ---------------  ---  -------
  230.           AC    Gigasnarf        AZ         0    
  231.           DZ    Electrowidgit    AZ              
  232.           EA    nanomouse        AZ        12    
  233.          
  234.           3 rows selected                             
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.                                    SELECT-4
  242.  
  243.  
  244.  
  245.  
  246.          Spaces are optional on either side of the "=".  You could have 
  247.          typed:
  248.          where mst='AZ';
  249.          Instead of single quotes you could have used double quotes:
  250.          where mst="AZ";
  251.  
  252.          The column name "defects" is the percent of defects when the 
  253.          product was manufactured.  If you wanted a list of all the 
  254.          product descriptions and their corresponding defects for those 
  255.          products with defects over 10%:
  256.  
  257.          select desc, defects                               
  258.          from pm                                 
  259.          where defects > 10;                    
  260.          
  261.           desc             defects    
  262.           ---------------  -------
  263.           nanomouse             12    
  264.          
  265.           1 row selected                              
  266.  
  267.          We find that only the nanomouse has more than 10% defects.
  268.  
  269.          SPECIAL SEARCH EXPRESSION - IS NULL, IS NOT NULL
  270.  
  271.          Nulls are an important concept in relational databases.  For 
  272.          example, let's look at the percentage of defects in Arizona: 
  273.  
  274.          select *                               
  275.          from pm                                 
  276.          where mst = 'AZ';                      
  277.          
  278.           code  desc             mst  defects    
  279.           ----  ---------------  ---  -------
  280.           AC    Gigasnarf        AZ         0    
  281.           DZ    Electrowidgit    AZ              
  282.           EA    nanomouse        AZ        12    
  283.          
  284.           3 rows selected                             
  285.          
  286.          In Arizona, the Gigasnarf has zero defects, the nanomouse has 12% 
  287.          defects but there is nothing listed for the Electrowidgit.  This 
  288.          "nothing" is called a null.  It does not mean zero or blank.  A 
  289.          null means that the data element is not relevant in the row.  In 
  290.          our case, Electrowidgits are not tracked for defects like other 
  291.          parts.  This isn't to say that there are zero defects.  It just 
  292.          means we have no entry.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.                                    SELECT-5
  302.         
  303.  
  304.  
  305.  
  306.  
  307.  
  308.         If we wanted to look at all products for which we do not track 
  309.          defects:
  310.  
  311.          select * 
  312.          from pm
  313.          where defects is null;
  314.  
  315.           code  desc             mst  defects    
  316.           ----  ---------------  ---  -------
  317.           DZ    Electrowidgit    AZ              
  318.          
  319.           1 row found                                 
  320.  
  321.          We could find all the products for which we track defects by 
  322.          using is not null:
  323.  
  324.          select * 
  325.          from pm 
  326.          where defects is not null;
  327.          
  328.           code  desc             mst  defects 
  329.           ----  ---------------  ---  -------
  330.           AB    Megawamp         WA         3 
  331.           AC    Gigasnarf        AZ         0 
  332.           DD    Decawidgit       WA         3 
  333.           DA    Technowidgit     OR        10 
  334.           EA    nanomouse        AZ        12 
  335.           EC    RGBMouse         ID         2 
  336.           FA    Dynamic Disk     NV         3 
  337.           AB    Megawamp         ID         5 
  338.          
  339.           8 rows selected                          
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.                                    SELECT-6
  361.  
  362.  
  363.  
  364.  
  365.  
  366.          
  367.          SPECIAL SEARCH EXPRESSION - LIKE, NOT LIKE
  368.  
  369.          The expression like is used when we want similar characters.  Get 
  370.          all product descriptions that begin with the letter 'D':
  371.  
  372.          select * 
  373.          from pm 
  374.          where desc like 'D%';
  375.          
  376.           code  desc             mst  defects 
  377.           ----  ---------------  ---  -------
  378.           DD    Decawidgit       WA         3 
  379.           FA    Dynamic Disk     NV         3 
  380.          
  381.           2 rows selected                          
  382.  
  383.          The special character '%' tells the expression to accept any 
  384.          characters at that position and any subsequent position. The 
  385.          underline is another special character. It tells the expression 
  386.          to accept any character at that position only.  Get all product 
  387.          codes that end in 'A':
  388.  
  389.          select * 
  390.          from pm
  391.          where code like '_A';
  392.  
  393.           code  desc             mst  defects 
  394.           ----  ---------------  ---  -------
  395.           DA    Technowidgit     OR        10 
  396.           EA    nanomouse        AZ        12 
  397.           FA    Dynamic Disk     NV         3 
  398.          
  399.           3 rows selected                          
  400.          
  401.          The two special characters '%' and '_' can also be used together 
  402.          in a search expression.  You can produce the opposite result by 
  403.          using the not modifier.  Get all product codes which do not end 
  404.          in 'A':
  405.  
  406.          select * 
  407.          from pm
  408.          where code not like '_A';
  409.  
  410.           code  desc             mst  defects 
  411.           ----  ---------------  ---  -------      
  412.           AB    Megawamp         WA         3 
  413.           AC    Gigasnarf        AZ         0 
  414.           DD    Decawidgit       WA         3 
  415.           DZ    Electrowidgit    AZ           
  416.           EC    RGBMouse         ID         2 
  417.           AB    Megawamp         ID         5 
  418.          
  419.           6 rows selected                          
  420.  
  421.  
  422.                                    SELECT-7
  423.  
  424.  
  425.  
  426.  
  427.          AND, OR, NOT
  428.  
  429.          We can link expressions through the use of ands and ors.  If you 
  430.          use an and, both expressions have to be true in order to add the 
  431.          row to the table we are producing.  If you use an or, only one of 
  432.          the expressions have to be true.  You can use as many ands and 
  433.          ors as you want in a query.  The expressions are executed from 
  434.          left to right.  All the ands are done first, then execution 
  435.          starts again from the left and the ors are processed.
  436.  
  437.          If you want to reference different column names in a search, the 
  438.          logic as stated in English is typically the same as it is in SQL.  
  439.          Usually there is no confusion between the and and the or as is 
  440.          the case when we are dealing with the same column name.  
  441.          
  442.          If you want to find all products in Washington with defects over 
  443.          2%:
  444.          
  445.          select *                               
  446.          from pm 
  447.          where mst = 'WA' and defects > 2; 
  448.          
  449.           code  desc             mst  defects    
  450.           ----  ---------------  ---  -------
  451.           AB    Megawamp         WA         3    
  452.           DD    Decawidgit       WA         3    
  453.          
  454.           2 rows selected                             
  455.  
  456.          Generally, the use of a single and or an or in a where clause is 
  457.          more or less common sense unless you are refering to the same 
  458.          column name.  What if you wanted to find all the products 
  459.          manufactured in Washington and Idaho.  This has two meanings in 
  460.          everyday English:
  461.          1.  Out of all the products we manufacture, I want a list of the 
  462.          ones we manufacture in Washington and a list of the ones we 
  463.          manufacture in Idaho.
  464.          2.  Out of all the products we manufacture, I want a list of each
  465.          one we manufacture in BOTH Washington and Idaho.
  466.          
  467.          We will accept the first meaning in this example.  It is 
  468.          interesting to note that we could have also said, "List all the 
  469.          products manufactured in Washington or Idaho."  It seems odd that 
  470.          in English "or" can have the same meaning as "and".  Even if you 
  471.          don't agree with my interpretation, beware that others do and 
  472.          that it may cause communication problems.
  473.  
  474.          In the English version you use the "and" so there is a tendency 
  475.          to always use the "and" in the SQL version too.  This would be 
  476.          wrong.  The English perspective and the SQL perspective are 
  477.          different.  The English perspective often looks at the whole 
  478.          group of items.  The SQL perspective looks at one item at a time.  
  479.  
  480.  
  481.                                    SELECT-8
  482.  
  483.  
  484.  
  485.          To translate Washington and Idaho into SQL we look at it from its 
  486.          perspective.  We will look at each item. If that item is 
  487.          manufactured in Washington or Idaho, we will add it to our table.  
  488.          Notice that when we look at it from the SQL perspective, we use 
  489.          the "or" instead of the "and".  This not just a quirk of SQL 
  490.          logic, it is common to virtually all data bases. To find out all 
  491.          the products manufactured in Washington and Idaho: 
  492.  
  493.          select desc
  494.          from pm 
  495.          where mst='WA' or mst='ID';    
  496.          
  497.           desc                                                   
  498.           -----------------                                       
  499.           Megawamp                                               
  500.           Decawidgit                                             
  501.           RGBMouse                                               
  502.           Megawamp                                               
  503.          
  504.          4 rows selected                                           
  505.          
  506.          Megawamp is repeated above because it is manufactured in both 
  507.          Washington and Idaho.  To get rid of the duplicate you would 
  508.          replace the first line with:  select distinct desc
  509.  
  510.          It is also interesting to note what the meaning would be if we 
  511.          used an and instead of an or.  Would it tell us every product 
  512.          manufactured in BOTH Washington and Idaho?   No, it wouldn't!  
  513.          Actually the following search makes no sense.  How could a state 
  514.          for an individual item be both WA and ID?????
  515.  
  516.          select desc
  517.          from pm 
  518.          where mst='WA' and mst='ID';  
  519.          [ none found ]                                               
  520.  
  521.          But how WOULD you find out every product manufactured in both 
  522.          Washington and Idaho?  The search strategy is rather complex and 
  523.          the topic is covered under joining and virtual tables.
  524.  
  525.          In order to express the opposite logic, you use the not.  If you 
  526.          wanted to find the products NOT made in Washington and/or Idaho:
  527.  
  528.          select * 
  529.          from pm 
  530.          where not (mst='WA' or mst='ID');  
  531.          
  532.           code  desc             mst  defects                
  533.           ----  ---------------  ---  -------
  534.           AC    Gigasnarf        AZ         0                
  535.           DA    Technowidgit     OR        10                
  536.           DZ    Electrowidgit    AZ                          
  537.           EA    nanomouse        AZ        12                
  538.           FA    Dynamic Disk     NV         3                
  539.          
  540.          5 rows selected                                          
  541.  
  542.                                    SELECT-9
  543.  
  544.  
  545.  
  546.  
  547.          Normally, if a condition is true, we add that row to the table.  
  548.          By using the not, if the condition is false, we add the row to 
  549.          the table.  On the first row above, the state is AZ.  It was 
  550.          selected because it is not equal to WA or ID.  Just as we can 
  551.          express the same thought in English more than one way, we can do 
  552.          the same with SQL.  We could have written:
  553.  
  554.          select * 
  555.          from pm 
  556.          where mst <> 'WA' and mst <> 'ID';
  557.  
  558.          or
  559.  
  560.          select * 
  561.          from pm 
  562.          where not (mst = 'WA') and not (mst = 'ID');
  563.  
  564.          What if we would have written "or" instead of "and" in the above 
  565.          two examples?  Every row would have been selected.  No matter 
  566.          what the state is, it would either be not equal to WA or not 
  567.          equal to ID!
  568.  
  569.          Remember that whenever you use the not, you must enclose the 
  570.          expression in parentheses.
  571.  
  572.          You can have as many ands and ors in a query as you need.  What 
  573.          if you wanted all products in Washington and Idaho with defects 
  574.          over 3%:
  575.  
  576.          select * 
  577.          from pm 
  578.          where (mst='WA' or mst='ID') and defects > 3;
  579.          
  580.           code  desc             mst  defects                          
  581.           ----  ---------------  ---  -------
  582.           AB    Megawamp         ID         5                          
  583.          
  584.           1 row selected                                                    
  585.  
  586.          We find that only one product is selected.  Notice that 
  587.          parentheses enclose the or expression.  This is because without 
  588.          the parentheses the and expression would have been executed 
  589.          first.  Remember that without parentheses  the ands are processed 
  590.          first and the ors are processed next. Not inserting the 
  591.          parentheses would have changed the meaning considerably:
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.  
  599.  
  600.  
  601.                                    SELECT-10
  602.       
  603.  
  604.  
  605.  
  606.          select * 
  607.          from pm 
  608.          where mst='WA' or mst='ID' and defects > 3;  
  609.          
  610.           code  desc             mst  defects                          
  611.           ----  ---------------  ---  -------
  612.           AB    Megawamp         WA         3                          
  613.           DD    Decawidgit       WA         3                          
  614.           AB    Megawamp         ID         5                          
  615.          
  616.           3 rows selected                                                   
  617.  
  618.          The above query is the equivalent of saying, "List all products 
  619.          in Washington and list all products in Idaho with defects over 
  620.          3%."   Another way of stating it is "In looking through all the 
  621.          products, if a product is manufactured in Washington or an Idaho 
  622.          product has over 3% defects, add it to the list."  The important 
  623.          concept is that the 3% defects relates to the products 
  624.          manufactured in Idaho, not all products. 
  625.          
  626.  
  627.  
  628.  
  629.  
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636.  
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.                                    SELECT-11
  660.  
  661.  
  662.  
  663.  
  664.          SETS (used with constants)
  665.  
  666.          Instead of relating items one at a time, we can relate groups of 
  667.          items at once.  These groups are called sets.  They are enclosed 
  668.          by parentheses and the constants are separated by commas.
  669.  
  670.          ANY
  671.  
  672.          You saw above how you can use =, >, <, <>, >=, <=.  We can expand 
  673.          that concept to include "any" and "all".  As you will see, the 
  674.          "any" corresponds to an "or" and the "all" corresponds to an 
  675.          "and".
  676.          
  677.          All possible combinations:
  678.  
  679.          =any means equal to any
  680.          >any means greater than any
  681.          <any means less than any
  682.          <>any means not equal to any
  683.          >=any means greater than or equal to any
  684.          <=any means less than or equal to any
  685.          
  686.          Let's rewrite the query concerning all products manufactured in 
  687.          Washington or Idaho: 
  688.  
  689.          select desc 
  690.          from pm 
  691.          where mst =any ('WA','ID');   
  692.          
  693.           desc                                                
  694.           -----------------                                    
  695.           Megawamp                                            
  696.           Decawidgit                                          
  697.           RGBMouse                                            
  698.           Megawamp                                            
  699.          
  700.           3 rows selected                                       
  701.  
  702.          Translated into English it would be:  Select the description from 
  703.          the  p(roduct) table where the state of manufacture is equal to 
  704.          any of the following - WA or ID.
  705.  
  706.          We can use numbers too.  What if you wanted to know the names of 
  707.          products that have a percentage of defects of 2% or 3%:
  708.  
  709.          select desc 
  710.          from pm 
  711.          where defects =any (2,3);
  712.          
  713.           desc                                           
  714.           -----------------                               
  715.           Megawamp                                       
  716.           Decawidgit                                     
  717.           RGBMouse                                       
  718.           Dynamic Disk                                   
  719.          
  720.           4 rows selected                                  
  721.                                    SELECT-12
  722.  
  723.  
  724.  
  725.  
  726.          IN
  727.  
  728.          An equivalent to =any is in.  We will see that sets are much more 
  729.          useful when we discuss using nested selects and multiple tables.  
  730.          However, you could translate the above query to: 
  731.  
  732.          select desc 
  733.          from pm 
  734.          where defects in (2,3);
  735.  
  736.  
  737.          ALL
  738.  
  739.          The "all" is similar to the "any" except that instead of the 
  740.          items being connected by "or's", they are connected by "and's".  
  741.          It is useful in determining a group of items outside a certain 
  742.          set.  If you wanted to find all products manufactured outside 
  743.          of WA and ID:
  744.  
  745.          select *
  746.          from pm
  747.          where mst !=all ('WA','ID');
  748.  
  749.  
  750.          BETWEEN constant AND constant
  751.  
  752.          An easy way to select a range of values is use the between/and 
  753.          component of the where clause.  If you wanted to find the product 
  754.          descriptions with defects between 3% and 10%:
  755.  
  756.          select desc, defects 
  757.          from pm 
  758.          where defects between 3 and 10; 
  759.          
  760.           desc             defects                                      
  761.           ---------------  -------                                         
  762.           Megawamp               3                                      
  763.           Decawidgit             3                                      
  764.           Technowidgit          10                                      
  765.           Dynamic Disk           3                                      
  766.           Megawamp               5                                      
  767.          
  768.           5 rows selected                                                  
  769.  
  770.          
  771.          Notice that when we use the between, we include the 3 and the 10.  
  772.          Unfortunately, the word "between" can have more than one meaning in 
  773.          standard English. If you say "The treasure is located between San 
  774.          Francisco and Walnut Creek", you would assume that the treasure 
  775.          is not IN San Francisco or Walnut Creek.  That is, you would not 
  776.          include the end points.  If you say "This insurance plan is 
  777.          available for those people between the ages of 18 and 65", you 
  778.          would assume that if you are 18 or 65, you are eligible.  That 
  779.          is, you would include the end points.  Just remember that in SQL, 
  780.          between includes the end points. 
  781.  
  782.  
  783.                                      SELECT-13
  784.          
  785.          
  786.          
  787.          
  788.          MATHEMATICAL FUNCTIONS - AVG, MIN, MAX, SUM, COUNT
  789.  
  790.          All of the mathematical functions operate on numeric columns 
  791.          except count which will operate on alphanumeric columns.
  792.  
  793.          SYNTAX:
  794.  
  795.          AVG([DISTINCT] column_name)
  796.          MIN(column_name)
  797.          MAX(column_name)
  798.          SUM([DISTINCT]column_name)
  799.          COUNT([DISTINCT]column_name)
  800.          
  801.          SUMMARY
  802.          AVG   - without DISTINCT: The average of all values in a column
  803.                  with DISTINCT   : The average of non-duplicate values
  804.          MIN   - The minimum value in the column
  805.          MAX   - The maximum value in the column
  806.          SUM   - without DISTINCT: The sum of all values in the column
  807.                  with DISTINCT   : The sum of non-duplicate values
  808.          COUNT - without DISTINCT: the number of column entries
  809.                  with DISTINCT   : the number of non-duplicate column entries
  810.          
  811.          Notice that some of the functions have the distinct option.  This 
  812.          will delete all rows in the table which have duplicate column 
  813.          names.  The following examples show the difference:
  814.  
  815.          select count(mst) 
  816.          from pm;         
  817.          
  818.           CNT(mst)                             
  819.           --------                               
  820.                 9                              
  821.          
  822.           9 rows selected                        
  823.  
  824.          The above counts all the entries in the mst column.  There are a 
  825.          total of nine.
  826.  
  827.          select count(distinct mst) 
  828.          from pm;  
  829.          
  830.           CNT(mst)                             
  831.           --------
  832.                 5                              
  833.          
  834.           9 rows selected                        
  835.  
  836.          The above is typically more useful.  It answers the question: 
  837.          "How many different states are used for manufacturing products?"
  838.          There is one difference between the syntax for standard SQL and 
  839.          this program, SSQL.  Standard SQL allows count(*).  In SSQL the 
  840.          "*" is not allowed in the count function.  You must have a valid 
  841.          column name. 
  842.  
  843.  
  844.                                    SELECT-14
  845.  
  846.  
  847.  
  848.  
  849.          Before we discuss the other functions, let's look at the 
  850.          percentage of defects in Arizona:
  851.  
  852.          select *                               
  853.          from pm                                 
  854.          where mst = 'AZ';                      
  855.          
  856.           code  desc             mst  defects    
  857.           ----  ---------------  ---  -------
  858.           AC    Gigasnarf        AZ         0    
  859.           DZ    Electrowidgit    AZ              
  860.           EA    nanomouse        AZ        12    
  861.          
  862.           3 rows selected                             
  863.  
  864.          In Arizona, the Gigasnarf has zero defects, the nanomouse has 12% 
  865.          defects but there is nothing listed for the Electrowidgit.  This 
  866.          "nothing" is called a null.  Nulls are important in relational 
  867.          data bases.  It does not mean zero or blank.  A null means that 
  868.          the data element is not relevant in the row.  In our case, 
  869.          Electrowidgits are not tracked for defects like other parts.  
  870.          This isn't to say that there are zero defects.  It just means we 
  871.          have no entry.  Because there are only two valid entries for 
  872.          defects in Arizona, the functions will not take into account the 
  873.          null data element.  The average percentage of defects will be 
  874.          12/2 = 6, not 12/3 = 4.
  875.  
  876.          select avg(defects) 
  877.          from pm 
  878.          where mst='AZ';
  879.          
  880.           AVG(defects)                                 
  881.           ------------                                   
  882.                      6                                 
  883.          
  884.          3 rows selected                                 
  885.          
  886.          select count(defects) 
  887.          from pm 
  888.          where mst = 'AZ';
  889.          
  890.           CNT(defects)                                     
  891.           ------------                                       
  892.                      2                                     
  893.          
  894.           3 rows selected                                    
  895.          
  896.  
  897.  
  898.  
  899.  
  900.  
  901.  
  902.  
  903.                                      SELECT-15
  904.          
  905.          
  906.          
  907.          
  908.          GROUP BY, HAVING
  909.         
  910.          The group by clause is normally used in conjunction with the 
  911.          mathematical functions.  It does two operations:
  912.          1.  It sorts by the column name.
  913.          2.  The mathematical functions only operate based on the rows 
  914.          which have the same column name.  The functions in essence create 
  915.          sub-totals based on the column name.
  916.  
  917.          What if we wanted to know the average percentage of defects in 
  918.          each state?
  919.  
  920.          select mst, avg(defects) 
  921.          from pm 
  922.          group by mst; 
  923.          
  924.           mst  AVG(defects)                               
  925.           -----------------
  926.           AZ              6                               
  927.           ID              3                               
  928.           NV              3                               
  929.           OR             10                               
  930.           WA              3                               
  931.          
  932.           5 rows selected                                    
  933.  
  934.          In SSQL you must select the column name that you use in the group 
  935.          by clause.
  936.  
  937.          You can restrict the group by clause through the use of the 
  938.          "having" component.  In SSQL you are allowed one simple selection 
  939.          which includes the =, >, <, >=, <=, and <>.  As with the group 
  940.          by, the column name in the having component must exist in the 
  941.          select clause.
  942.  
  943.          If you only wanted a list of states with the average percentage 
  944.          of defects over 3%, you would type the following:
  945.  
  946.          select mst, avg(defects) 
  947.          from pm 
  948.          group by mst having avg(defects) > 3;
  949.          
  950.           mst  AVG(defects)   
  951.           ---  ------------
  952.           AZ              6   
  953.           OR             10   
  954.          
  955.           2 rows selected        
  956.  
  957.          The group by clause can have multiple fields.  This is when you 
  958.          want a sort within a sort.  However with such a small table we 
  959.          cannot create anything useful. 
  960.          
  961.          
  962.                                      SELECT-16
  963.          
  964.          
  965.          ORDER BY
  966.  
  967.          The order by clause sorts the table based on the column name(s) 
  968.          listed. As with the group by clause, the order by clause can be 
  969.          used with the where clause.  For example, the following will 
  970.          produce a sorted list of descriptions for those items 
  971.          manufactured in Arizona:
  972.          
  973.          select desc 
  974.          from pm 
  975.          where mst = 'AZ' 
  976.          order by desc;
  977.          
  978.           desc                                                 
  979.           -----------------                                     
  980.           Electrowidgit                                        
  981.           Gigasnarf                                            
  982.           nanomouse                                            
  983.          
  984.           3 rows selected                                        
  985.  
  986.          You can also produce a sort within a sort. The following will 
  987.          produce an ordered list of states and within each state the 
  988.          product descriptions will be sorted: 
  989.  
  990.          select mst, desc 
  991.          from pm 
  992.          order by mst, desc;
  993.          
  994.           mst  desc                                    
  995.           ---  -----------------                        
  996.           AZ   Electrowidgit                           
  997.           AZ   Gigasnarf                               
  998.           AZ   nanomouse                               
  999.           ID   Megawamp                                
  1000.           ID   RGBMouse                                
  1001.           NV   Dynamic Disk                            
  1002.           OR   Technowidgit                            
  1003.           WA   Decawidgit                              
  1004.           WA   Megawamp                                
  1005.          
  1006.           9 rows selected                                 
  1007.  
  1008.          The order by clause assumes ascending order.  You can reverse the 
  1009.          order by adding the descending modifier to the sort. It alway is 
  1010.          put after the the column name it is modifying. 
  1011.  
  1012.          select * 
  1013.          from pm 
  1014.          where defects > 3 
  1015.          order by defects descending; 
  1016.          
  1017.           code  desc             mst  defects                            
  1018.           ----  ---------------  ---  -------
  1019.           EA    nanomouse        AZ        12                            
  1020.           DA    Technowidgit     OR        10                            
  1021.           AB    Megawamp         ID         5                            
  1022.          
  1023.           3 rows selected                                                     
  1024.  
  1025.                                      SELECT-17
  1026.          
  1027.          
  1028.          
  1029.          
  1030.          INTO file_name
  1031.  
  1032.          Normally the report that is produced by the select command is 
  1033.          displayed on the monitor.  However, through the use of the into 
  1034.          clause, the report can be redirected to a text file or to the 
  1035.          printer.  The text file is in standard ASCII text.  That is, 
  1036.          there are no special characters in it.  The file name you give it 
  1037.          has the extension ".txt" added.  Your file name must be one to 
  1038.          eight characters and you cannot have a period "." in the file 
  1039.          name.  
  1040.  
  1041.          If you wanted all the Arizona products stored in a file called 
  1042.          ARIZ and you wanted the report sorted by product description:
  1043.  
  1044.          select * 
  1045.          from pm 
  1046.          where mst='AZ' 
  1047.          into ariz 
  1048.          order by desc;
  1049.          [ 3 found ]                                                  
  1050.          
  1051.          The only response to your query would be the number of rows 
  1052.          selected.  Now it would be stored on your disk under the name 
  1053.          "ARIZ.TXT".  To display the file on the monitor, you would exit 
  1054.          SSQL and type the following AT THE DOS PROMPT.  The report would 
  1055.          be displayed: 
  1056.  
  1057.          A>type ariz.txt
  1058.           code  desc             mst  defects 
  1059.           ----  ---------------  ---  -------
  1060.           DZ    Electrowidgit    AZ           
  1061.           AC    Gigasnarf        AZ         0 
  1062.           EA    nanomouse        AZ        12 
  1063.  
  1064.          You can also have the report printed on the printer.  Replace the 
  1065.          file name with prn or lpt1.  Make sure the printer is on when you 
  1066.          use this option.  For example, the following would print the 
  1067.          report instead of display it on the monitor.
  1068.  
  1069.          select * 
  1070.          from pm 
  1071.          where mst='AZ' 
  1072.          into prn
  1073.          order by desc;
  1074.          [ 3 found ]                                                  
  1075.          
  1076.  
  1077.  
  1078.  
  1079.  
  1080.  
  1081.  
  1082.  
  1083.  
  1084.  
  1085.                                      SELECT-18
  1086.          
  1087.