home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / 1990 / 03 / ssql / select.shr < prev    next >
Encoding:
Text File  |  1988-03-25  |  37.3 KB  |  1,090 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 manufactured outside of Washington and 
  527.          Idaho:
  528.  
  529.          select * 
  530.          from pm 
  531.          where not (mst='WA' or mst='ID');  
  532.          
  533.           code  desc             mst  defects                
  534.           ----  ---------------  ---  -------
  535.           AC    Gigasnarf        AZ         0                
  536.           DA    Technowidgit     OR        10                
  537.           DZ    Electrowidgit    AZ                          
  538.           EA    nanomouse        AZ        12                
  539.           FA    Dynamic Disk     NV         3                
  540.          
  541.          5 rows selected                                          
  542.          
  543.                                    SELECT-9
  544.  
  545.          
  546.          This query finds products that may be manufactured in Washington 
  547.          or Idaho but they are manufactured in other states.  It just so 
  548.          happens that in this small set of values, there is none.
  549.  
  550.          Notice that is not the same as finding the products not 
  551.          manufactured in Washington or Idaho.  In this query we would 
  552.          have to find all the products manufactured in Washington or 
  553.          Idaho and then find those product code not in that set.  Getting 
  554.          a bit complicated, huh?  To solve this problem, a subquery is 
  555.          needed.
  556.          
  557.          Normally, if a condition is true, we add that row to the table.  
  558.          By using the not, if the condition is false, we add the row to 
  559.          the table.  On the first row above, the state is AZ.  It was 
  560.          selected because it is not equal to WA or ID.  Just as we can 
  561.          express the same thought in English more than one way, we can do 
  562.          the same with SQL.  We could have written:
  563.  
  564.          select * 
  565.          from pm 
  566.          where mst <> 'WA' and mst <> 'ID';
  567.  
  568.          or
  569.  
  570.          select * 
  571.          from pm 
  572.          where not (mst = 'WA') and not (mst = 'ID');
  573.  
  574.          What if we would have written "or" instead of "and" in the above 
  575.          two examples?  Every row would have been selected.  No matter 
  576.          what the state is, it would either be not equal to WA or not 
  577.          equal to ID!
  578.  
  579.          Remember that whenever you use the not, you must enclose the 
  580.          expression in parentheses.
  581.  
  582.          You can have as many ands and ors in a query as you need.  What 
  583.          if you wanted all products in Washington and Idaho with defects 
  584.          over 3%:
  585.  
  586.          select * 
  587.          from pm 
  588.          where (mst='WA' or mst='ID') and defects > 3;
  589.          
  590.           code  desc             mst  defects                          
  591.           ----  ---------------  ---  -------
  592.           AB    Megawamp         ID         5                          
  593.          
  594.           1 row selected                                                    
  595.  
  596.          We find that only one product is selected.  Notice that 
  597.          parentheses enclose the or expression.  This is because without 
  598.          the parentheses the and expression would have been executed 
  599.          first.  Remember that without parentheses  the ands are processed 
  600.          first and the ors are processed next. Not inserting the 
  601.          parentheses would have changed the meaning considerably:
  602.  
  603.  
  604.                                    SELECT-10
  605.       
  606.  
  607.  
  608.  
  609.          select * 
  610.          from pm 
  611.          where mst='WA' or mst='ID' and defects > 3;  
  612.          
  613.           code  desc             mst  defects                          
  614.           ----  ---------------  ---  -------
  615.           AB    Megawamp         WA         3                          
  616.           DD    Decawidgit       WA         3                          
  617.           AB    Megawamp         ID         5                          
  618.          
  619.           3 rows selected                                                   
  620.  
  621.          The above query is the equivalent of saying, "List all products 
  622.          in Washington and list all products in Idaho with defects over 
  623.          3%."   Another way of stating it is "In looking through all the 
  624.          products, if a product is manufactured in Washington or an Idaho 
  625.          product has over 3% defects, add it to the list."  The important 
  626.          concept is that the 3% defects relates to the products 
  627.          manufactured in Idaho, not all products. 
  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.  
  660.  
  661.  
  662.                                    SELECT-11
  663.  
  664.  
  665.  
  666.  
  667.          SETS (used with constants)
  668.  
  669.          Instead of relating items one at a time, we can relate groups of 
  670.          items at once.  These groups are called sets.  They are enclosed 
  671.          by parentheses and the constants are separated by commas.
  672.  
  673.          ANY
  674.  
  675.          You saw above how you can use =, >, <, <>, >=, <=.  We can expand 
  676.          that concept to include "any" and "all".  As you will see, the 
  677.          "any" corresponds to an "or" and the "all" corresponds to an 
  678.          "and".
  679.          
  680.          All possible combinations:
  681.  
  682.          =any means equal to any
  683.          >any means greater than any
  684.          <any means less than any
  685.          <>any means not equal to any
  686.          >=any means greater than or equal to any
  687.          <=any means less than or equal to any
  688.          
  689.          Let's rewrite the query concerning all products manufactured in 
  690.          Washington or Idaho: 
  691.  
  692.          select desc 
  693.          from pm 
  694.          where mst =any ('WA','ID');   
  695.          
  696.           desc                                                
  697.           -----------------                                    
  698.           Megawamp                                            
  699.           Decawidgit                                          
  700.           RGBMouse                                            
  701.           Megawamp                                            
  702.          
  703.           3 rows selected                                       
  704.  
  705.          Translated into English it would be:  Select the description from 
  706.          the  p(roduct) table where the state of manufacture is equal to 
  707.          any of the following - WA or ID.
  708.  
  709.          We can use numbers too.  What if you wanted to know the names of 
  710.          products that have a percentage of defects of 2% or 3%:
  711.  
  712.          select desc 
  713.          from pm 
  714.          where defects =any (2,3);
  715.          
  716.           desc                                           
  717.           -----------------                               
  718.           Megawamp                                       
  719.           Decawidgit                                     
  720.           RGBMouse                                       
  721.           Dynamic Disk                                   
  722.          
  723.           4 rows selected                                  
  724.                                    SELECT-12
  725.  
  726.  
  727.  
  728.  
  729.          IN
  730.  
  731.          An equivalent to =any is in.  We will see that sets are much more 
  732.          useful when we discuss using nested selects and multiple tables.  
  733.          However, you could translate the above query to: 
  734.  
  735.          select desc 
  736.          from pm 
  737.          where defects in (2,3);
  738.  
  739.  
  740.          ALL
  741.  
  742.          The "all" is similar to the "any" except that instead of the 
  743.          items being connected by "or's", they are connected by "and's".  
  744.          It is useful in determining a group of items outside a certain 
  745.          set.  If you wanted to find all products manufactured outside 
  746.          of WA and ID:
  747.  
  748.          select *
  749.          from pm
  750.          where mst !=all ('WA','ID');
  751.  
  752.  
  753.          BETWEEN constant AND constant
  754.  
  755.          An easy way to select a range of values is use the between/and 
  756.          component of the where clause.  If you wanted to find the product 
  757.          descriptions with defects between 3% and 10%:
  758.  
  759.          select desc, defects 
  760.          from pm 
  761.          where defects between 3 and 10; 
  762.          
  763.           desc             defects                                      
  764.           ---------------  -------                                         
  765.           Megawamp               3                                      
  766.           Decawidgit             3                                      
  767.           Technowidgit          10                                      
  768.           Dynamic Disk           3                                      
  769.           Megawamp               5                                      
  770.          
  771.           5 rows selected                                                  
  772.  
  773.          
  774.          Notice that when we use the between, we include the 3 and the 10.  
  775.          Unfortunately, the word "between" can have more than one meaning in 
  776.          standard English. If you say "The treasure is located between San 
  777.          Francisco and Walnut Creek", you would assume that the treasure 
  778.          is not IN San Francisco or Walnut Creek.  That is, you would not 
  779.          include the end points.  If you say "This insurance plan is 
  780.          available for those people between the ages of 18 and 65", you 
  781.          would assume that if you are 18 or 65, you are eligible.  That 
  782.          is, you would include the end points.  Just remember that in SQL, 
  783.          between includes the end points. 
  784.  
  785.  
  786.                                      SELECT-13
  787.          
  788.          
  789.          
  790.          
  791.          MATHEMATICAL FUNCTIONS - AVG, MIN, MAX, SUM, COUNT
  792.  
  793.          All of the mathematical functions operate on numeric columns 
  794.          except count which will operate on alphanumeric columns.
  795.  
  796.          SYNTAX:
  797.  
  798.          AVG([DISTINCT] column_name)
  799.          MIN(column_name)
  800.          MAX(column_name)
  801.          SUM([DISTINCT]column_name)
  802.          COUNT([DISTINCT]column_name)
  803.          
  804.          SUMMARY
  805.          AVG   - without DISTINCT: The average of all values in a column
  806.                  with DISTINCT   : The average of non-duplicate values
  807.          MIN   - The minimum value in the column
  808.          MAX   - The maximum value in the column
  809.          SUM   - without DISTINCT: The sum of all values in the column
  810.                  with DISTINCT   : The sum of non-duplicate values
  811.          COUNT - without DISTINCT: the number of column entries
  812.                  with DISTINCT   : the number of non-duplicate column entries
  813.          
  814.          Notice that some of the functions have the distinct option.  This 
  815.          will delete all rows in the table which have duplicate column 
  816.          names.  The following examples show the difference:
  817.  
  818.          select count(mst) 
  819.          from pm;         
  820.          
  821.           CNT(mst)                             
  822.           --------                               
  823.                 9                              
  824.          
  825.           9 rows selected                        
  826.  
  827.          The above counts all the entries in the mst column.  There are a 
  828.          total of nine.
  829.  
  830.          select count(distinct mst) 
  831.          from pm;  
  832.          
  833.           CNT(mst)                             
  834.           --------
  835.                 5                              
  836.          
  837.           9 rows selected                        
  838.  
  839.          The above is typically more useful.  It answers the question: 
  840.          "How many different states are used for manufacturing products?"
  841.          There is one difference between the syntax for standard SQL and 
  842.          this program, SSQL.  Standard SQL allows count(*).  In SSQL the 
  843.          "*" is not allowed in the count function.  You must have a valid 
  844.          column name. 
  845.  
  846.  
  847.                                    SELECT-14
  848.  
  849.  
  850.  
  851.  
  852.          Before we discuss the other functions, let's look at the 
  853.          percentage of defects in Arizona:
  854.  
  855.          select *                               
  856.          from pm                                 
  857.          where mst = 'AZ';                      
  858.          
  859.           code  desc             mst  defects    
  860.           ----  ---------------  ---  -------
  861.           AC    Gigasnarf        AZ         0    
  862.           DZ    Electrowidgit    AZ              
  863.           EA    nanomouse        AZ        12    
  864.          
  865.           3 rows selected                             
  866.  
  867.          In Arizona, the Gigasnarf has zero defects, the nanomouse has 12% 
  868.          defects but there is nothing listed for the Electrowidgit.  This 
  869.          "nothing" is called a null.  Nulls are important in relational 
  870.          data bases.  It does not mean zero or blank.  A null means that 
  871.          the data element is not relevant in the row.  In our case, 
  872.          Electrowidgits are not tracked for defects like other parts.  
  873.          This isn't to say that there are zero defects.  It just means we 
  874.          have no entry.  Because there are only two valid entries for 
  875.          defects in Arizona, the functions will not take into account the 
  876.          null data element.  The average percentage of defects will be 
  877.          12/2 = 6, not 12/3 = 4.
  878.  
  879.          select avg(defects) 
  880.          from pm 
  881.          where mst='AZ';
  882.          
  883.           AVG(defects)                                 
  884.           ------------                                   
  885.                      6                                 
  886.          
  887.          3 rows selected                                 
  888.          
  889.          select count(defects) 
  890.          from pm 
  891.          where mst = 'AZ';
  892.          
  893.           CNT(defects)                                     
  894.           ------------                                       
  895.                      2                                     
  896.          
  897.           3 rows selected                                    
  898.          
  899.  
  900.  
  901.  
  902.  
  903.  
  904.  
  905.  
  906.                                      SELECT-15
  907.          
  908.          
  909.          
  910.          
  911.          GROUP BY, HAVING
  912.         
  913.          The group by clause is normally used in conjunction with the 
  914.          mathematical functions.  It does two operations:
  915.          1.  It sorts by the column name.
  916.          2.  The mathematical functions only operate based on the rows 
  917.          which have the same column name.  The functions in essence create 
  918.          sub-totals based on the column name.
  919.  
  920.          What if we wanted to know the average percentage of defects in 
  921.          each state?
  922.  
  923.          select mst, avg(defects) 
  924.          from pm 
  925.          group by mst; 
  926.          
  927.           mst  AVG(defects)                               
  928.           -----------------
  929.           AZ              6                               
  930.           ID              3                               
  931.           NV              3                               
  932.           OR             10                               
  933.           WA              3                               
  934.          
  935.           5 rows selected                                    
  936.  
  937.          In SSQL you must select the column name that you use in the group 
  938.          by clause.
  939.  
  940.          You can restrict the group by clause through the use of the 
  941.          "having" component.  In SSQL you are allowed one simple selection 
  942.          which includes the =, >, <, >=, <=, and <>.  As with the group 
  943.          by, the column name in the having component must exist in the 
  944.          select clause.
  945.  
  946.          If you only wanted a list of states with the average percentage 
  947.          of defects over 3%, you would type the following:
  948.  
  949.          select mst, avg(defects) 
  950.          from pm 
  951.          group by mst having avg(defects) > 3;
  952.          
  953.           mst  AVG(defects)   
  954.           ---  ------------
  955.           AZ              6   
  956.           OR             10   
  957.          
  958.           2 rows selected        
  959.  
  960.          The group by clause can have multiple fields.  This is when you 
  961.          want a sort within a sort.  However with such a small table we 
  962.          cannot create anything useful. 
  963.          
  964.          
  965.                                      SELECT-16
  966.          
  967.          
  968.          ORDER BY
  969.  
  970.          The order by clause sorts the table based on the column name(s) 
  971.          listed. As with the group by clause, the order by clause can be 
  972.          used with the where clause.  For example, the following will 
  973.          produce a sorted list of descriptions for those items 
  974.          manufactured in Arizona:
  975.          
  976.          select desc 
  977.          from pm 
  978.          where mst = 'AZ' 
  979.          order by desc;
  980.          
  981.           desc                                                 
  982.           -----------------                                     
  983.           Electrowidgit                                        
  984.           Gigasnarf                                            
  985.           nanomouse                                            
  986.          
  987.           3 rows selected                                        
  988.  
  989.          You can also produce a sort within a sort. The following will 
  990.          produce an ordered list of states and within each state the 
  991.          product descriptions will be sorted: 
  992.  
  993.          select mst, desc 
  994.          from pm 
  995.          order by mst, desc;
  996.          
  997.           mst  desc                                    
  998.           ---  -----------------                        
  999.           AZ   Electrowidgit                           
  1000.           AZ   Gigasnarf                               
  1001.           AZ   nanomouse                               
  1002.           ID   Megawamp                                
  1003.           ID   RGBMouse                                
  1004.           NV   Dynamic Disk                            
  1005.           OR   Technowidgit                            
  1006.           WA   Decawidgit                              
  1007.           WA   Megawamp                                
  1008.          
  1009.           9 rows selected                                 
  1010.  
  1011.          The order by clause assumes ascending order.  You can reverse the 
  1012.          order by adding the descending modifier to the sort. It alway is 
  1013.          put after the the column name it is modifying. 
  1014.  
  1015.          select * 
  1016.          from pm 
  1017.          where defects > 3 
  1018.          order by defects descending; 
  1019.          
  1020.           code  desc             mst  defects                            
  1021.           ----  ---------------  ---  -------
  1022.           EA    nanomouse        AZ        12                            
  1023.           DA    Technowidgit     OR        10                            
  1024.           AB    Megawamp         ID         5                            
  1025.          
  1026.           3 rows selected                                                     
  1027.  
  1028.                                      SELECT-17
  1029.          
  1030.          
  1031.          
  1032.          
  1033.          INTO file_name
  1034.  
  1035.          Normally the report that is produced by the select command is 
  1036.          displayed on the monitor.  However, through the use of the into 
  1037.          clause, the report can be redirected to a text file or to the 
  1038.          printer.  The text file is in standard ASCII text.  That is, 
  1039.          there are no special characters in it.  The file name you give it 
  1040.          has the extension ".txt" added.  Your file name must be one to 
  1041.          eight characters and you cannot have a period "." in the file 
  1042.          name.  
  1043.  
  1044.          If you wanted all the Arizona products stored in a file called 
  1045.          ARIZ and you wanted the report sorted by product description:
  1046.  
  1047.          select * 
  1048.          from pm 
  1049.          where mst='AZ' 
  1050.          into ariz 
  1051.          order by desc;
  1052.          [ 3 found ]                                                  
  1053.          
  1054.          The only response to your query would be the number of rows 
  1055.          selected.  Now it would be stored on your disk under the name 
  1056.          "ARIZ.TXT".  To display the file on the monitor, you would exit 
  1057.          SSQL and type the following AT THE DOS PROMPT.  The report would 
  1058.          be displayed: 
  1059.  
  1060.          A>type ariz.txt
  1061.           code  desc             mst  defects 
  1062.           ----  ---------------  ---  -------
  1063.           DZ    Electrowidgit    AZ           
  1064.           AC    Gigasnarf        AZ         0 
  1065.           EA    nanomouse        AZ        12 
  1066.  
  1067.          You can also have the report printed on the printer.  Replace the 
  1068.          file name with prn or lpt1.  Make sure the printer is on when you 
  1069.          use this option.  For example, the following would print the 
  1070.          report instead of display it on the monitor.
  1071.  
  1072.          select * 
  1073.          from pm 
  1074.          where mst='AZ' 
  1075.          into prn
  1076.          order by desc;
  1077.          [ 3 found ]                                                  
  1078.          
  1079.  
  1080.  
  1081.  
  1082.  
  1083.  
  1084.  
  1085.  
  1086.  
  1087.  
  1088.                                      SELECT-18
  1089.          
  1090.