home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / sql / ssql.doc < prev    next >
Text File  |  1990-01-20  |  164KB  |  4,458 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.                                      SSQL
  20.                                   VERSION 2.2
  21.  
  22.  
  23.                        COPYRIGHT (C) 1990 BY STEVE SILVA
  24.     
  25.                             SILVAWARE
  26.                             3902 NORTH 87TH STREET
  27.                             SCOTTSDALE, AZ 85251
  28.  
  29.  
  30.  
  31.                      Special thanks to the hard-working 
  32.                      students in CIS425 at the DeVry 
  33.                      Institute of Technology, Phoenix, Arizona.
  34.  
  35.  
  36.  
  37.  
  38.          ***************************************************************
  39.          ** READ THIS!!!!!!!                                          **
  40.          ** IF YOU HAVE A PREVIOUS VERSION OF SSQL, YOU NEED CONVERT  **
  41.          ** YOUR FILES.  FROM YOUR DIRECTORY (or disk) THAT CONTAINS  **
  42.          ** THE .SQL FILES, TYPE A:CONVERT (assuming that the new     **
  43.          ** new SSQL disk is in drive A).                             **
  44.          ***************************************************************
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.          dBase is a trademark of Ashton-Tate.
  60.  
  61.  
  62.                                  TABLE OF CONTENTS
  63.          
  64.          
  65.          INTRODUCTION  . . . . . . . . . . . . . . . . . . . . . . . INTRO-1
  66.            Key Words Needed to Understand the Documentation  . . . . INTRO-1
  67.            What Is SQL And Why Is It So Important To Know? . . . . . INTRO-1
  68.            How Does This Implementation Of SQL Differ From Others? . INTRO-3
  69.            Strengths and Weaknesses of SSQL  . . . . . . . . . . . . INTRO-4
  70.            Differences In The Registered Version . . . . . . . . . . INTRO-4
  71.            Install SSQL  . . . . . . . . . . . . . . . . . . . . . . INTRO-6
  72.            Permission to copy  . . . . . . . . . . . . . . . . . . . INTRO-6
  73.            Changes from 1.4 to 2.2 . . . . . . . . . . . . . . . . . INTRO-7
  74.            Syntax Diagrams . . . . . . . . . . . . . . . . . . . . . INTRO-7
  75.            SSQL Specifications . . . . . . . . . . . . . . . . . . . INTRO-8
  76.            Contents of Disk  . . . . . . . . . . . . . . . . . . . . INTRO-8
  77.            dBase Compatibility . . . . . . . . . . . . . . . . . . . INTRO-9
  78.         
  79.          THE EXAMPLE DATABASES . . . . . . . . . . . . . . . . . . . DATABASE-1
  80.          USING YOUR OWN WORD PROCESSOR FROM WITHIN SSQL  . . . . . . WORD-1
  81.          USING SCRIPT FILES  . . . . . . . . . . . . . . . . . . . . SCRIPT-1
  82.          
  83.          EXTRACTING DATA FROM A SINGLE TABLE . . . . . . . . . . . . SELECT-1
  84.            Distinct  . . . . . . . . . . . . . . . . . . . . . . . . SELECT-2
  85.            Where . . . . . . . . . . . . . . . . . . . . . . . . . . SELECT-3
  86.            search_expression . . . . . . . . . . . . . . . . . . . . SELECT-3
  87.            Special Search Expression - is null, is not null  . . . . SELECT-5
  88.            Special Search Expression - like, not like  . . . . . . . SELECT-6
  89.            And, Or, Not  . . . . . . . . . . . . . . . . . . . . . . SELECT-7
  90.            Any . . . . . . . . . . . . . . . . . . . . . . . . . . . SELECT-11
  91.            In. . . . . . . . . . . . . . . . . . . . . . . . . . . . SELECT-12
  92.            All . . . . . . . . . . . . . . . . . . . . . . . . . . . SELECT-12
  93.            Between constant AND constant . . . . . . . . . . . . . . SELECT-13
  94.            Importance of the NULL (Unknown value)  . . . . . . . . . SELECT-13
  95.            Mathmatical Functions Avg, Min, Max, Sum, Count . . . . . SELECT-15
  96.            Column Calculations (+, -, *, /)  . . . . . . . . . . . . SELECT-18
  97.            Group by, Having  . . . . . . . . . . . . . . . . . . . . SELECT-19
  98.            Order by  . . . . . . . . . . . . . . . . . . . . . . . . SELECT-20
  99.            Redirectto  . . . . . . . . . . . . . . . . . . . . . . . SELECT-21
  100.       
  101.          JOINING . . . . . . . . . . . . . . . . . . . . . . . . . . JOIN-1
  102.          SIMPLE SUBQUERIES . . . . . . . . . . . . . . . . . . . . . SUB-1
  103.          CORRELATED SUBQUERIES . . . . . . . . . . . . . . . . . . . COR-1
  104.          CONVERTING ENGLISH TO SQL . . . . . . . . . . . . . . . . . ENG-1
  105.          CREATE A TABLE  . . . . . . . . . . . . . . . . . . . . . . CREATE-1
  106.          CREATE A VIEW . . . . . . . . . . . . . . . . . . . . . . . VIEW-1
  107.          DROP A TABLE/VIEW . . . . . . . . . . . . . . . . . . . . . DROP-1
  108.          INSERT DATA INTO A TABLE  . . . . . . . . . . . . . . . . . INSERT-1
  109.          UPDATE DATA IN A TABLE  . . . . . . . . . . . . . . . . . . UPDATE-1
  110.          DELETE DATA FROM A TABLE  . . . . . . . . . . . . . . . . . DELETE-1
  111.          TUTORIAL  . . . . . . . . . . . . . . . . . . . . . . . . . TUTORIAL-1
  112.  
  113. ******** ORDER FORM  * * * * * * * * * * * * * * * * * * * * * * * * LAST PAGE
  114.          
  115.          
  116.          
  117.          KEY WORDS NEEDED TO UNDERSTAND THE DOCUMENTATION
  118.  
  119.          SQL - Structured Query Language. A standard method of 
  120.          interacting with a database.  It is pronounced "SEQUEL"!!  
  121.          However,  over the years I have noticed more and more people 
  122.          saying the letters S-Q-L.
  123.          
  124.          TABLE - A table is typically known as a FILE in other systems.
  125.          You may ask why they don't just call a table a file.  It is 
  126.          because that although normally a table does refer to a specific 
  127.          file, a table can refer to something that spans two or more 
  128.          files.  This can be done by "creating a view" (see 
  129.          documentation). If you read a book on relational databases, they 
  130.          will probably refer to a table as a relation. 
  131.  
  132.          ROW - A row corresponds to a record or a portion of a record in 
  133.          a file.  In relational theory it is called a tuple.
  134.  
  135.          COLUMN - A column is typically known as a field in other 
  136.          systems.  In relational theory it is called an attribute.
  137.  
  138.          The above names were created to give relational databases a 
  139.          consistent and accurate view of data.
  140.  
  141.          EXAMPLE:
  142.  
  143.          You may have a TABLE named sales which contains COLUMNs called 
  144.          date, custnum, partnum and quantity.  Every time you made a 
  145.          sale, you would add a ROW of data to the TABLE.
  146.  
  147.                       COLUMNS
  148.            -----------------------------
  149.           |         |        |         |
  150.          date    custnum  partnum  quantity
  151.          ------  -------  -------  --------
  152.          880201  8524     AD873         928       <-- ROW
  153.          880203  7687     VF8709         87       <-- ROW
  154.  
  155.          ----------------------------------
  156.                         ^
  157.                         |
  158.                       TABLE 
  159.  
  160.          WHAT IS SQL AND WHY IS IT SO IMPORTANT TO KNOW?
  161.          
  162.          SQL stands for Structured Query Language.  It was developed as 
  163.          a standard method to query (extract data from) a relational 
  164.          database and do other operations to maintain relational 
  165.          databases.  Relational databases look at files as if they were 
  166.          simply tables.  SQL was developed years ago at a theoretical 
  167.          level but because of its inherent inefficiencies and programming 
  168.          complexity, it has been very difficult to create workable programs.  
  169.          It provides the most flexible approach to extracting data from a 
  170.          database.  It allows us to extract data in seconds that would 
  171.          take a knowledgeable programmer hours, days or weeks to extract, 
  172.          even if the programmer had the most advanced non-SQL languages 
  173.          available.  For a sampling of the types of queries that SQL can 
  174.          handle, refer to Appendix A.
  175.  
  176.                                       INTRO-1
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.          Most database management systems on PCs, minicomputers, and 
  184.          mainframes have an SQL interface.  So far, there is no "better" 
  185.          language waiting to replace SQL so once you learn SQL, it 
  186.          should serve you for many years.
  187.          
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.                                   INTRO-2
  237.          
  238.          HOW DOES THIS IMPLEMENTATION DIFFER FROM THE OTHERS?
  239.          The following is a table from the January, 1988 issue of BYTE.  
  240.          I have added SSQL to the end of the table for comparison: 
  241.  
  242.          SQL Command  Informix  Ingres  Oracle  SQLBase  XDB   XQL    SSQL
  243.                         2.0      5.0     5.1     3.2.2    II   1.0     2.1
  244.                        ($795)   ($950)  ($1295)  ($995) ($395) ($795) ($30)
  245.          DML (Data Manipulation Language)
  246.          SELECT         Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  247.            COLUMNS      Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  248.            EXPRESSIONS  Yes      Yes      Yes     Yes    Yes    No     Yes
  249.          DISTINCT       Yes      Yes      Yes     Yes    Yes    No     Yes 
  250.          FROM           Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  251.          WHERE          Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  252.          GROUP BY       Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  253.          HAVING         Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  254.          ORDER BY       Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  255.          SUBQUERIES     Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  256.          UPDATE SET     Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  257.          WHERE          Yes      Yes      Yes     Yes    Yes    Yes    Yes  
  258.          SUBQUERIES     Yes      Yes      Yes     Yes    Yes    No     Yes 
  259.          INSERT INTO    Yes      Yes      Yes     Yes    Yes    Yes     1  
  260.          SUBQUERY       Yes      Yes      Yes     Yes    Yes    No     Yes  
  261.          DELETE FROM    Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  262.          SUBQUERIES     Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  263.          UNION          Yes      Yes      Yes     Yes    Yes    No     Yes
  264.          CORRELATED -                                                      
  265.          SUBQUERIES     Yes      Yes      Yes     Yes    Yes    No     Yes  
  266.          DML Predicates 
  267.          BETWEEN        Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  268.          LIKE           Yes      Yes      Yes     Yes    Yes    No     Yes 
  269.          IS NULL        Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  270.          EXISTS         Yes      Yes      Yes     Yes    Yes    No     Yes  
  271.          ALL            Yes      Yes      Yes     Yes    Yes    No     Yes 
  272.          ANY            Yes      Yes      Yes     Yes    Yes    No     Yes 
  273.          SOME           No       No       No      No     No     No     No  
  274.          [NOT]          Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  275.          DML Functions 
  276.          AVG            Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  277.          COUNT(*)       Yes      Yes      Yes     Yes    Yes    No     Yes  
  278.          COUNT          Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  279.          MAX            Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  280.          MIN            Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  281.          SUM            Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  282.          DDL (Data Definition Language)
  283.          ALTER TABLE    Yes      Yes      Yes     Yes    Yes    Yes     2  
  284.          CREATE TABLE   Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  285.            NOT NULL     Yes      Yes      Yes     Yes    Yes    No     Yes  
  286.          CREATE INDEX   Yes      Yes      Yes     Yes    Yes    Yes    No  
  287.          CREATE UNIQUE
  288.          INDEX          Yes      Yes      Yes     Yes    Yes    No     No  
  289.          CREATE VIEW    Yes      Yes      Yes     Yes    Yes    Yes    Yes
  290.          DROP TABLE     Yes      Yes      Yes     Yes    Yes    Yes    Yes
  291.          DROP INDEX     Yes      Yes      Yes     Yes    Yes    Yes    No  
  292.  
  293.          1. Similar to the standard except that you cannot use a 
  294.          calculation to update a set of rows.
  295.          2. Although the syntax is different, You can delete columns in a 
  296.          table, change the names of the columns, change the size and data 
  297.          type of a column, etc.
  298.                                       INTRO-3
  299.          
  300.          
  301.          
  302.          STRENGTHS AND WEAKNESSES OF SSQL
  303.  
  304.          My emphasis has been on the Data Manipulation Language since 
  305.          that is the most difficult to master and it is the most useful 
  306.          to the end-user.  Since the current version of SSQL cannot 
  307.          create indexes, joining large tables tends to be slow in 
  308.          comparison.  When joining tables, SSQL on a PC with a RAM disk 
  309.          can evaluate about 750-800 rows/minute.  On a PC AT with a hard 
  310.          disk it is about 3200 rows/minute.  However, my next major 
  311.          project will be to incorporate the use of index files which 
  312.          should increase the speed dramatically.
  313.  
  314.          I don't think SSQL can be touched on a price/performance basis 
  315.          though.  I use Oracle 5.1 - the stack of documentation is over 
  316.          a foot high and it requires a PC AT with one megabyte of 
  317.          extended memory.  It is an excellent package but not everybody 
  318.          needs the power of a $1,295 product. 
  319.  
  320.          SSQL documentation is oriented toward the end-user, not the
  321.          programmer.  There are nearly 400 pages!!!!  It contains many 
  322.          detailed explanations and examples. 
  323.  
  324.          DIFFERENCES IN THE REGISTERED VERSION
  325.          
  326.          PROGRAM
  327.          There is NO difference in the programs except that registered 
  328.          users are assured of getting the most current version.
  329.  
  330.          DISK-BASED DOCUMENTATION (UNREGISTERED USERS)
  331.          In order to fit everything on a single disk, there is only 
  332.          enough documentation on some commands to explain the basics. 
  333.          However, it does show all the ANSI SQL commands so you can see 
  334.          for yourself that the commands actually work. You do not get 
  335.          documentation on most of the utilities that make working in 
  336.          this environment a bit easier and allow you, for example, to 
  337.          output any query to a dBase file which gives you the ability to 
  338.          customize reports. 
  339.  
  340.          FULL DOCUMENTATION (REGISTERED USERS)
  341.  
  342.          Actually, it is much more than just documentation.  It is a 
  343.          book with nearly 400 pages.  It was produced with a desk-top 
  344.          publishing package, printed on a laser printer, and bound. 
  345.  
  346.          The name of the book is APPLYING SQL.  Besides explaining all 
  347.          the details of syntax, it shows the common constructs used in 
  348.          SQL to make creating your own queries much easier.  Much of 
  349.          each chapter is centered around translating English into SQL 
  350.          and how to note peculiarities and ambiguities in English that 
  351.          can cause problems in writing queries.
  352.  
  353.          Support is offered by mail or through CompuServe (73177,2771).
  354.  
  355.  
  356.  
  357.                                       INTRO-4
  358.          
  359.  
  360.  
  361.  
  362.  
  363.  
  364.          One of the most complex (and useful) topics in SQL is the 
  365.          correlated subquery.  Most authors give you a page or two on 
  366.          the topic.  That is not nearly enough for you to be able to use 
  367.          it on your own.  My chapter on correlated subqueries is over 40 
  368.          pages.  One good gauge on the quality of a book on SQL is how 
  369.          well it explains the correlated subquery and how well it 
  370.          conveys the importance of the topic.  It is an essential part 
  371.          of SQL that opens up various categories of queries that cannot 
  372.          be accomplished any other way.
  373.          
  374.          The last chapter on queries is CONVERTING ENGLISH TO SQL.  It 
  375.          gives you over 50 pages on approaches to converting English to 
  376.          SQL and dozens of examples.  It explains how to break down a 
  377.          sentence in English that simplifies its conversion.  Many of 
  378.          the common words used in queries are analyzed.  As is often the 
  379.          case with English, the same word in different contexts can 
  380.          significantly change the meaning of the sentence and its 
  381.          conversion to SQL.  Much of the chapter explains how to convert 
  382.          sentences with negation which can be VERY tricky.  For 
  383.          example, let's anylze the following query:
  384.          
  385.          List the purchase dates when all of the following products were 
  386.          purchased: MW, NM, GC. 
  387.          
  388.          The query, although a bit complex when solving it SQL, is 
  389.          nonetheless rather straightforward.  When we negate the query 
  390.          it gets even more confusing (and realistic!!): 
  391.  
  392.          List the purchase dates when all of the following products were 
  393.          NOT purchased: MW, NM, GC. 
  394.          (if all the products were purchased, skip the date) 
  395.  
  396.          List the purchase dates when NONE of the following products were 
  397.          purchased: MW, NM, GC.
  398.          (if any of the products were purchased, skip the date)
  399.  
  400.          List the purchase dates when something other than the following 
  401.          products were purchased: MW, NM, GC. 
  402.  
  403.          Once you see the solutions to these queries you will better 
  404.          understand why SQL is "structured" and it should give you a 
  405.          more critical understanding of English queries.
  406.  
  407.          The full chapters on the joins, and subqueries have many more 
  408.          examples and detailed explanations.
  409.  
  410.          The power of SQL can be TOTALLY lost if the user does not 
  411.          understand the basics of data normalization. Normalization 
  412.          involves the rules for creating tables.  If the tables are not 
  413.          organized correctly, SQL cannot be used to its full potential.  
  414.          It is important to note that the topic is discussed with the 
  415.          non-technical end-user in mind.  Since there have not been any 
  416.          widely available SQL program, all the books on normalization 
  417.          tend to be very theoretical and academically oriented.  
  418.  
  419.  
  420.                                      INTRO-5            
  421.  
  422.  
  423.  
  424.  
  425.          
  426.          You also get full information on utilities to delete columns from 
  427.          tables, modify column names, change the width of columns, create 
  428.          tables which are subsets of a current table, etc. 
  429.  
  430.          
  431.          INSTALL SSQL
  432.          There is no installation per se.  Normally you just want to 
  433.          copy the contents of the distribution disk into a subdirectory 
  434.          on your hard disk.  If you have a floppy disk system put the  
  435.          SSQL disk in drive A and a blank formatted disk in drive B.  
  436.          With the DOS prompt on drive A type:
  437.          COPY SSQL.EXE B:
  438.          COPY ERROR.DAT B:
  439.          COPY *.DBF B:
  440.          COPY *.SQD B:
  441.  
  442.          The only things you have to remember is that SSQL.EXE and 
  443.          ERROR.DAT have to be in the same directory and the database 
  444.          files must be in your default directory.
  445.          Here is some more information about the files on the 
  446.          distribution disk.. 
  447.          SSQL.EXE - The executable program. 
  448.          ERROR.DAT - contains the error messages
  449.          The other files (tables, views, etc.) must be in your current 
  450.          directory.  In essence, your directory corresponds to your 
  451.          complete database. 
  452.          MEANING OF FILE EXTENSIONS
  453.          DBF - The dbase files.  On the distribution disk, you have 
  454.          BRANCH.DBF, SALES.DBF, PROD.DBF, MANU.DBF, EMP.DBF and 
  455.          CUST.DBF.
  456.          SQD - Definition files.  There is one for every DBF file.
  457.          VIE - The view files. These are text files that contain SELECT 
  458.          statements which form the views.
  459.          TXT - Table output to disk.  These files are created when you 
  460.          use the REDIRECTTO option in the SELECT command to save your 
  461.          report in a disk file instead of sending it to the printer.
  462.          EDIT.FIL - This contains the command to start your word 
  463.          processor or text editor from within SSQL.  See the section on 
  464.          USING YOUR OWN WORD PROCESSOR for more details on this. 
  465.          
  466.          You can put SSQL.EXE and ERROR.DAT in a single directory and 
  467.          set a path to it. You can then have various other directories 
  468.          for the tables. 
  469.          
  470.          PERMISSION TO COPY
  471.  
  472.          Please copy this disk and give it to a friend (or anybody else).
  473.          However, the following restrictions apply:
  474.          1.  No changes can be made to the distribution disk, including the 
  475.          documentation.
  476.          2. You cannot copy or reproduce the printed manual.  
  477.          
  478.          
  479.          
  480.                               INTRO-6
  481.  
  482.  
  483.  
  484.          Any commercial, educational, governmental and other such 
  485.          organizations are required to purchase a copy of SSQL for every 
  486.          building it is used in. 
  487.  
  488.          Quantity discounts available.
  489.  
  490.          CHANGES FROM 1.4 TO 2.2
  491.  
  492.          Bug fixes too numerous to mention.
  493.          dBase compatible files
  494.          Row width increased from 132 to 500 characters
  495.          Create table, create view, insert, update and delete are ANSI 
  496.          standard 
  497.          Add count(*)
  498.          Addition of drop table, drop view, and correlated subqueries 
  499.          Ability to use calculations in the select list, in the where 
  500.          clause, and update
  501.          The UNION command
  502.          table size increased from 20K to amount of memory.
  503.          The non-standard 'into' is changed to 'redirectto' to eliminate 
  504.          the conflict with the ANSI 'into'
  505.          Complete revision of documentation for registered users.
  506.  
  507.         SYNTAX DIAGRAMS
  508.         Many chapters begin with a syntax diagram that concisely
  509.         describes a command and its options.  It takes some practice to
  510.         read them but there are only four basic components:
  511.  
  512.         lowercase_letters - they describe something that you must supply. 
  513.         The rules for correctly writing this part of the command follow
  514.         in the documentation.  It may represent a single word, such as
  515.         the syntax for table_name or a rather complicated series of words
  516.         that is needed for a column_definition.
  517.  
  518.         [  ] - items between square brackets are optional.
  519.  
  520.         ...   -  three dots mean previous item may be repeated.
  521.  
  522.         UPPER CASE LETTERS and all other symbols such as '(', ')', ';',
  523.         '.', and ','  - these must be typed as shown.  Although when
  524.         typing the command, you can use lower case instead of upper case
  525.         letters.  For example, refer to the syntax diagram for creating 
  526.         tables.
  527.         
  528.         Since CREATE TABLE is in uppercase, it must
  529.         always be typed as shown.  The items in lowercase such as
  530.         table_name, column_definition, and uniqueness_constraint must be
  531.         replaced by information you supply.  The rules for forming the
  532.         phrases required by the words in lowercase will be contained in
  533.         the chapter.  The item called column_definition is repeated.  The
  534.         first occurance is outside of the square brackets which means
  535.         that it is required.  The second occurance of column_definition
  536.  
  537.  
  538.  
  539.                                  INTRO-7
  540.  
  541.  
  542.  
  543.  
  544.  
  545.         is within square brackets which means that it is optional.  There
  546.         is a comma after the left square bracket which means that if you
  547.         add another column_definition, there must be a comma before it. 
  548.         There are three dots after the second occurance of
  549.         column_definition which means that column_definitions can be more
  550.         than two column_definitions.  Other symbols, such as '(', ')' and
  551.         ';' must be included at the positions shown in the example.  In
  552.         SQL, spacing and lines make no difference.  You can create a
  553.         table with all the components on a single line or put each word
  554.         on a separate line.
  555.  
  556.  
  557.         SSQL SPECIFICATIONS
  558.  
  559.         Requires only one floppy diskette 
  560.         Program only takes up about 122K
  561.         Minimum Memory 256K
  562.         Maximum number of columns 128
  563.         Maximum length of a row 500 bytes (characters)
  564.         Maximum number of rows 
  565.         It is limited by memory. With a 640K PC, the OUTPUT size of the
  566.         table cannot exceed about 400K.  The tables on the disk can be
  567.         larger, you just could not display all the rows and columns at
  568.         once.  Also, you could join multiple large tables as long as the
  569.         resultant table was not over 400K. This means that a table with
  570.         rows 100 characters wide could contain about 4,000 rows.
  571.         
  572.         Maximum levels of subqueries 14
  573.         Maximum number of tables you can join 14
  574.         
  575.  
  576.         CONTENTS OF DISK
  577.         READ.ME       - Basic information
  578.         SSQL.EXE      - The executable file
  579.         ERROR.DAT     - Contains error messages
  580.         EDIT.FIL      - The text file with the location of your word processor
  581.         BRANCH.DBF, PROD.DBF, EMP.DBF, MANU.DBF, SALES.DBF, CUST.DBF,
  582.         BRANCH.SQD, PROD.SQD, EMP.SQD, MANU.SQD, SALES.SQD, CUST.SQD,
  583.                       - The tables used in the documentation
  584.         SSQL.DOC      - Documentation
  585.         ORDER.FRM     - Order form. type: COPY ORDER.FRM PRN 
  586.                         for a printout
  587.         PRINTDOC.BAT  - The batch file to print the documentation
  588.         CONVERT.EXE   - Converts the older .SQL files to .DBF and .SQD
  589.         RUN           - ALTHOUGH THIS IS NOT ON THE DISK IT WILL BE AFTER
  590.                         THE FIRST TIME YOU RUN SSQL.  IT CONTAINS YOUR
  591.                         LAST COMMAND
  592.  
  593.  
  594.  
  595.                                      INTRO-8
  596.  
  597.  
  598.  
  599.  
  600.  
  601.  
  602.          DBASE COMPATIBILITY
  603.  
  604.          SSQL creates dBase III compatible files and it can read any 
  605.          dBase file with the following exceptions:
  606.                 No memo data type
  607.                 No field length over 132
  608.  
  609.          A significant difference between SQL and dBase is the ability 
  610.          of SQL to store a null (see SELECT-13 for more information).  
  611.          Even dBase IV cannot store a null.  The way I have done it is 
  612.          that character columns are filled with blanks, and numeric 
  613.          columns contain -0 (negative zero).  You will never see a -0 
  614.          when using SSQL, the column value will be blank.  This 
  615.          information might be useful if you manipulate files created by 
  616.          SSQL in dBase.
  617.  
  618.          You will notice that every .DBF file created by SSQL has a 
  619.          corresponding .SQD file.  This stores information specific to 
  620.          file creation - Not Null and Unique (see CREATE-2 for more 
  621.          information).  If you access a dBase file not created through 
  622.          SSQL, the .DBF file is created with Not Null set for every 
  623.          column and no uniqueness constraints. 
  624.  
  625.          If you change the number of fields in a dBase file after it has 
  626.          been accessed by SSQL, you should delete the corresponding .SQD 
  627.          file. Otherwise, the Not Null and Uniqueness constraints will 
  628.          not be correct.  
  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.                                 INTRO-9
  656.  
  657.  
  658.  
  659.          
  660.          
  661.          
  662.          
  663.          THE EXAMPLE DATABASES
  664.          There are two sets of simplified databases.  The manufacturing
  665.          database is primarily used for examples throughout the
  666.          documentation.  The law firm data is primarily used for the
  667.          questions at the end of the chapters.
  668.          
  669.          
  670.          MONOLITH MANUFACTURING
  671.          Monolith Manufacturing manufactures and markets high tech
  672.          products in the western states.  There are manufacturing
  673.          facilities in various states.  Products are manufactured in
  674.          batches at each facility.  When the batch is finished an entry is
  675.          made in the table with the date the batch was finished, the
  676.          product code, the state where the product was manufactured, the
  677.          quantity of saleable items and the percent of defects in the
  678.          batch if that is tracked.  Related to the manufacturing table is
  679.          the product table which simply has the description that
  680.          corresponds to each product code.
  681.          The sales portion of the business has as its core the sales
  682.          table.  When a sale is made, an entry is made which contains the
  683.          date of sale, branch code, customer code, salesperson number,
  684.          product code and quantity purchased.  Related to the sales table
  685.          are the branch table, the employee table and the customer table. 
  686.          The branch table contains the branch code, the state the branch
  687.          is in and the employee number of the manager.  The employee table
  688.          contains the employee number, the name of the employee, and the
  689.          employee's manager number if the employee has a manager (the
  690.          president does not have a manager).  All manager numbers have an
  691.          entry in the employee number column since all managers are also
  692.          employees.  The customer table contains the customer code, the
  693.          customer name and the state where the customer resides.
  694.          The following contains details on the Monolith's six tables.  The
  695.          data types correspond to the types used in the create table
  696.          command.  When a column in one table corresponds to a column
  697.          (especially a primary key) in another table, it is in the form of
  698.          TABLE.column. This information will be useful when we need to
  699.          retrieve information that spans tables.  In chapter 22, we will
  700.          refer to these columns as having common domains.
  701.          
  702.          SALES TABLE
  703.          column  data                               Common columns in 
  704.          name    type       description             other tables
  705.          date    date       Date of sale 
  706.          bc      char(2)    Branch code             BRANCH.code
  707.          cc      char(2)    Customer code           CUST.code
  708.          sn      numeric(2) Salesperson number      EMP.code,
  709.                                                  EMP.mgrnum, BRANCH.mgrnum
  710.          pc      char(2)    product code            PROD.code, MANU.code
  711.          qty     numeric(3) quantity
  712.  
  713.          Key is date, bc, cc, sn, pc
  714.  
  715.  
  716.  
  717.                                DATABASE-1
  718.  
  719.  
  720.  
  721.  
  722.          BRANCH TABLE
  723.          column   data                              Common columns in 
  724.          name     type       description            other tables
  725.          code     char(2)    branch code            SALES.bc
  726.          st       char(2)    state code             CUST.st, MANU.mst
  727.          mgrnum   numeric(2) manager number         EMP.enum,
  728.                                                     EMP.mgrnum, SALES.sn
  729.          KEY is code
  730.          
  731.          EMP TABLE
  732.          column   data                              Common columns in 
  733.          name     type       description            other tables
  734.          enum     numeric(2) employee number        EMP.mgrnum, BRANCH.mgrnum, 
  735.                                                     SALES.sn 
  736.          name     char(20)   employee name
  737.          mgrnum   numeric(2) manager number         EMP.enum,
  738.                                                     BRANCH.mgrnum, SALES.sn
  739.          KEY is enum
  740.  
  741.          PROD TABLE
  742.          column   data                              Common columns in 
  743.          name     type       description            other tables
  744.          code     char(2)    product code           MANU.code, SALES.pc
  745.          descrip  char(15)   product description
  746.          KEY is code        
  747.  
  748.          MANU TABLE
  749.          column   data                               Common columns in 
  750.          name     type       description             other tables
  751.          date     date       date of manufacture
  752.          code     char(2)    product code            PROD.code
  753.          mst      char(2)    state where product     BRANCH.st, PROD.st
  754.                              is manufactured
  755.          defects  numeric(3) percent defects in batch  
  756.          qty      numeric(3) quantity manufactured
  757.          KEY is date, code, ms
  758.  
  759.          CUST TABLE
  760.          column   data                               Common columns in 
  761.          name     type       description             other tables
  762.          code     char(2)    Unique customer code    SALES.cc
  763.          name     char(15)
  764.          st       char(2)    State                   MANU.mst, PROD.st
  765.          rating   numeric(2)  
  766.          KEY is code
  767.          
  768.          
  769.          COLE'S LAW FIRM
  770.          Cole's Law offers service in a variety of specialties such as
  771.          Real Estate, Computers, Crime, and Patents.  All attorneys work
  772.          in only one of the specialties.  Each attorney is assigned a
  773.          category as follows:
  774.          1 - Partner
  775.          2 - Associate
  776.          3 - Clerk
  777.  
  778.  
  779.                                DATABASE-2
  780.  
  781.  
  782.  
  783.  
  784.  
  785.          All associates and clerks have supervising attorneys.Clients are
  786.          also put in categories but they are defined very differently. It
  787.          is related to yearly billings:
  788.          1 - over $1,000,000 yr
  789.          2 - $500,000 - $999,999
  790.          3 - $100,000 - $499,999
  791.          4 - less than $100,000
  792.          Since attorneys get bonus points for bringing clients to the
  793.          firm, we also track the originating attorney.
  794.          Cases are based on a unique case number.  A client could be
  795.          associated with many cases.  There is a primary attorney in each
  796.          case.  The main topic of the case will always relate to the
  797.          specialty of the primary attorney.  Not all attorneys on the case
  798.          will have the same specialty however. 
  799.          Work on the cases is organized by date, case number and attorney
  800.          number.  The hours worked are for a single day.
  801.          
  802.          SPCLTY TABLE
  803.          column   data                               Common columns in 
  804.          name     type       description             other tables
  805.          code     char(2)    unique specialty        ATTORNEY.spec
  806.          descrip  char(15)   specialty
  807.          KEY is code
  808.  
  809.          ATTORNEY TABLE
  810.          column   data                               Common columns in 
  811.          name     type       description             other tables
  812.          num      char(2)    unique number
  813.          name     char(15)
  814.          city     char(10)
  815.          st       char(2)
  816.          supatt   char(2)    Supervising attorney     ATTORNEY.num, 
  817.                                                       CLIENT.origatt, 
  818.                                                       CASE.primatt, 
  819.                                                       CASEWORK.attnum
  820.          spec     char(2)    Specialty of att.        SPCLTY.code
  821.          cat      numeric(1) Category
  822.          yrs      numeric(2) Years of service
  823.          salary   numeric(8)
  824.          KEY is num
  825.  
  826.          CLIENT TABLE
  827.          column   data                               Common columns in 
  828.          name     type       description             other tables
  829.          num      char(2)                            CASE.clnum
  830.          name     char(20)
  831.          city     char(10)
  832.          st       char(2)    State
  833.          cat      numeric(1) Category
  834.          origatt  char(2)    Originating attorney    ATTORNEY.num, 
  835.                                                      ATTORNEY.supatt,
  836.                                                      CASE.primatt, 
  837.                                                      CASEWORK.attnum
  838.          KEY is num
  839.          
  840.  
  841.  
  842.                                DATABASE-3
  843.  
  844.  
  845.  
  846.  
  847.          CASE TABLE
  848.          column   data                               Common columns in 
  849.          name     type       description             other tables
  850.          num      char(2)    Case number
  851.          city     char(10)
  852.          st       char(2)    State
  853.          clnum    char(2)    Client number           CLIENT.num
  854.          primatt  char(2)    Primary attorney num.   ATTORNEY.num, 
  855.                                                      ATTORNEY.supatt, 
  856.                                                      CLIENT.origatt, 
  857.                                                      CASEWORK.attnum
  858.          KEY is num
  859.  
  860.          CASEWORK TABLE
  861.          column   data                               Common columns in 
  862.          name     type       description             other tables
  863.          date     date       Date worked
  864.          casenum  char(2)    Case number             CASE.num
  865.          attnum   char(2)    Attorney number         ATTORNEY.num,
  866.                                                      CLIENT.origatt,       
  867.                                                      CASE.primatt, 
  868.                                                      ATTORNEY.supatt
  869.          hours  numeric(5,2) Hours worked on date
  870.          KEY is date, casenum, attnum
  871.         
  872.          
  873.          
  874.  
  875.  
  876.  
  877.  
  878.  
  879.  
  880.  
  881.  
  882.  
  883.  
  884.  
  885.  
  886.  
  887.  
  888.  
  889.  
  890.  
  891.  
  892.  
  893.  
  894.  
  895.  
  896.  
  897.  
  898.  
  899.  
  900.  
  901.  
  902.  
  903.                                DATABASE-4
  904.  
  905.          
  906.          
  907.          
  908.          
  909.          
  910.          
  911.          USING YOUR WORD PROCESSOR
  912.  
  913.          You can modify any SSQL command by calling your text editor or
  914.          word processor from within SSQL.  You must first specify which
  915.          text editor or word processor you are using in a file called
  916.          EDIT.FIL.  The file must contain the path and name of the text
  917.          editor or word processor.  YOU CANNOT CALL A BATCH FILE.  The
  918.          distribution disk contains an EDIT.FIL with the following:
  919.          C:\PCWRITE\ED
  920.          The command to start PC-Write is ED and it resides on drive C: in
  921.          the PCWRITE directory.  You can replace the above line with the
  922.          path and program name of your own. The EDIT.FIL must reside on
  923.          your default directory. The syntax is:
  924.          EDIT [file_name];
  925.          The file_name is any valid name which will be accepted by your
  926.          word processor on the command line.  For example, if you want to
  927.          edit MGRORG, you would type:
  928.          edit mgrorg;
  929.          As is true with all commands except calling a script file, you
  930.          must end this one with a ';'.  When you exit your word
  931.          processor/text editor, you will return to SSQL.  Refer to the
  932.          next section to find out how to process the file you just
  933.          created.
  934.          There is a special case when you want to edit the last SSQL
  935.          command that you typed.  Every command you type except edit and
  936.          exit is stored in a file called RUN.  To edit the last command
  937.          entered type:
  938.          edit run;
  939.          If your word processor does not allow you to type the file name
  940.          that you want to edit after the word processor command in
  941.          EDIT.FIL simply type:
  942.          edit;
  943.  
  944.  
  945.  
  946.  
  947.  
  948.  
  949.  
  950.  
  951.  
  952.  
  953.  
  954.  
  955.  
  956.  
  957.  
  958.  
  959.  
  960.  
  961.  
  962.                              WORD-1
  963.  
  964.  
  965.          
  966.          
  967.          
  968.          
  969.          
  970.          
  971.          CALL SCRIPT FILE 
  972.  
  973.          A script file is a text file which contains an SSQL command.  The
  974.          script file can then be called from SSQL and the command it
  975.          contains will be processed.
  976.          
  977.          SYNTAX:
  978.          @file_name 
  979.          
  980.          The file_name can be any valid DOS file name.
  981.          The file must be text only, no special word processing formats
  982.          allowed. PC-Write or Qedit are excellent choices because they
  983.          always produce standard text. If you use WordPerfect, save the
  984.          file using Text Out (Ctrl-F5). If you use Wordstar, I feel sorry
  985.          for you but anyway, use the non-document format. 
  986.          
  987.          COMMENTS IN THE SCRIPT FILE
  988.          You can precede a comment by two dashes and from that point to
  989.          the end of the line will not be processed by SSQL. The comment
  990.          can be at any place in the line, not necessarily at the beginning
  991.          as the following example shows.
  992.          You can create a file called MGRORG which contains:
  993.          --This query finds the names of managers who have
  994.          -- salespeople who have sold products to Organomice
  995.          select name      --Get names
  996.          from emp      
  997.          where enum in 
  998.            (select mgrn   --of managers
  999.             from emp 
  1000.             where enum in   
  1001.               (select sn        -- who have salespeople
  1002.                from sales,cust  -- who have sold products to
  1003.                where cc = code  -- Organomice
  1004.                and name = 'Organomice')); 
  1005.          
  1006.          From SSQL, you would simply type: 
  1007.          
  1008.          @MGRORG 
  1009.  
  1010.          The file you created would be displayed on the screen and then 
  1011.          the result would appear. 
  1012.          The script file can contain multiple commands.
  1013.          
  1014.  
  1015.  
  1016.  
  1017.  
  1018.  
  1019.  
  1020.  
  1021.  
  1022.  
  1023.  
  1024.  
  1025.                             SCRIPT-1
  1026.  
  1027.  
  1028.  
  1029.  
  1030.          QUERIES - SINGLE TABLE
  1031.  
  1032.          OVERVIEW
  1033.  
  1034.          The select command is used to retrieve data from tables. The
  1035.          select command is covered in three later chapters too.  Although
  1036.          the use of select on a single table is relatively easy,
  1037.          translating from English to SQL (or most other query languages)
  1038.          can be rather tricky.  The approach of this chapter is not merely
  1039.          to explain the syntax of the command, it should give you a more
  1040.          critical approach to applying SQL.
  1041.  
  1042.          SYNTAX: 
  1043.  
  1044.          SELECT [DISTINCT] column_name [,column_name ...] 
  1045.          FROM table_name [,table_name ...] 
  1046.          [WHERE search_expression] 
  1047.          [REDIRECTTO file_name]         <--Non-Standard 
  1048.          [GROUP BY column_name [,column_name ...] 
  1049.          [HAVING criteria]]
  1050.          [ORDER BY column_name [,column_name ...] 
  1051.  
  1052.          If you want all the columns in a table you can replace the list
  1053.          of column names with an asterisk (*). 
  1054.          The select command produces a report in the form of a table.
  1055.          Although the basic use of the select takes a short time to learn,
  1056.          the more advanced uses take a long time to master. 
  1057.          The tables used in this chapter are on the distribution disk so
  1058.          you can try entering the commands yourself.
  1059.          All the examples have each clause starting on a new line for
  1060.          purposes of clarity. It is not necessary to have them on separate
  1061.          lines. It is sometimes advantageous to keep the query on one line
  1062.          because if you need to make a correction, you cannot backup a
  1063.          line. However, to make correcting command much easier, you can
  1064.          call your word processor from within SSQL. This is described in
  1065.          the full documentation.
  1066.  
  1067.          THE BASICS OF SELECT 
  1068.  
  1069.          Find all the data in the manu table.
  1070.          
  1071.          select *  
  1072.          from manu; 
  1073.          date     code mst defects qty  
  1074.          -------- ---- --- ------- ---  
  1075.          02/07/87 GC   ID       12  15 
  1076.          02/01/87 GC   ID        0  55  
  1077.          02/02/87 NM   CA       17  93  
  1078.          02/02/87 DD   ID           25  
  1079.          02/03/87 DD   WA       22  46  
  1080.          02/02/87 NM   WA       15  25  
  1081.          02/04/87 DD   AZ       12  25  
  1082.          02/04/87 DD   CA       15  25  
  1083.          02/06/87 GC   AZ        4  43  
  1084.           
  1085.          9 rows selected  
  1086.  
  1087.  
  1088.                                 SELECT-1
  1089.  
  1090.  
  1091.  
  1092.  
  1093.  
  1094.          
  1095.          Instead of typing all the column names, you can use the * to
  1096.          denote all column names. On the next line, the "from manu" tells
  1097.          us that we want the data to come from the table named "manu". All
  1098.          select commands must end with a semi-colon (;). If we just wanted
  1099.          the product code and the state that it was manufactured in you
  1100.          would type:
  1101.          
  1102.           select code, mst 
  1103.           from manu; 
  1104.           code mst  
  1105.           ---- ---  
  1106.           GC   ID   
  1107.           GC   ID   
  1108.           NM   CA   
  1109.           DD   ID   
  1110.           DD   WA   
  1111.           NM   WA   
  1112.           DD   AZ   
  1113.           DD   CA   
  1114.           GC   AZ   
  1115.            
  1116.           9 rows selected 
  1117.          
  1118.          DISTINCT 
  1119.          The optional statement "distinct" will eliminate all rows with
  1120.          duplicate columns that you have specified. If you wanted to find
  1121.          out all the different states where products are manufactured, you
  1122.          would type:
  1123.          
  1124.          select distinct mst 
  1125.          from manu; 
  1126.           
  1127.          mst  
  1128.          ---  
  1129.          ID   
  1130.          CA   
  1131.          WA   
  1132.          AZ   
  1133.           
  1134.          4 rows selected 
  1135.          
  1136.          As you can see only 4 rows were found. This is because  there can
  1137.          be more than one row with the same state. Only the first
  1138.          occurrence of each data element is retained. 
  1139.          The "distinct" modifier can be used with one or more column names
  1140.          in a select statement. However, the "distinct" modifier operates
  1141.          on the whole row, not just a single column. In the manu table,
  1142.          the primary key is date, code, mst. What if we wanted not only
  1143.          the state where the products are manufactured but the product
  1144.          codes too. Notice what happens with the following:
  1145.  
  1146.  
  1147.  
  1148.  
  1149.  
  1150.                                 SELECT-2
  1151.  
  1152.  
  1153.  
  1154.  
  1155.  
  1156.          
  1157.          select mst, code 
  1158.          from manu; 
  1159.           
  1160.          mst code  
  1161.          --- ----  
  1162.          ID  GC    
  1163.          ID  GC    
  1164.          CA  NM    
  1165.          ID  DD    
  1166.          WA  DD    
  1167.          WA  NM    
  1168.          AZ  DD    
  1169.          CA  DD    
  1170.          AZ  GC    
  1171.           
  1172.          9 rows selected 
  1173.          
  1174.          Some of the rows are duplicated because it is possible to
  1175.          manufacture the same product in the same state but on a different
  1176.          date. To get rid of the duplication of rows we can add the
  1177.          "distinct" modifier.
  1178.          
  1179.          WHERE 
  1180.          
  1181.          The where clause of the select command is the most powerful and
  1182.          complex part of SQL. Our discussion will be broken down into
  1183.          various phases. The first phase is using the where with a single
  1184.          table name. Later you will see it used with multiple tables and
  1185.          you will find that you can even put another select command within
  1186.          the where clause. 
  1187.          
  1188.          search_expression 
  1189.          
  1190.          The simplest search expression relates a column name to a
  1191.          constant. A constant is an exact value that you enter. All column
  1192.          values are tested against the constant value. If the column name
  1193.          that you are testing is defined as a character, the constant must
  1194.          be enclosed in single quotes or double quotes. The constant you
  1195.          are searching for may not contain a quote. If the column name you
  1196.          are testing for is defined as numeric, the constant is a number
  1197.          without any quotes. 
  1198.          You can relate the column name to the constant in the following
  1199.          ways: 
  1200.          
  1201.          = equal 
  1202.          <> not equal 
  1203.          != not equal 
  1204.          ~= not equal
  1205.          >  greater than 
  1206.            less than 
  1207.          >= greater than or equal 
  1208.          <=  less than or equal 
  1209.          IS NULL   column value is null
  1210.          IS NOT NULL   column value is not null
  1211.          LIKE  like a pattern
  1212.          NOT LIKE  not like a pattern
  1213.  
  1214.  
  1215.                                 SELECT-3
  1216.  
  1217.  
  1218.  
  1219.  
  1220.  
  1221.          
  1222.          Find out which customers are in Arizona. 
  1223.          
  1224.          select * 
  1225.          from cust 
  1226.          where st = 'AZ'; 
  1227.           
  1228.          code name            st rating  
  1229.          ---- --------------- -- ------  
  1230.          ZZ   Organomice      AZ     34  
  1231.          DD   QuarkCo         AZ     10  
  1232.           
  1233.          2 rows selected 
  1234.          
  1235.          Spaces are optional on either side of the "=". You could have
  1236.          typed: 
  1237.          
  1238.          where st='AZ'; 
  1239.          
  1240.          Instead of single quotes you could have used double quotes: 
  1241.          
  1242.          where st="AZ"; 
  1243.          
  1244.          In the manu table, the column name "defects" is the percent of
  1245.          defects when the product was manufactured. The statement below
  1246.          shows you how to get a list of all the information for those
  1247.          products with defects over 10%.
  1248.          
  1249.          select * 
  1250.          from manu 
  1251.          where defects > 10; 
  1252.           
  1253.          date     code mst defects qty  
  1254.          -------- ---- --- ------- ---  
  1255.          02/07/87 GC   ID       12  15  
  1256.          02/02/87 NM   CA       17  93  
  1257.          02/03/87 DD   WA       22  46  
  1258.          02/02/87 NM   WA       15  25  
  1259.          02/04/87 DD   AZ       12  25  
  1260.          02/04/87 DD   CA       15  25  
  1261.           
  1262.          6 rows selected 
  1263.          
  1264.          The symbols such as <, >, =, <=, >=, and != can be used with
  1265.          character data too. For example, the statement below shows how to
  1266.          retrieve all customer names alphabetically greater than Machado.
  1267.  
  1268.  
  1269.  
  1270.  
  1271.  
  1272.  
  1273.  
  1274.  
  1275.                                 SELECT-4
  1276.  
  1277.  
  1278.  
  1279.  
  1280.  
  1281.          
  1282.          select name 
  1283.          from cust 
  1284.          where name > "Machado"; 
  1285.           
  1286.          name             
  1287.          ---------------  
  1288.          Technoharps      
  1289.          Organomice       
  1290.          QuarkCo          
  1291.          Marswarp         
  1292.          Multicrud        
  1293.           
  1294.          5 rows selected 
  1295.          
  1296.          SPECIAL SEARCH EXPRESSION - IS NULL, IS NOT NULL 
  1297.          
  1298.          Nulls are an important concept in relational databases. For
  1299.          example, let's look at the percentage of defects in Idaho:
  1300.          
  1301.          select *
  1302.          from manu
  1303.          where mst = 'ID'; 
  1304.            
  1305.          date     code mst defects qty  
  1306.          ------   ---- --- ------- ---  
  1307.          02/07/87 GC   ID       12  15  
  1308.          02/01/87 GC   ID        0  55  
  1309.          02/02/87 DD   ID           25  
  1310.           
  1311.          3 rows selected 
  1312.           
  1313.          In Idaho, the batch of MW on 02/07/87 has 12% defects, GC on 02/01/87
  1314.          has 0% defects but there is nothing listed for DD. This "nothing"
  1315.          is called a null. It does not mean zero or blank. A null means
  1316.          that the data element is unknown. In our case, product DD in
  1317.          Idaho is not tracked for defects like other products. This isn't
  1318.          to say that there are zero defects. It just means we have no
  1319.          entry. 
  1320.          If we wanted to look at all products for which we do not track
  1321.          defects, we would use the following:
  1322.          
  1323.          select * 
  1324.          from manu 
  1325.          where defects is null; 
  1326.          date     code mst defects qty  
  1327.          -------- ---- --- ------- ---  
  1328.          02/02/87 DD   ID           25  
  1329.           
  1330.          1 row selected 
  1331.          
  1332.  
  1333.  
  1334.  
  1335.                                 SELECT-5
  1336.  
  1337.  
  1338.  
  1339.  
  1340.  
  1341.          
  1342.          We could find all the products for which we track defects by
  1343.          using is not null:
  1344.          
  1345.          select * 
  1346.          from manu 
  1347.          where defects is not null; 
  1348.          date     code mst defects qty  
  1349.          -------- ---- --- ------- ---  
  1350.          02/07/87 GC   ID       12  15  
  1351.          02/01/87 GC   ID        0  55  
  1352.          02/02/87 NM   CA       17  93  
  1353.          02/03/87 DD   WA       22  46  
  1354.          02/02/87 NM   WA       15  25  
  1355.          02/04/87 DD   AZ       12  25  
  1356.          02/04/87 DD   CA       15  25  
  1357.          02/06/87 GC   AZ        4  43  
  1358.           
  1359.          8 rows selected 
  1360.          
  1361.          Null values can be used with both numeric columns and character
  1362.          columns.
  1363.          
  1364.          SPECIAL SEARCH EXPRESSION - LIKE, NOT LIKE 
  1365.          
  1366.          An expression with like is used when we want to retrieve data
  1367.          that is similar in some way to another set of characters. The
  1368.          like expression can only be used with character data. This is
  1369.          explained in the full documentation.
  1370.          
  1371.          AND, OR, NOT 
  1372.          
  1373.          We can link expressions with ands and ors. If you use an and,
  1374.          both expressions have to be true in order to add the row to the
  1375.          table we are producing. If you use an or, only one of the
  1376.          expressions has to be true. You can use as many ands and ors as
  1377.          you want in a query. The expressions are evaluated from left to
  1378.          right. All the ands are done first, then evaluation starts again
  1379.          from the left and the ors are processed. 
  1380.          If you want to reference different column names in a search, the
  1381.          logic as stated in English is typically the same as it is in SQL.
  1382.          Usually there is no confusion between the and and the or unless
  1383.          you are dealing with the same column name. 
  1384.          Find all products in Washington with defects over 16%:
  1385.          
  1386.          select distinct code 
  1387.          from manu 
  1388.          where mst = 'WA' and defects > 16; 
  1389.           
  1390.          code  
  1391.          ----  
  1392.          DD    
  1393.           
  1394.          1 row selected 
  1395.  
  1396.  
  1397.                                 SELECT-6
  1398.  
  1399.  
  1400.  
  1401.  
  1402.  
  1403.          
  1404.          Generally, the use of a single and or an or in a where clause is
  1405.          more or less common sense unless you are referring to the same
  1406.          column name. What if you wanted to find all the products
  1407.          manufactured in Washington and Idaho? This has two meanings in
  1408.          everyday English: 
  1409.          
  1410.          1. Out of all the products we manufacture, I want a list of the
  1411.          ones we manufacture in Washington and a list of the ones we
  1412.          manufacture in Idaho. 
  1413.          
  1414.          2. Out of all the products we manufacture, I want a list of each
  1415.          one we manufacture in BOTH Washington and Idaho. 
  1416.          
  1417.          We will accept the first meaning in this example. It is
  1418.          interesting to note that we could have also said, "List all the
  1419.          products manufactured in Washington or Idaho." It seems odd that
  1420.          in English "or" can have the same meaning as "and". Even if you
  1421.          don't agree with my interpretation, beware that others do and
  1422.          that it may cause communication problems. 
  1423.          In the English version you use the "and" so there is a tendency
  1424.          to always use the "and" in the SQL version too. This would be
  1425.          wrong. The English perspective and the SQL perspective are
  1426.          different. The English perspective often looks at the whole group
  1427.          of items. The SQL perspective looks at one item at a time. To
  1428.          translate Washington and Idaho into SQL we look at it from its
  1429.          perspective. We will look at each item. If that item is
  1430.          manufactured in Washington or Idaho, we will add it to our table.
  1431.          Notice that when we look at it from the SQL perspective, we use
  1432.          the "or" instead of the "and". This not just a quirk of SQL
  1433.          logic, it is common to virtually all data bases. The statement
  1434.          below shows how to find out all the products manufactured in
  1435.          Washington and Idaho: 
  1436.          
  1437.          select distinct code 
  1438.          from manu 
  1439.          where mst='WA' or mst='ID'; 
  1440.           
  1441.          code  
  1442.          ----  
  1443.          GC    
  1444.          DD    
  1445.          NM    
  1446.           
  1447.          3 rows selected 
  1448.          
  1449.          It is interesting to note what the meaning would be if we used an
  1450.          and instead of an or. Would it tell us every product manufactured
  1451.          in BOTH Washington and Idaho? No, it wouldn't as you can see in
  1452.          the following:
  1453.          
  1454.          select distinct code 
  1455.          from manu 
  1456.          where mst='WA' and mst='ID'; 
  1457.           
  1458.          no rows found 
  1459.  
  1460.  
  1461.          
  1462.                                 SELECT-7
  1463.  
  1464.  
  1465.  
  1466.  
  1467.  
  1468.          
  1469.          Actually, it makes no sense. How could a state for an individual
  1470.          item be both WA and ID????? 
  1471.          
  1472.          But how WOULD you find out every product manufactured in both
  1473.          Washington and Idaho? The search strategy is rather complex and
  1474.          the topic is covered under joining tables and again when we
  1475.          discuss subqueries. 
  1476.          
  1477.          In order to express the opposite logic, you use the not but you
  1478.          have to be careful. The statement below shows how to find the
  1479.          products manufactured outside of Washington or Idaho. 
  1480.          
  1481.          select * 
  1482.          from manu 
  1483.          where not (mst='WA' or mst='ID'); 
  1484.          
  1485.          date     code mst defects qty  
  1486.          -------- ---- --- ------- ---  
  1487.          02/02/87 NM   CA       17  93  
  1488.          02/04/87 DD   AZ       12  25  
  1489.          02/04/87 DD   CA       15  25  
  1490.          02/06/87 GC   AZ        4  43  
  1491.           
  1492.          4 rows selected 
  1493.          
  1494.          Again, we must be careful about how we express the query in
  1495.          English. The query above cannot be translated as: Find the
  1496.          products NOT manufactured in Washington or Idaho. The query above
  1497.          retrieved some products that happen to be manufactured in Idaho
  1498.          or Washington. We really did not care. The only constraint we
  1499.          cared about was the state in which it was manufactured. The query
  1500.          could accurately be translated in fractured English: Find the
  1501.          products manufactured not in Washington or Idaho. By changing the
  1502.          statement to one that requests products not manufactured in
  1503.          Washington or Idaho, we change the words modified by "not" from
  1504.          the states to products manufactured. To find the products not
  1505.          manufactured in Washington or Idaho we must first find the set of
  1506.          all products manufactured in Washington or Idaho:
  1507.          
  1508.          select distinct code 
  1509.          from manu 
  1510.          where mst = 'WA' or mst = 'ID'; 
  1511.          
  1512.          code  
  1513.          ----  
  1514.          GC    
  1515.          DD    
  1516.          NM    
  1517.           
  1518.          3 rows selected 
  1519.  
  1520.  
  1521.  
  1522.  
  1523.  
  1524.          
  1525.                                 SELECT-8
  1526.  
  1527.  
  1528.  
  1529.  
  1530.  
  1531.          
  1532.          Then we must find all the products in the other states that are
  1533.          not in the above set of three products. We end up with the
  1534.          following which finds that there are not any products
  1535.          manufactured in the other states that are not manufactured in
  1536.          Washington and Idaho.
  1537.          
  1538.          select distinct code 
  1539.          from manu 
  1540.          where code != 'NM' and code != 'GC' and code != 'DD'; 
  1541.           
  1542.          no rows found
  1543.          
  1544.          Well, you may ask, what IS the select statement to find the
  1545.          products not manufactured in Washington or Idaho? Not so fast.
  1546.          Actually, it would require a subquery that is covered in the
  1547.          section on subqueries. 
  1548.          
  1549.          If I may digress...
  1550.          
  1551.          I know. Just a few pages into the basics of the select statement
  1552.          and you are ready to give up. Remember, the logic of SQL is not
  1553.          all that difficult. The main problem is that you do not think
  1554.          critically about the meaning of sentences. Much has been written
  1555.          about the problems of communication between humans and computers.
  1556.          Most of the problems are actually caused by the end-user and the
  1557.          "computer expert". I put the burden on the "computer expert" who
  1558.          should know enough about English to note the subtlties and
  1559.          ambiguities of what the end-user is requesting. This is just as
  1560.          relevant for interpreting queries as it is building complete
  1561.          application systems.
  1562.          
  1563.          Meanwhile, back at the ranch...
  1564.          
  1565.          Normally, if a condition is true, we add that row to the table.
  1566.          By using the not, if the condition is false, we add the row to
  1567.          the table. On the first row of the select statement to find
  1568.          products manufactured outside Idaho and Washington, the state is
  1569.          CA. It was selected because it is not equal to WA or ID. Just as
  1570.          we can express the same thought in English more than one way, we
  1571.          can do the same with SQL. We could have written:
  1572.          
  1573.          select * 
  1574.          from manu 
  1575.          where mst != 'WA' and mst != 'ID'; 
  1576.          OR 
  1577.          select * 
  1578.          from manu 
  1579.          where not (mst = 'WA') and not (mst = 'ID'); 
  1580.  
  1581.  
  1582.  
  1583.  
  1584.  
  1585.          
  1586.                                 SELECT-9
  1587.  
  1588.  
  1589.  
  1590.  
  1591.  
  1592.          
  1593.          What if we would have written or instead of and in the two
  1594.          examples above? Every row would have been selected. No matter
  1595.          what the state is, it would either be not equal to WA or not
  1596.          equal to ID! 
  1597.          
  1598.          Remember that whenever you use the not, you must enclose the
  1599.          expression in parentheses. 
  1600.          
  1601.          You can have as many ands and ors in a query as you need. What if
  1602.          you wanted all products in Washington and Idaho with defects less
  1603.          than 16%?  
  1604.          
  1605.          select * 
  1606.          from manu 
  1607.          where (mst='WA' or mst='ID') and defects < 16; 
  1608.           
  1609.          date     code mst defects qty  
  1610.          -------- ---- --- ------- ---  
  1611.          02/07/87 GC   ID       12  15  
  1612.          02/01/87 GC   ID        0  55  
  1613.          02/02/87 NM   WA       15  25  
  1614.           
  1615.          3 rows selected 
  1616.          
  1617.          We find that only two products are selected. Notice that
  1618.          parentheses enclose the or expression. This is because without
  1619.          the parentheses the and expression would have been executed
  1620.          first. Remember that without parentheses the ands are processed
  1621.          first and the ors are processed next. Not inserting the
  1622.          parentheses would have changed the meaning considerably:
  1623.          
  1624.          select * 
  1625.          from manu 
  1626.          where mst='WA' or mst='ID' and defects < 16; 
  1627.           
  1628.          date     code mst defects qty  
  1629.          -------- ---- --- ------- ---  
  1630.          02/07/87 GC   ID       12  15  
  1631.          02/01/87 GC   ID        0  55  
  1632.          02/03/87 DD   WA       22  46  
  1633.          02/02/87 NM   WA       15  25  
  1634.           
  1635.          4 rows selected 
  1636.          
  1637.          The above query is the equivalent of saying, "List all products
  1638.          in Washington and list all products in Idaho with defects under
  1639.          16%."  Another way of stating it is "In looking through all the
  1640.          products, if a product is manufactured in Washington or an Idaho
  1641.          product has under 16% defects, add it to the list." The important
  1642.          concept is that the 16% defects relates to the products
  1643.          manufactured in Idaho, not all products. 
  1644.          
  1645.  
  1646.  
  1647.                                 SELECT-10
  1648.  
  1649.  
  1650.  
  1651.  
  1652.  
  1653.          
  1654.          SETS (used with constants) 
  1655.          
  1656.          Instead of relating items one at a time, we can relate groups of
  1657.          items at once. These groups are called sets. They are enclosed
  1658.          parentheses and the constants are separated by commas. 
  1659.          
  1660.          You saw above how you can use =, !=, <, >, <=, and >=. We can
  1661.          expand that concept to include any and all.  This chapter has a
  1662.          brief introduction to sets.  Sets are the main topic of the
  1663.          chapters on subqueries where instead of using constants in sets,
  1664.          we use select statements.
  1665.          
  1666.          ANY 
  1667.          
  1668.          As you will see, the use of the any with constants is simply a
  1669.          short-hand method of writing a series of comparisons with ors.
  1670.          The any may be combined with any of the comparison operators:
  1671.          =any  equal to any 
  1672.          >any  greater than any 
  1673.          <any  less than any 
  1674.          !=any  not equal to any 
  1675.          >=any  greater than or equal to any 
  1676.          <=any  less than or equal to any 
  1677.          
  1678.          Although there are many possible combinations with any, very few
  1679.          are of any use. With constants, the only one that is typically
  1680.          used is =any. 
  1681.          
  1682.          Let's rewrite the query concerning all products manufactured in
  1683.          Washington or Idaho:
  1684.          
  1685.          select distinct code 
  1686.          from manu 
  1687.          where mst =any ('WA','ID'); 
  1688.          code  
  1689.          ----  
  1690.          GC    
  1691.          DD    
  1692.          NM    
  1693.           
  1694.          3 rows selected 
  1695.          
  1696.          Translated into English it would be: Select the description from
  1697.          the manufacturing table where the state of manufacture is equal
  1698.          to any of the following - WA or ID. 
  1699.          We can use numbers too. What if you wanted to know the basic
  1700.          information on products that have a percentage of defects of 12%
  1701.          or 15%?
  1702.  
  1703.  
  1704.  
  1705.  
  1706.          
  1707.                                 SELECT-11
  1708.  
  1709.  
  1710.  
  1711.  
  1712.  
  1713.          
  1714.          select date, code, mst 
  1715.          from manu 
  1716.          where defects =any (12,15); 
  1717.          date     code mst  
  1718.          -------- ---- ---  
  1719.          02/07/87 GC   ID   
  1720.          02/02/87 NM   WA   
  1721.          02/04/87 DD   AZ   
  1722.          02/04/87 DD   CA   
  1723.           
  1724.          4 rows selected 
  1725.          
  1726.          IN 
  1727.          
  1728.          An equivalent to =any is in. The above query could be translated:
  1729.          
  1730.          select date, code, mst
  1731.          from manu  
  1732.          where defects in (12,15);  
  1733.          
  1734.          The above two queries could be translated into a query without a
  1735.          set:
  1736.          
  1737.          select date, code, mst 
  1738.          from manu 
  1739.          where defects = 12 or defects = 15; 
  1740.          
  1741.          ALL
  1742.          
  1743.          The all can be combined with all the comparison operators just as
  1744.          was seen with the any. The only common use with constants occurs
  1745.          with the !=all which allows us to exclude a set of data. In the
  1746.          following statement we show information on products manufactured
  1747.          outside of Washington and Idaho.
  1748.          
  1749.          select date, code, mst 
  1750.          from manu 
  1751.          where mst !=all('WA','ID'); 
  1752.           
  1753.          date     code mst  
  1754.          -------- ---- ---  
  1755.          02/02/87 NM   CA   
  1756.          02/04/87 DD   AZ   
  1757.          02/04/87 DD   CA   
  1758.          02/06/87 GC   AZ   
  1759.           
  1760.          4 rows selected 
  1761.          
  1762.          Although we translated =any as "equal to any item in the set",
  1763.          !=all is usually translated as "not equal to any item in the
  1764.          set". Another way to state the above query is to find products
  1765.          manufactured in a state that is not any of the following -
  1766.          Washington and Idaho.  Confusing, isn't it??  Just remember that
  1767.  
  1768.  
  1769.  
  1770.  
  1771.                                 SELECT-12
  1772.  
  1773.  
  1774.  
  1775.  
  1776.  
  1777.          
  1778.          !=all is the opposite of =any.  This ambiguity in English is
  1779.          related to a riddle I heard Theo ask on the Cosby show:  "What
  1780.          two coins add up to 30 cents and one of them is not a quarter?" 
  1781.          The answer, of course, is a nickel and a quarter.  The nickel is
  1782.          not a quarter which satisfies the rule.  Theo was applying a
  1783.          !=any to the set when a typical listener would think that !=all
  1784.          was actually meant.
  1785.          
  1786.          BETWEEN constant AND constant 
  1787.          An easy way to select a range of values is use the between/and
  1788.          component of the where clause. The following shows how to find
  1789.          the basic information for products with defects between 12% and
  1790.          16%: 
  1791.          
  1792.          select date, code, mst, defects 
  1793.          from manu 
  1794.          where defects between 12 and 16; 
  1795.           
  1796.          date     code mst defects  
  1797.          -------- ---- --- -------  
  1798.          02/07/87 GC   ID       12  
  1799.          02/02/87 NM   WA       15  
  1800.          02/04/87 DD   AZ       12  
  1801.          02/04/87 DD   CA       15  
  1802.           
  1803.          4 rows selected 
  1804.          
  1805.          Notice that when we use the between, we include the 12 and the
  1806.          16. Unfortunately, the word "between" can have more than one
  1807.          meaning in standard English. If you say "The treasure is located
  1808.          between San Francisco and Walnut Creek", you would assume that
  1809.          the treasure is not IN San Francisco or Walnut Creek. That is,
  1810.          you would not include the end points - San Francisco or Walnut
  1811.          Creek. If you say "This insurance plan is available for those
  1812.          people between the ages of 18 and 65", you would assume that if
  1813.          you are 18 or 65, you are eligible. That is, you would include
  1814.          the end points. In English, when the word between is used with
  1815.          numbers, the end points are included. In other cases, the end
  1816.          points are not included. Silly, isn't it!! Just remember that in
  1817.          SQL, between includes the end points.
  1818.          
  1819.          IMPORTANCE OF THE NULL (UNKNOWN VALUE)
  1820.          Now that you understand the basics of how the where clause works,
  1821.          we need to complicate it a bit with considering the unknown value
  1822.          (the null).  If the where clause evaluates to TRUE, then the row
  1823.          is displayed, otherwise, it is not displayed.  In most non-SQL
  1824.          databases, the where clause only evaluates to TRUE and FALSE.  In
  1825.          SQL we have TRUE, FALSE, and UNKNOWN.  We can use truth tables to
  1826.          show the relationship.
  1827.  
  1828.  
  1829.  
  1830.  
  1831.  
  1832.                                 SELECT-13
  1833.  
  1834.  
  1835.  
  1836.  
  1837.  
  1838.          
  1839.           AND  |TRUE  FALSE  UNK     OR   |TRUE  FALSE  UNK
  1840.           ----------------------     -----------------------
  1841.           TRUE |TRUE  FALSE  UNK     TRUE |TRUE  TRUE   TRUE
  1842.           FALSE|FALSE FALSE  FALSE   FALSE|TRUE  FALSE  UNK   
  1843.           UNK  |UNK   FALSE  UNK     UNK  |TRUE  UNK    UNK
  1844.          
  1845.           NOT
  1846.           ------------
  1847.           TRUE  |FALSE
  1848.           FALSE |TRUE
  1849.           UNK   |UNK
  1850.          
  1851.          This can cause some confusing results until you are used to it.
  1852.          For example, there are nine rows in the manu table.  However, the
  1853.          two queries below result in a total of only eight rows.  
  1854.          
  1855.          select *
  1856.          from manu
  1857.          where defects = 15;
  1858.          
  1859.          date     code mst defects qty 
  1860.          -------- ---- --- ------- --- 
  1861.          02/02/87 NM   WA       15  25 
  1862.          02/04/87 DD   CA       15  25 
  1863.          
  1864.          2 rows selected
  1865.          
  1866.          select *
  1867.          from manu
  1868.          where defects != 15;
  1869.          
  1870.          date     code mst defects qty 
  1871.          -------- ---- --- ------- --- 
  1872.          02/07/87 GC   ID       12  15 
  1873.          02/01/87 GC   ID        0  55 
  1874.          02/02/87 NM   CA       17  93 
  1875.          02/03/87 DD   WA       22  46 
  1876.          02/04/87 DD   AZ       12  25 
  1877.          02/06/87 GC   AZ        4  43 
  1878.          
  1879.          6 rows selected
  1880.          
  1881.          
  1882.          At first glance you would think that defects are either equal to
  1883.          fifteen or not equal to fifteen.  However there is one instance
  1884.          where defects has an unknown value so it is not displayed.
  1885.          Intuitively you may be saying to yourself that since an unknown
  1886.          value is not equal to fifteen, it should be displayed, but that
  1887.          is not the way it works.  If you look at both the AND truth table
  1888.          and OR truth table, there is no difference between the false and
  1889.          the unknown since a result of FALSE or UNKNOWN will prevent a row
  1890.          from being displayed.  However, in the truth table for NOT, there
  1891.          is a difference. The opposite of unknown is still unknown.
  1892.          
  1893.          
  1894.  
  1895.                                 SELECT-14
  1896.  
  1897.  
  1898.  
  1899.  
  1900.  
  1901.          
  1902.          AGGREGATE FUNCTIONS - AVG, MIN, MAX, SUM, COUNT 
  1903.          
  1904.          All the aggregate functions operate on numeric columns except
  1905.          count which will operate on any columns. 
  1906.          
  1907.          SYNTAX: 
  1908.          
  1909.          AVG([DISTINCT] column_name) 
  1910.          MIN(column_name) 
  1911.          MAX(column_name) 
  1912.          SUM([DISTINCT]column_name) 
  1913.          COUNT([DISTINCT]column_name) 
  1914.          
  1915.          SUMMARY 
  1916.          
  1917.          AVG - without DISTINCT: The average of all values in a column
  1918.          AVG - with DISTINCT: The average of non-duplicate values 
  1919.          MIN - The minimum value in the column 
  1920.          MAX - The maximum value in the column 
  1921.          SUM - without DISTINCT: The sum of all values in the column 
  1922.          SUM - with DISTINCT: The sum of non-duplicate values 
  1923.          COUNT - without DISTINCT: the number of column values 
  1924.          COUNT - with DISTINCT: the number of non-duplicate column values 
  1925.          
  1926.          Notice that some of the functions have the distinct option. This
  1927.          will exclude all rows in the table which have duplicate column
  1928.          values. The follwing two examples show the difference. 
  1929.          select count(mst) 
  1930.          from manu; 
  1931.          CNT(mst)  
  1932.          --------  
  1933.                 9  
  1934.           
  1935.          9 rows selected 
  1936.          
  1937.          The above query can be translated as: 
  1938.          How many rows have values in the mst column? There are a total of
  1939.          9. This is not very useful because every row is going to have an
  1940.          entry for mst. The answer will always be the total number of rows
  1941.          in the table since the mst column was created with the "not null"
  1942.          constraint. 
  1943.          The following query is typically more useful. It answers the
  1944.          question: "How many different states manufacture products?"
  1945.          
  1946.          select count(distinct mst) 
  1947.          from manu; 
  1948.          CNT(mst)  
  1949.          --------  
  1950.                 4  
  1951.           
  1952.          9 rows selected 
  1953.          
  1954.  
  1955.  
  1956.  
  1957.                                 SELECT-15
  1958.  
  1959.  
  1960.  
  1961.  
  1962.  
  1963.          
  1964.          Typically, when you are using a column name with count, you will
  1965.          modify it with distinct. When querying a table with a multiple
  1966.          part key, all columns will have the possibility of having
  1967.          duplicate entries. This is the case with the manu table with a
  1968.          key of date, code, mst. With such tables, you will modify the
  1969.          column name with distinct. When you query a table that has a key
  1970.          based on a single column, the only column you can be assured of
  1971.          not having duplicates is the key column. If you want to count all
  1972.          the rows in the table, there is a short-cut:
  1973.          
  1974.          select count(*) 
  1975.          from manu; 
  1976.          
  1977.          CNT(*)  
  1978.          ------  
  1979.               9  
  1980.           
  1981.          9 rows selected 
  1982.          
  1983.          At this point, you may ask what use the column name is without
  1984.          the distinct modifier? It is that wonderful concept of the null
  1985.          that has been touched on before and will be discussed in more
  1986.          detail about the other functions. The count function will not
  1987.          count rows that have a null value in the column you are counting.
  1988.          Actually, all the functions skip rows with nulls. In the employee
  1989.          table, not everyone has a corresponding manager number. The entry
  1990.          for employees without a manager is a null (actually only the
  1991.          president doesn't have a manager in this table). The following
  1992.          tells us how many employees have managers.
  1993.          
  1994.          select count(mgrnum) 
  1995.          from emp; 
  1996.          CNT(mgrnum)  
  1997.          -----------  
  1998.                   14  
  1999.           
  2000.          15 rows selected 
  2001.          
  2002.          This can be contrasted with the following query. Notice how
  2003.          simply adding the word "distinct" completely changes the meaning
  2004.          of the query to finding how many managers there are in the
  2005.          company.
  2006.          
  2007.          select count(distinct mgrnum) 
  2008.          from emp; 
  2009.          
  2010.          CNT(mgrnum)  
  2011.          -----------  
  2012.                    7  
  2013.           
  2014.          15 rows selected 
  2015.          
  2016.  
  2017.  
  2018.  
  2019.                                 SELECT-16
  2020.  
  2021.  
  2022.  
  2023.  
  2024.  
  2025.          
  2026.          The functions also can be used with the where clause. The
  2027.          following shows how many batches of product DD are in the table.
  2028.          
  2029.          select count(*) 
  2030.          from manu 
  2031.          where code = 'DD'; 
  2032.          
  2033.          CNT(*)  
  2034.          ------  
  2035.               4  
  2036.           
  2037.          4 rows selected 
  2038.           
  2039.          Before we discuss the other functions, let's look at the
  2040.          percentage of defects in Idaho:
  2041.          
  2042.          select * 
  2043.          from manu 
  2044.          where mst = 'ID'; 
  2045.          date     code mst defects qty  
  2046.          -------- ---- --- ------- ---  
  2047.          02/07/87 GC   ID       12  15  
  2048.          02/01/87 GC   ID        0  55  
  2049.          02/02/87 DD   ID           25  
  2050.           
  2051.          3 rows selected 
  2052.          
  2053.          In Idaho, the are three entries: 12, 0, and "nothing". This
  2054.          "nothing" is called a null. Nulls are important in relational
  2055.          data bases. It does not mean zero or blank. A null means that the
  2056.          data element is not relevant in the row. In our case, product DD
  2057.          is not tracked for defects like other products. This isn't to say
  2058.          that there are zero defects. It just means we have no entry.
  2059.          Because there are only two valid entries for defects in Idaho,
  2060.          the functions will not take into account the null data element.
  2061.          The average percentage of defects will be 12/2 = 6, not 12/3 = 4:
  2062.          
  2063.          select avg(defects) 
  2064.          from manu 
  2065.          where mst='ID'; 
  2066.          AVG(defects)  
  2067.          ------------  
  2068.                     6  
  2069.           
  2070.          3 rows selected 
  2071.          
  2072.          
  2073.          The sum function totals the numeric values in the column(s)
  2074.          requested. Below we sum the number of product DD that has been
  2075.          manufactured.
  2076.  
  2077.  
  2078.  
  2079.  
  2080.          
  2081.                                 SELECT-17
  2082.  
  2083.  
  2084.  
  2085.  
  2086.  
  2087.          
  2088.          select sum(qty) 
  2089.          from manu 
  2090.          where code = 'DD'; 
  2091.          
  2092.          SUM(qty)  
  2093.          --------  
  2094.               121  
  2095.           
  2096.          4 rows selected 
  2097.          
  2098.          More than one function can be used in a query: 
  2099.          
  2100.          
  2101.          select avg(qty), sum(qty) 
  2102.          from manu 
  2103.          where code = 'DD'; 
  2104.           
  2105.          AVG(qty) SUM(qty)  
  2106.          -------- --------  
  2107.                30      121  
  2108.           
  2109.          4 rows selected 
  2110.          
  2111.          
  2112.          Except for the group by clause, you cannot retrieve column values
  2113.          and functions based on column values in the same query. 
  2114.          
  2115.          COLUMN CALCULATIONS (*, /, +, -)
  2116.          
  2117.          Numeric columns can be used in calculations.  The following
  2118.          symbols are used:
  2119.          
  2120.          * = multiplication
  2121.          / = division
  2122.          + = addition
  2123.          - = subtraction
  2124.          The following shows the values if the defects doubled.
  2125.          
  2126.          select code, defects*2
  2127.          from manu;
  2128.          
  2129.          code      defects 
  2130.          ---- ------------ 
  2131.          GC             24 
  2132.          GC              0 
  2133.          NM             34 
  2134.          DD                
  2135.          DD             44 
  2136.          NM             30 
  2137.          DD             24 
  2138.          DD             30 
  2139.          GC              8 
  2140.          
  2141.          9 rows selected
  2142.  
  2143.  
  2144.                                 SELECT-18
  2145.  
  2146.  
  2147.  
  2148.  
  2149.  
  2150.          
  2151.          In a calculation, multiplication and division are processed
  2152.          first, then the addition and subtraction.  The order of
  2153.          processing can be changed by adding parentheses.  Calculations
  2154.          within parentheses are calculated first.  More than one column
  2155.          can be used in a calulation and even the same column can be
  2156.          referenced more than once.  The column name for the calculation
  2157.          is taken from the first column name however you can rename the
  2158.          column (as shown in the full documentation).  The data type for
  2159.          the calculation is based on the first column in the calculation.
  2160.          Calculations can occur also in the where clause.
  2161.          
  2162.          GROUP BY, HAVING
  2163.          
  2164.          The group by clause is normally used with aggregate functions. 
  2165.          It has two operations:
  2166.          
  2167.          1.  It sorts by the column name.
  2168.          
  2169.          2.  The aggregate functions only operate based on the rows that
  2170.          have the same column name.  The functions in essence create
  2171.          sub-totals based on the column name.
  2172.          
  2173.          What if we wanted to know the average defects in each state:
  2174.          
  2175.          select mst, avg(defects) 
  2176.          from manu 
  2177.          group by mst; 
  2178.           
  2179.          mst AVG(defects)  
  2180.          --- ------------  
  2181.          AZ             8  
  2182.          CA            16  
  2183.          ID             6  
  2184.          WA            18  
  2185.           
  2186.          4 rows selected 
  2187.          
  2188.          
  2189.          The column name after the group by must exist on the line with
  2190.          the select.  After the select you can only have column names in
  2191.          the group by and aggregate functions - nothing else.
  2192.          You can restrict the group by clause with the having component.
  2193.          In SSQL you are allowed one simple selection that includes the =.
  2194.          <, <=, >, >=, and !=.  As with the group by, the column name
  2195.          in the having component must exist in the select clause.
  2196.          If you only wanted a list of states with defects over 10% you
  2197.          would use the following:
  2198.          
  2199.  
  2200.  
  2201.  
  2202.  
  2203.  
  2204.  
  2205.                                 SELECT-19
  2206.  
  2207.  
  2208.  
  2209.  
  2210.  
  2211.          
  2212.          select mst, avg(defects) 
  2213.          from manu 
  2214.          group by mst having avg(defects) > 10; 
  2215.           
  2216.          mst AVG(defects)  
  2217.          --- ------------  
  2218.          CA            16  
  2219.          WA            18  
  2220.           
  2221.          2 rows selected 
  2222.          
  2223.          The group by clause can have up to two fields.  This is when you
  2224.          want a group within a group.  
  2225.          The group by clause can be used with the where clause.  When we
  2226.          found the average defects in each state we could have modified it
  2227.          by excluding product DD. 
  2228.          The where clause and the having clause are related.  However, the
  2229.          where clause operates on the whole table without regard for the
  2230.          grouping.  The having clause operates on the data that has
  2231.          already been grouped.  Because of the way the having clause
  2232.          works, most of the time the having clause contains an operation
  2233.          with an aggregate function because that is the basic purpose of
  2234.          the group by clause.
  2235.          
  2236.          ORDER BY
  2237.          
  2238.          The order by clause sorts the output of the table based on the
  2239.          column name(s) listed.  The original table is not changed. As
  2240.          with the group by clause, the order by clause can be used with
  2241.          the where clause.  For example, the following query will produce
  2242.          a list of manufacturing information sorted by defects for those
  2243.          items manufactured in Idaho.
  2244.          
  2245.          select date, code, defects 
  2246.          from manu 
  2247.          where mst = 'ID' 
  2248.          order by defects; 
  2249.           
  2250.          date     code defects  
  2251.          -------- ---- -------  
  2252.          02/02/87 DD            
  2253.          02/01/87 GC         0  
  2254.          02/07/87 GC        12  
  2255.           
  2256.          3 rows selected 
  2257.          
  2258.          You also can produce a sort within a sort.  The following query
  2259.          will produce an ordered list of dates and within each date the
  2260.          state will be sorted.
  2261.  
  2262.  
  2263.  
  2264.  
  2265.          
  2266.                                 SELECT-20
  2267.  
  2268.  
  2269.  
  2270.  
  2271.  
  2272.          
  2273.          select date, mst, code, qty 
  2274.          from manu 
  2275.          order by date, mst; 
  2276.           
  2277.          date     mst code qty  
  2278.          -------- --- ---- ---  
  2279.          02/01/87 ID  GC    55  
  2280.          02/02/87 CA  NM    93  
  2281.          02/02/87 ID  DD    25  
  2282.          02/02/87 WA  NM    25  
  2283.          02/03/87 WA  DD    46  
  2284.          02/04/87 AZ  DD    25  
  2285.          02/04/87 CA  DD    25  
  2286.          02/06/87 AZ  GC    43  
  2287.          02/07/87 ID  GC    15  
  2288.           
  2289.          9 rows selected 
  2290.          
  2291.          The order by clause assumes ascending order.  You can reverse the
  2292.          order by finding out in the full documentation
  2293.          
  2294.          When there are two columns to sort, one can be in ascending order
  2295.          while the other is in descending order. 
  2296.          In the current version of SSQL, you are limited to two sort
  2297.          columns.
  2298.          
  2299.          REDIRECTTO file_name
  2300.          
  2301.          Normally the report that is produced by the select command is
  2302.          displayed on the monitor.  However, with the redirectto clause,
  2303.          the report can be redirected to a text file or to the printer. 
  2304.          The text file is in standard ASCII text.  That is, there are no
  2305.          special characters in it.  The file name you give it must not
  2306.          have an extension because SSQL adds the extension ".txt".  
  2307.          If you wanted all the Arizona products stored in a file called
  2308.          ARIZ.TXT and you wanted the report sorted by product code, you
  2309.          would use the following query.
  2310.          
  2311.          select * 
  2312.          from  manu 
  2313.          where mst='AZ' 
  2314.          redirectto ariz 
  2315.          order by code; 
  2316.          
  2317.          The output would be stored in a file called ARIZ.TXT on your
  2318.          default drive.  To display the file on the monitor, you would
  2319.          exit SSQL and the following at the DOS prompt:
  2320.          
  2321.          type ariz.txt
  2322.  
  2323.  
  2324.  
  2325.  
  2326.          
  2327.                                 SELECT-21
  2328.  
  2329.  
  2330.  
  2331.  
  2332.  
  2333.          
  2334.          You also can have the report sent to the printer.  Replace the
  2335.          file name with prn or lpt1.  Make sure the printer is on when you
  2336.          use this option.  
  2337.          
  2338.          UNION
  2339.          The union command allows you to combine output from two or 
  2340.          three different select statements.  The two select statements 
  2341.          must have exactly the same output format - the same number of 
  2342.          columns and each column in the corresponding tables must be of 
  2343.          the same data type.  For the following example, I assume that 
  2344.          there are two tables with the same structure as SALES called 
  2345.          SALESCA and SALESAZ however it is not necessary that the tables 
  2346.          have the same structure, just the output columns.  The 
  2347.          following statement will get the customer code and quantity 
  2348.          from SALESCA for those customers who have purchased over 50 of 
  2349.          a particular item.  The output will also contain all the 
  2350.          customers and corresponding quanitities from the SALESAZ table. 
  2351.          The output is sorted by customer code.
  2352.  
  2353.          select cc,qty
  2354.          from salesca
  2355.          where qty > 50
  2356.          union
  2357.          select cc,qty
  2358.          from salesaz
  2359.          order by cc;
  2360.          
  2361.  
  2362.          EXERCISES (ANSWERS IN THE FULL DOCUMENTATION)
  2363.          
  2364.          IT IS IMPORTANT TO USE ONLY THE INFORMATION IN THE QUESTION TO
  2365.          FORMULATE THE QUERY.  For example, it is not acceptable in
  2366.          question 15 to look at the data and just select attorneys that
  2367.          have a category of 1.
  2368.          
  2369.          1.  List the rows in the attorney table.
  2370.          2.  List the rows in the attorney table but just the following
  2371.          columns in the following order:  name, salary, years of service
  2372.          3.  List the different case numbers in the casework table.
  2373.          4.  List the attorney names for attorneys with real estate
  2374.          specialties.
  2375.          5.  List the attorney names for attorneys with over 15 years of
  2376.          service
  2377.          6.  List the client names for clients not in California.
  2378.          7.  List the client columns for clients who do not have
  2379.          originating attorneys.
  2380.          8.  List the cases that have primary attorneys.
  2381.          9.  List the clients whose names end in Inc.
  2382.          10. List the client names that contain the word Discount.
  2383.          11. List the casework information for August of any year. 
  2384.          12. List the attorney names for category 1 and category 2
  2385.          attorneys.
  2386.  
  2387.  
  2388.                                SELECT-22
  2389.  
  2390.  
  2391.  
  2392.  
  2393.  
  2394.  
  2395.          13. List category 1 attorneys where the salary is over $50,000.
  2396.          14. Attorney names for all category 1 attorneys and category 2
  2397.          attorneys that earn over $40,000 and have over 4 years of
  2398.          service.
  2399.          15. List the attorneys who do not have a category of 2 or 3.
  2400.          16. Do number 15 another way.
  2401.          17. Do number 16 another way.
  2402.          18. List the attorneys who are category 1 or 2 and receive a
  2403.          salary of over $50,000.
  2404.          19. List the attorney names for attorneys who have worked 5, 6,
  2405.          10, or 12 years.
  2406.          20. Do number 19 another way.
  2407.          21. List the attorneys who have salaries greater than or equal to
  2408.          $40,000 and less than or equal to $60,000.
  2409.          22. Do number 21 another way.
  2410.          23. How many different originating attorneys are there?
  2411.          24. What is the average years of service for category 1
  2412.          attorneys?
  2413.          25. Who has the most years of service?
  2414.          26. List the number of entries in the casework table, by case
  2415.          number.
  2416.          27. How many hours has each attorney worked?
  2417.          28. What is the average salary by category?
  2418.          29. What is the lowest salary in each specialty?  Only include
  2419.          salaries greater than $30,000.
  2420.          30. How would you do #29 except save it in a file called
  2421.          NUM29.TXT?
  2422.          31. Sort the casework table by case number, and within case
  2423.          number the attorney number.
  2424.  
  2425.  
  2426.  
  2427.  
  2428.  
  2429.  
  2430.  
  2431.  
  2432.  
  2433.  
  2434.  
  2435.  
  2436.  
  2437.  
  2438.  
  2439.  
  2440.  
  2441.  
  2442.  
  2443.  
  2444.  
  2445.                                 SELECT-23
  2446.  
  2447.  
  2448.  
  2449.  
  2450.  
  2451.          JOINING TABLES
  2452.          
  2453.          
  2454.          Often, the data we need exists in more than one table. In order
  2455.          to extract the data, we need to select the appropriate columns
  2456.          and join the tables. The type of join primarily discussed is
  2457.          called a natural join but for the sake of brevity I will just use
  2458.          the word "join" alone. 
  2459.          Whenever we join tables, there must normally be a common column.
  2460.          Hear the common column contains the customer code. It is not
  2461.          important that the column names are different. The important
  2462.          aspect is that they describe precisely the same thing - i.e.,
  2463.          they have a common domain.
  2464.          In SQL, the join operation normally has three basic components:
  2465.          
  2466.          SELECT column names - for duplicate names, precede the column
  2467.          name with the table name and a period as in prod.code
  2468.          
  2469.          FROM table names separated by commas
  2470.          
  2471.          WHERE common columns are set to be equal
  2472.          
  2473.          The following shows how to combine the sales information from 
  2474.          the sales table and the customer name from cust:
  2475.          
  2476.          select name, pc, qty 
  2477.          from sales, cust
  2478.          where cc=code;
  2479.  
  2480.          name            pc qty 
  2481.          --------------- -- --- 
  2482.          Organomice      MW  23 
  2483.          Organomice      DD  34 
  2484.          QuarkCo         AB   2 
  2485.          Marswarp        MW  81 
  2486.          Technoharps     NM   3 
  2487.          Technoharps     MW  41 
  2488.          Technoharps     GC  33 
  2489.          Compugorp       MW 125 
  2490.          Compugorp       MW 947 
  2491.          Compugorp       DD 452 
  2492.          Compugorp       NM  32 
  2493.          
  2494.          name            pc qty 
  2495.          --------------- -- --- 
  2496.          QuarkCo         GC 845 
  2497.          Organomice      NM  45 
  2498.          Organomice      MW  73 
  2499.          Compugorp       GC  50 
  2500.          Compugorp       DD  32 
  2501.          Compugorp       GC  77 
  2502.          
  2503.          17 rows selected
  2504.  
  2505.          MUCH MORE IN THE FULL DOCUMENTATION
  2506.  
  2507.                                 JOIN-1
  2508.  
  2509.  
  2510.  
  2511.  
  2512.          
  2513.          EXERCISES (ANSWERS IN THE FULL DOCUMENTATION)
  2514.          
  2515.          Use only the information contained in the question!!!
  2516.          
  2517.          1.  List the attorney name and the specialty description (not
  2518.          just the specialty code) for each attorney.
  2519.          2.  List the casework table except replace the attorney number
  2520.          with the attorney name.
  2521.          3.  List the attorney name and specialty description for all
  2522.          originating attorneys who have clients in California. 
  2523.          4.  List the case numbers for all primary attorneys who are
  2524.          partners. 
  2525.          5.  List the attorney name and case numbers for all attorneys who
  2526.          are partners and work with clients from California.
  2527.          6.  List the attorney name and the number of hours worked by each
  2528.          attorney. 
  2529.          7.  List the attorney name and the number of hours worked by each
  2530.          attorney where the total number of hours is over  
  2531.          8.  List the attorney name and his or her supervisor's name for
  2532.          all those attorneys who do not work for partners. 
  2533.          9.  List any attorney who works for a client who has a case in
  2534.          Mesa, Arizona. 
  2535.          10. List all attorneys who work on at least one case outside
  2536.          Mesa, Arizona. 
  2537.          11. List the client name, case number, and originating attorney
  2538.          name for all clients in California.  Sort the report by case
  2539.          number within client name. 
  2540.  
  2541.  
  2542.  
  2543.  
  2544.  
  2545.  
  2546.  
  2547.  
  2548.  
  2549.  
  2550.  
  2551.  
  2552.  
  2553.  
  2554.  
  2555.  
  2556.  
  2557.  
  2558.  
  2559.  
  2560.  
  2561.  
  2562.  
  2563.  
  2564.  
  2565.  
  2566.  
  2567.  
  2568.                                 JOIN-2
  2569.  
  2570.  
  2571.  
  2572.  
  2573.  
  2574.  
  2575.  
  2576.          SIMPLE SUBQUERIES
  2577.          
  2578.          OVERVIEW
  2579.          
  2580.          In previous chapters, any comparison operation was done with
  2581.          actual values or a column name. Whenever we used the =, !=, <,
  2582.          > , <=, >=, in, all, or any there was a column name on one
  2583.          side and either a column name, an actual value or for the in,
  2584.          all, or any, a set of actual values on the other side. The
  2585.          problem is that often the values on right side of the comparison
  2586.          operation are not known until the query is made. In such cases
  2587.          the right side of the comparison is stated as a separate select
  2588.          command that is called a subquery. The result of the subquery is
  2589.          obtained first and the data is passed back to the previous query.
  2590.          Many queries can be linked together in this way.
  2591.          Another way of looking at it is that often, we need to break down
  2592.          our problem into more than one query. Each query will have its
  2593.          own select. We need subqueries when, in analyzing a problem, we
  2594.          find that we need information from one query before we can
  2595.          process another query. Sometimes we can either join tables or use
  2596.          a subquery. Typically, subqueries retrieve information more
  2597.          rapidly than joining tables.
  2598.          
  2599.          SUBQUERIES WITH =, !=, <, >, <=, AND >=
  2600.          
  2601.          These operators can be used with any subquery that produces a
  2602.          single value. It is commonly used with subqueries that contain  
  2603.          an aggregate function. The following shows the customer codes and
  2604.          quantity for those customers who have purchased more of product
  2605.          MW in a single purchase than the total purchased by customer BB.
  2606.          
  2607.          select  cc, qty
  2608.          from  sales
  2609.          where  pc = 'MW'
  2610.          and  qty >
  2611.            (select  sum(qty)
  2612.            from  sales
  2613.            where  cc = 'BB'
  2614.            and  pc = 'MW');
  2615.          
  2616.          cc qty 
  2617.          -- --- 
  2618.          EE  81 
  2619.          AA 125 
  2620.          AA 947 
  2621.          ZZ  73 
  2622.          
  2623.          4 rows selected
  2624.          
  2625.          This method also can be used with any column where we know it can
  2626.          only return a single value.
  2627.  
  2628.  
  2629.  
  2630.                                      SUB-1
  2631.  
  2632.  
  2633.  
  2634.  
  2635.  
  2636.          SET EXPRESSIONS - IN
  2637.          
  2638.          In the section on simple select statements, we used any and in
  2639.          with constants (actual values). The true power of the set
  2640.          expressions involves its use in subqueries. 
  2641.          
  2642.          We will first compare a join with a subquery. If we wanted to get
  2643.          product codes for all products purchased by customers in
  2644.          California, we could join the SALES table and the customer table.
  2645.          
  2646.          select  distinct pc
  2647.          from  sales, cust
  2648.          where  cc = code
  2649.          and  st = 'CA';
  2650.          
  2651.          pc 
  2652.          -- 
  2653.          MW 
  2654.          
  2655.          1 row selected
  2656.          
  2657.          
  2658.          Another approach would be to break it down into two queries. We
  2659.          know that we need a list of product codes from the SALES table.
  2660.          The SALES table has customer codes but it doesn't have
  2661.          information on states. Another way to state the problem is that
  2662.          we want all product codes from the SALES table where the customer
  2663.          code is in the set of customers that are in California. find the
  2664.          customers in California.
  2665.          
  2666.          
  2667.          select  distinct pc
  2668.          from  sales
  2669.          where  cc in
  2670.            (select  code
  2671.            from  cust
  2672.            where  st='CA');
  2673.          
  2674.          pc 
  2675.          -- 
  2676.          MW 
  2677.          
  2678.          1 row selected
  2679.          
  2680.          THERE IS MUCH MORE IN THE FULL DOCUMENTATION
  2681.          
  2682.          EXERCISES (ANSWERS IN THE FULL DOCUMENTATION)
  2683.          
  2684.          Use only the information found in the questions.  You will notice
  2685.          that some of the questions are the same as the ones in the
  2686.          chapter on joins.
  2687.          
  2688.          1.  List the case numbers that Hilda Wildabeas has worked on.
  2689.          2.  List the case numbers for all primary attorneys who are
  2690.          partners. 
  2691.  
  2692.  
  2693.  
  2694.                                      SUB-2
  2695.  
  2696.  
  2697.  
  2698.  
  2699.  
  2700.  
  2701.          3.  List the attorney name for all attorneys who are partners and
  2702.          work with clients from California. (Not just as primary
  2703.          attorneys)
  2704.          4.  List any attorney who works for a client who has a case in
  2705.          Mesa, Arizona. 
  2706.          5.  List all attorneys who work on at least one case outside
  2707.          Mesa, Arizona. 
  2708.          6.  List all attorneys who do not work on any cases in Mesa,
  2709.          Arizona. 
  2710.          7.  List attorneys with above average years of service.
  2711.          8.  List the names of attorneys who are originating attorneys
  2712.          9.  List attorney numbers for attorneys who work on both case 01
  2713.          and case 04. 
  2714.          10. List attorney numbers for attorneys who do not work on case
  2715.          01 or 04.
  2716.          11. List attorney names for originating attorneys who do not have
  2717.          clients in California.
  2718.  
  2719.  
  2720.  
  2721.  
  2722.  
  2723.  
  2724.  
  2725.  
  2726.  
  2727.  
  2728.  
  2729.  
  2730.  
  2731.  
  2732.  
  2733.  
  2734.  
  2735.  
  2736.  
  2737.  
  2738.  
  2739.  
  2740.  
  2741.  
  2742.  
  2743.  
  2744.  
  2745.  
  2746.  
  2747.  
  2748.  
  2749.  
  2750.  
  2751.  
  2752.  
  2753.  
  2754.                                      SUB-3
  2755.  
  2756.  
  2757.  
  2758.  
  2759.  
  2760.  
  2761.          CORRELATED SUBQUERIES 
  2762.           
  2763.          OVERVIEW 
  2764.           
  2765.          A correlated subquery is a subquery that refers to a table 
  2766.          OUTSIDE the subquery. This causes correlated subqueries to be 
  2767.          processed differently from simple subqueries. Simple subqueries 
  2768.          are processed once, and the result is passed back to the main 
  2769.          query. The correlated subquery is processed for every row in the
  2770.          
  2771.          main query. This allows us to process a subquery based on a 
  2772.          particular column value in each row of the main query. It can get
  2773.          even more complex with multiple levels of subqueries. With simple
  2774.          subqueries, each subquery is isolated and passes back a single 
  2775.          set of values to the previous level. With correlated subqueries,
  2776.          each subquery can depend on more than one of the previous levels.
  2777.          
  2778.          If we are working on the SALES table we can have a subquery that
  2779.          is processed for each customer or product, not the whole table. 
  2780.          Another way of looking at it is that for each row in the main 
  2781.          query we can take a value, say customer code, and process a 
  2782.          subquery based on the customer. The value it produces for the 
  2783.          customer can be compared to another value in the same row in the
  2784.          main query. We will do that in the next section when we want to 
  2785.          display the complete row in a SALES table for every customer 
  2786.          purchase where the quantity purchased is above average FOR THAT 
  2787.          CUSTOMER. With a simple subquery we could only compare customer 
  2788.          purchases with average SALES for the whole table or a SINGLE 
  2789.          customer. Using the correlated subquery is sometimes like being 
  2790.          able to compare values to a subquery with a GROUP BY.  
  2791.           
  2792.          Often, we just want to test for the existence of a row in a 
  2793.          subquery based on a value in the first level of the query so SQL
  2794.          has the exists predicate. By using the correlated subquery we can
  2795.          create some rather complex tests on sets of data.  
  2796.           
  2797.          AGGREGATE FUNCTIONS 
  2798.           
  2799.          Let's contrast the correlated subquery with a simple subquery 
  2800.          that is produced below: 
  2801.           
  2802.          select * 
  2803.          from sales  
  2804.          where qty > 
  2805.           (select avg(qty) 
  2806.           from sales); 
  2807.           
  2808.          date     bc cc sn pc qty  
  2809.          -------- -- -- -- -- ---  
  2810.          04/01/87 1A AA 10 MW 947  
  2811.          04/08/87 1A AA 10 DD 452  
  2812.          04/08/87 1B DD 11 GC 845  
  2813.           
  2814.          3 rows selected 
  2815.  
  2816.  
  2817.                                      COR-1
  2818.  
  2819.  
  2820.  
  2821.  
  2822.           
  2823.          It solves the problem for displaying the complete row in the 
  2824.          SALES table for every customer purchase where the quantity 
  2825.          purchased is above average (based on the complete SALES table). 
  2826.           
  2827.          The way a simple subquery works is that it is processed ONCE, and
  2828.          
  2829.          the result is passed back to the main query. 
  2830.          Now let's change the query a little bit. Now we want to display 
  2831.          the complete row in a SALES table for every customer purchase 
  2832.          where the quantity purchased is above average FOR THAT CUSTOMER.
  2833.          
  2834.          Without knowing much else, it should at least be obvious that we
  2835.          need to process the subquery more than once since the query 
  2836.          states that we need an average for EACH customer to compare the 
  2837.          quantity to. Actually, the correlated subquery is more 
  2838.          inefficient than this. Usually, for EVERY row in the first table,
  2839.          
  2840.          the subquery is processed: 
  2841.           
  2842.          select * 
  2843.          from sales sales1 
  2844.          where qty > 
  2845.           (select avg(qty) 
  2846.           from sales 
  2847.           where cc = sales1.cc); 
  2848.           
  2849.          date     bc cc sn pc qty  
  2850.          -------- -- -- -- -- ---  
  2851.          04/12/87 1B BB 27 MW  41  
  2852.          04/15/87 2A BB 33 GC  33  
  2853.          04/01/87 1A AA 10 MW 947  
  2854.          04/08/87 1A AA 10 DD 452  
  2855.          04/08/87 1B DD 11 GC 845  
  2856.          04/01/87 2A ZZ 12 NM  45  
  2857.          04/08/87 2A ZZ 12 MW  73  
  2858.           
  2859.          7 rows selected 
  2860.           
  2861.           
  2862.          On the last line of the subquery we set cc = sales1.cc. This 
  2863.          means that for every row in the first table, it will take the 
  2864.          customer code (sales1.cc), and compute the average quantity for 
  2865.          that customer. It will then determine whether the quantity is 
  2866.          greater than the average quantity. We had to use the alias sales1
  2867.          to distinguish one SALES table from the other. Following any 
  2868.          table name we can rename it to avoid confusion or ambiguity. The
  2869.          alias can be any name that would be a valid table name. 
  2870.           
  2871.  
  2872.  
  2873.  
  2874.  
  2875.  
  2876.                                      COR-2
  2877.  
  2878.  
  2879.  
  2880.  
  2881.  
  2882.          EXISTS/NOT EXISTS  
  2883.           
  2884.          The exists predicate tests for the existence of a row selected in
  2885.          a subquery BASED ON DATA IN THE CURRENT QUERY (and possibly outer
  2886.          queries). We do not care what data selected happens to be in the
  2887.          subquery, we just want to know that it exists. The following 
  2888.          shows how to find the names of products in the SALES table. 
  2889.           
  2890.          select descrip 
  2891.          from prod 
  2892.          where exists 
  2893.           (select * 
  2894.           from sales 
  2895.           where code = pc); 
  2896.           
  2897.          descrip          
  2898.          ---------------  
  2899.          Megawamp         
  2900.          Gigasnarf        
  2901.          Nanomouse        
  2902.          Dynamic Disk     
  2903.           
  2904.          4 rows selected 
  2905.           
  2906.          THE TRUE VALUE OF THE CORRELATED SUBQUERY CAN BE SEEN IN THE FULL
  2907.          DOCUMENTATION - ABOUT 40 PAGES!!! 
  2908.           
  2909.          EXERCISES (ANSWERS IN THE FULL DOCUMENTATION) 
  2910.           
  2911.          1.  List the state and the attorney with above average years of 
  2912.          service in that state.  
  2913.          2.  List all attorneys who work in over 2 cases.  
  2914.          3.  List all attorney numbers and corresponding cases for those 
  2915.          who have worked over 10 hours on a single case.  
  2916.          4.  List the case numbers for all primary attorneys who are 
  2917.          partners.  
  2918.          5. List the attorney numbers for all attorneys (not just primary
  2919.          attorneys) who are partners and work with clients from 
  2920.          California.  
  2921.          6.  List the originating attorneys who have clients in all 
  2922.          states.  
  2923.          7.  List the originating attorneys who have clients in all states
  2924.          except California  
  2925.  
  2926.  
  2927.  
  2928.  
  2929.  
  2930.  
  2931.  
  2932.  
  2933.  
  2934.  
  2935.  
  2936.                                      COR-3
  2937.  
  2938.  
  2939.  
  2940.  
  2941.  
  2942.  
  2943.          CONVERTING ENGLISH TO SQL
  2944.          
  2945.          OVERVIEW
  2946.          The previous chapters emphasized the point of view of SQL and
  2947.          understanding all the parts.  This chapter is more concerned with
  2948.          the point of view of a person wanting to solve queries using SQL. 
  2949.          There are three pieces of important information - understanding
  2950.          the database, understanding the query, and finally writing the
  2951.          SQL statement.
  2952.          
  2953.          ***************************************************************
  2954.          **  THE FULL DOCUMENTATION HAS THE SQL STATEMENTS AND        **
  2955.          **  EXPLANATIONS.  THE FOLLOWING WILL GIVE YOU A GOOD        **
  2956.          **  IDEA OF THE FLEXIBILITY OF SQL AND THE VARIETY OF        **
  2957.          **  PROBLEMS THAT IT CAN SOLVE  (IT SHOULD ALSO CONVINCE     **
  2958.          **  YOU TO REGISTER SO YOU CAN GET THE FULL DOCUMENTATION!)  **
  2959.          ***************************************************************
  2960.          
  2961.          UNDERSTANDING THE DATABASE
  2962.          In the chapter on the example databases, when a column was
  2963.          described, any other common columns in other tables was mentioned
  2964.          also.  This commonality allows us to work with information that
  2965.          spans tables.  There are other ways to group this information
  2966.          that may be useful.  The first way is to group them by the common
  2967.          domains.  The Monolith Manufacturing database has four basic
  2968.          domains (I do not count date): state, product code, employee
  2969.          number, and branch code. We can group them as follows:
  2970.          
  2971.          
  2972.          
  2973.          NAME TABLES COLUMN NAME
  2974.          STATE CUST st
  2975.           MANU mst
  2976.           BRANCH st
  2977.          
  2978.          PRODUCT MANU code
  2979.          CODE PROD code
  2980.           SALES pc
  2981.          
  2982.          EMPLOYEE SALES sn
  2983.          NUMBER EMP enum
  2984.           EMP mgrnum
  2985.           BRANCH mgrnum
  2986.          
  2987.          BRANCH BRANCH code
  2988.          CODE SALES bc
  2989.          
  2990.          Whenever we join tables or use a subquery, we must use common
  2991.          domains so the above information is essential.
  2992.          
  2993.          
  2994.          
  2995.          
  2996.          
  2997.          
  2998.                                     ENG-1
  2999.  
  3000.  
  3001.  
  3002.  
  3003.  
  3004.          GENERAL APPROACH TO SOLVING THE QUERY
  3005.          (This section is only in the full documentation)
  3006.  
  3007.          The Numbering Scheme For Queries
  3008.          Each key word or words is given a number from 1 to 4.  The number
  3009.          is further divided into numbers to the right of a decimal as to
  3010.          how the keywords are used.  Each example is  followed by a
  3011.          letter.  Query 3.2B corresponds to the second example(B) for the
  3012.          second use(2) of keyword 3.  Queries preceded by an 'N' means
  3013.          that it is a negated version of the query.  There may be more
  3014.          than one way to negate it so the version is in the right-most
  3015.          position as in: N3.2B.2.
  3016.          
  3017.          1 - AND
  3018.          The and is used to refer to information in columns.  In its
  3019.          simplest form, it is used to select which columns to display.  It
  3020.          is commonly used to determine which rows to display.  As you will
  3021.          see from the examples, there are many ways to translate and into
  3022.          SQL.
  3023.          1.1 Used to describe which columns to display.
  3024.          1.1A List the date of manufacture, product code, and quantity.
  3025.          
  3026.          1.2 Used when you need or:
  3027.          1.2A List the rating for Organomice and Compugorp.
  3028.          1.2B List the dates of purchase for products MW and NM.
  3029.          
  3030.          1.3 Ambiguous 
  3031.          Solution could be an or a more complex construction.  Usually the
  3032.          ambiguity arises because we are referring to sets of values
  3033.          instead of single values.  The answer could be a union of the two
  3034.          sets (or) or an intersection (subquery). Query 1.2A asks for
  3035.          rating which will be a single value for each customer.  In query
  3036.          1.3A, we are concerned with two sets of values - products
  3037.          manufactured in Washington and products manufactured in Idaho. 
  3038.          Just because it refers to sets of values does not necessarily
  3039.          mean that it is ambiguous.  Query 1.2B refers to sets but (in my
  3040.          opinion) it is not ambiguous.  Contrast 1.2B with 1.3B which I
  3041.          think is ambiguous.
  3042.          1.3A  List the products manufactured in Washington and Idaho.
  3043.          1.3B  List the dates when products MW and NM were purchased.
  3044.          
  3045.          1.4. Not Ambiguous
  3046.          However the and translates into a more complex SQL statement. 
  3047.          What we are after here is often an intersection of the sets as
  3048.          you can see in 1.4A and 1.4B.  That is, we want values common to
  3049.          all sets. An intersection can normally be achieved through a
  3050.          series of subqueries.
  3051.          1.4A  List the products manufactured in both Washington and
  3052.          Idaho.
  3053.          1.4B  List the dates when all of the following products were
  3054.          purchased: MW, NM, and GC.
  3055.          1.4C  List the customers who are from Arizona and have purchased
  3056.          product MW.  
  3057.  
  3058.  
  3059.  
  3060.  
  3061.                                     ENG-2
  3062.  
  3063.  
  3064.  
  3065.  
  3066.  
  3067.          1.5 Definitely need an and in the where clause.  
  3068.          The and is used when we want to test two or more different
  3069.          columns in a row within a single table.
  3070.          1.5A  List the customers who are from Arizona and have a rating
  3071.          over 15.
  3072.          
  3073.          1.6 Implied and
  3074.          1.6A  List the Arizona customers who have a rating over 15.
  3075.          Same as 1.5A
  3076.          1.6B  List the Arizona customers who have purchased product MW.
  3077.          Same as 1.4C
  3078.          
  3079.          OR
  3080.          2.1  Normally translates to or:
  3081.          2.1A  List the customer codes for customers who have purchased MW
  3082.          or NM.
  3083.          2.1B  List the customers names for customers who have purchased
  3084.          MW or NM.
  3085.          2.1C  List the customer names for customers who are from
  3086.          California or have a rating less than 10.
  3087.          
  3088.          2.2 Comparison operators <= and >=.
  3089.          2.2A  List customers who have a rating greater than or equal to
  3090.          15.
  3091.          
  3092.          3 - WHICH, WHO, WHOSE, THAT, WITH, WHERE
  3093.          The above words have various meanings but I will emphasize their
  3094.          use in clauses.  Clauses connect parts of sentences.  Sometimes
  3095.          is makes a query clearer when it is converted to a form that has
  3096.          a clause.  The first six queries ask the same thing in different
  3097.          ways.  
  3098.          3.1A  Which customers have a rating over 10?
  3099.          3.1B  Who has a rating over 10?
  3100.          3.1C  List customers who have ratings over 10.
  3101.          3.1D  List customers with ratings over 10.
  3102.          3.1E  List every customer whose rating is over 10.
  3103.          3.1F  List every customer where the rating is over 10.
  3104.          Queries 3.1C through 3.1D are the most straightforward because
  3105.          the first part describes what is to be displayed and the second
  3106.          part describes the logic to determine which rows we want.  Many
  3107.          of the following can be rewritten in a similar manner but I will
  3108.          usually present only one approach.
  3109.          
  3110.          3.2  Link tables  
  3111.          3.2A  List the customer's states for customers who have purchased
  3112.          products  that are manufactured in Idaho.
  3113.          3.2B  Get branch codes for branches that sell products that are
  3114.          sold by branches that sell product DD. This is a rather
  3115.          convoluted one but shows the power of recognizing clauses. 
  3116.          Logically we want to find all the branches that sell product DD.
  3117.  
  3118.  
  3119.  
  3120.  
  3121.  
  3122.  
  3123.                                     ENG-3
  3124.  
  3125.  
  3126.  
  3127.  
  3128.  
  3129.          Then we want to take those branches and find all the products
  3130.          they sell.  With that set of products, we want to find all the
  3131.          branches that sell those products.  Notice how in 3.2B each
  3132.          subquery corresponds to each clause.  Be careful doing the
  3133.          conversion.  Sometimes the "that" does not have to be included. 
  3134.          Get branch codes for branches that sell products sold by branches
  3135.          that sell product DD.  In the previous sentence the "that" is
  3136.          missing between "products" and "sold".
  3137.          3.2C  List the names of employees who have sold products to
  3138.          Compugorp.
  3139.          3.2D. Get customer names and product names for customers who have
  3140.          purchased items that are manufactured outside their own state.
  3141.          3.2E  Get branch code, customer names and product names for those
  3142.          who purchase products that are manufactured in their own state
  3143.          from a branch in their own state.
  3144.          3.2F  Get branch numbers for branches that sell to both AA and
  3145.          BB.
  3146.          3.2G Who are the managers?
  3147.          3.2H  What are the names of managers who actually sold something?
  3148.          3.2I  What are the names of managers whose salespeople have sold
  3149.          products to Organomice?
  3150.          3.2J  What are the names of managers whose salespeople have sold
  3151.          products ONLY to Organomice? We have to add something to exclude
  3152.          salespeople who sold to a customer other than Organomice.
  3153.          3.2K List the names of Xero Xanadu's subordinates who sold
  3154.          Technowidgits and what were the quantities sold?
  3155.          3.2L  Get branch codes for branches that sell to a Washington or
  3156.          Arizona customer a product manufactured in Oregon.
  3157.          3.2M Get all pairs of state values where the branch in the first
  3158.          state sells to a customer in a second state.
  3159.          
  3160.          3.4  Implied that or who  
  3161.          The words in parentheses in the following three queries are
  3162.          optional.
  3163.          3.4A  Get product codes for products (that were) sold to any
  3164.          customer in California.
  3165.          3.4B  Get product numbers and corresponding customer names for
  3166.          products (that are) manufactured in the same state as a customer.
  3167.          3.4C  Get customer numbers for customers (who were) sold at least
  3168.          one product from a branch in the same state.
  3169.          
  3170.          4 - ANY/ALL
  3171.          Translating any and all cause the same sort of problems that were
  3172.          caused by and.  This is because the way we use a word in English
  3173.          may be different from the way we translate it to SQL.
  3174.          First of all, I will skip over any queries in which the any or
  3175.          all serve no purpose.  Often they are added for emphasis and CAN
  3176.          mean the same thing as in queries 4.1A and 4.1B.  Normally, the
  3177.          word any serves no purpose unless it is used in a comparison.
  3178.  
  3179.  
  3180.  
  3181.  
  3182.                                     ENG-4
  3183.  
  3184.  
  3185.  
  3186.  
  3187.  
  3188.          4.1  All and any not needed.  
  3189.          4.1A  Get all product codes for all products purchased by
  3190.          customers in California. 
  3191.          Same as 3.4A
  3192.          4.1B  Get any product code for any products purchased by any
  3193.          customer in California. 
  3194.          Same as 3.4A
  3195.          4.1C  Get product codes for products purchased by customers in
  3196.          California. 
  3197.          Same as 3.4A
  3198.          
  3199.          4.2  All used as part of a FORALL quantifier
  3200.          This was discussed in detail in the chapter on correlated
  3201.          subqueries. It is used when we want to compare one set of values
  3202.          to another set of values instead of comparing a single value to a
  3203.          set of values or comparing a single value to a single value.
  3204.          4.2A  Get product codes for products purchased by all customers.
  3205.          4.2B  Get product codes for products purchased by all customers
  3206.          in California.
  3207.          4.2C  Get product codes for products where the minimum defects is
  3208.          greater than 10 and has been purchased by all customers.
  3209.          4.2C  Get customer names for customers who have purchased all
  3210.          products.
  3211.          4.2D  States that manufacture all products sold by a branch in
  3212.          the same state.
  3213.          4.2E  Branches that have sold all the products manufactured in
  3214.          their own state.
  3215.          4.2F Branches that have sold all the products to all the
  3216.          customers.
  3217.          
  3218.          4.3 Using any and all in comparisons
  3219.          These are very deceiving.  The use of any normally translates to
  3220.          all in SQL or an equivalent construction.  This idea is discussed
  3221.          in the chapter on subqueries.
  3222.          4.3A  List products that have defects greater than any defect for
  3223.          products manufactured in Idaho.  Compare this to 4.3B.
  3224.          4.3B  List products that have defects greater than all defects
  3225.          for products manufactured in Idaho.
  3226.          Query 4.3A is normally interpreted as the same as 4.3B although
  3227.          logically it means something different.  If you think that they
  3228.          are different in common English then I guess SQL has distorted
  3229.          your thinking a bit.  When a tennis player says that he is better
  3230.          than ANY tennis player, we assume that he means that he is better
  3231.          than ALL tennis players.  The confusion is caused by the fact
  3232.          that in most contexts any and all are very different as in the
  3233.          following two sentences.  Give me any book.  Give me all the
  3234.          books.
  3235.          4.3C  List products that have defects less than any (all) defects
  3236.          for products manufactured in Idaho.
  3237.          4.3D  List products that have defects the same as any defects for
  3238.          products manufactured in Idaho.  In this case we cannot replace
  3239.          the "any" with an "all" and retain the same meaning.
  3240.          
  3241.  
  3242.  
  3243.                                     ENG-5
  3244.  
  3245.  
  3246.  
  3247.  
  3248.  
  3249.          N - NEGATION
  3250.          In this section I will take many of the above queries and negate
  3251.          them.
  3252.          N1.2a List the rating for all except Organomice and Compugorp.
  3253.          N1.3A.1  List the products manufactured in neither Washington nor
  3254.          Idaho.
  3255.          N1.3A.2 List the products not manufactured in both Washington and
  3256.          Idaho.
  3257.          N1.3B.1  List the dates that products MW or NM were not
  3258.          purchased.
  3259.          N1.3B.2  List the dates that products other than MW or NM were
  3260.          purchased.
  3261.          N1.4B.1  List the dates when all of the following products were
  3262.          not purchased: MW, NM, and GC.
  3263.          N1.4B.2  List the dates when none of the following products were
  3264.          purchased: MW, NM, and GC.
  3265.          N1.4B.3  List the dates when something other than the following
  3266.          products were purchased: MW, NM, and GC.
  3267.          N1.4C.1  List the customers who are from Arizona and have not
  3268.          purchased product MW.  
  3269.          N2.1B.1  List the customers names for customers who have not
  3270.          purchased MW or NM.
  3271.          N2.1B.2  List the customers names for customers have purchased
  3272.          products other than MW and NM.
  3273.          The difference between N2.1B.1 and N2.1B.2 is that the first one
  3274.          would exclude any customer who has purchased MW or NM.  The
  3275.          second one would include the customer as long as the customer
  3276.          purchased another product.
  3277.          N3.2A.1  List the customer's states for those customers who have
  3278.          not purchased products manufactured in Idaho.
  3279.          N3.2A.2  List the customer's states for those customers who have 
  3280.          purchased products not manufactured in Idaho.
  3281.          N3.2A.2  List the customers' states for those customers who have 
  3282.          purchased products manufactured outside (not in) Idaho.
  3283.          The above differs from the previous query in that it will include
  3284.          any product manufactured in Idaho IF it is manufactured outside
  3285.          Idaho.
  3286.          N3.2B.1  Get branch codes for branches that do not sell products
  3287.          that are sold by branches that sell product DD.
  3288.          N3.2B.2  Get branch codes for branches that sell products that
  3289.          are not sold by branches that sell product DD.
  3290.          N3.2B.3  Get branch codes for branches that sell products that
  3291.          are sold by branches that do not sell product DD.
  3292.          The negation is on selling product DD.  This is different from
  3293.          saying: "...branches that sell products other than DD".  
  3294.          N3.2C.1  List the names of employees who have not sold products
  3295.          to Compugorp.
  3296.          N3.2D.1 Get customer names and product names for customers who
  3297.          have purchased items not manufactured outside their own state.
  3298.          N3.2D.2 Get customer names for customers who have not purchased
  3299.          items manufactured outside their own state.  
  3300.  
  3301.  
  3302.  
  3303.  
  3304.                                     ENG-6
  3305.  
  3306.  
  3307.  
  3308.  
  3309.  
  3310.          N3.2D.3 Get customer names for customers who have only purchased
  3311.          items in their own state. 
  3312.          At first glance N3.2D.3 seems the same as N3.2D.2 but N3.2D.2
  3313.          would display customers who have not purchased anything.  We have
  3314.          to add some more to make sure that the customer actually made a
  3315.          purchase.
  3316.          N3.2F.1  Get branch codes for branches that do not sell to both
  3317.          AA and BB.
  3318.          N3.2F.1  Get branch codes for branches that sell to neither AA
  3319.          nor BB.
  3320.          N3.2F.1  Get branch codes for branches that do not sell to AA or
  3321.          BB.
  3322.          N3.2G.1  What are the names of managers who did not sell
  3323.          anything?
  3324.          N3.2I.1  What are the names of managers whose salespeople have
  3325.          not sold products to Organomice?
  3326.          N3.2I.2  What are the names of managers whose salespeople have 
  3327.          sold products to everyone except Organomice?  This becomes a
  3328.          tough one because the opposite of "only Organomice" in 3.2I is
  3329.          "everyone except Organomice" and "everyone" corresponds to the
  3330.          FORALL quantifier covered in the chapter on correlated
  3331.          subqueries.  The core of this query has to do with "salespeople
  3332.          selling to ALL customers (except Organomice)".
  3333.          N3.2L.1  Get branch codes for branches that sell to a Washington
  3334.          or Arizona customer a product not manufactured in Oregon.
  3335.          N3.2L.2  Get branch codes for branches that sell to a Washington
  3336.          or Arizona customer a product manufactured outside of Oregon.
  3337.          N4.2A.1  Get product codes for products not purchased by all
  3338.          customers.
  3339.          N4.2A.2  Get product codes for products purchased by none of the
  3340.          customers.
  3341.          N4.2D.1  States that do not manufacture all products sold by a
  3342.          branch in the same state.
  3343.          N4.2D.2  States that manufacture no products sold by a branch in
  3344.          the same state.
  3345.          N4.2E.1  Branches that have not sold all the products
  3346.          manufactured in their own state.
  3347.          N4.2E.2  Branches that have sold none of the products
  3348.          manufactured in their own state.
  3349.          N4.2E.3  Branches that have sold all the products not
  3350.          manufactured in their own state.
  3351.          N4.2E.4  Branches that have sold all the products manufactured
  3352.          outside their own state.
  3353.  
  3354.                                     ENG-7
  3355.  
  3356.  
  3357.  
  3358.  
  3359.  
  3360.          CREATE  TABLE
  3361.          
  3362.          OVERVIEW
  3363.          The CREATE TABLE command is used to create a table file. You can
  3364.          control the contents of the columns in two ways.  You can prevent
  3365.          a column from being null and you can make sure that a column or
  3366.          set of columns never contain duplicate values.  These
  3367.          restrictions allow us to maintain valid primary keys when we use
  3368.          the INSERT command to add rows to a table or the UPDATE command
  3369.          to modify values in a table.
  3370.          
  3371.          SYNTAX
  3372.          
  3373.          CREATE TABLE table_name (
  3374.            column_definition [, column_definition...]
  3375.            [, uniqueness_constraint]
  3376.          );
  3377.  
  3378.          EXAMPLE:
  3379.          The statement below shows how to create the cust table that  the
  3380.          documentation uses. 
  3381.          
  3382.          create table cust ( 
  3383.          code char(2) not null unique,   
  3384.          name char(15) not null,
  3385.          st char(2) not null,
  3386.          rating numeric(2)
  3387.          );
  3388.          
  3389.          You can define up to 27 columns in a table.
  3390.          As with all SQL commands, they can be in uppercase or lowercase. 
  3391.          After the name of the table, parentheses enclose the column
  3392.          definitions. The column definitions are put on separate lines to
  3393.          aid in readability.  SQL never requires anything to be put on
  3394.          separate lines or have specific spacing. Note that all column
  3395.          definitions except the last one has a comma after it.
  3396.          The above command states that there will be four columns in the
  3397.          cust table:
  3398.          
  3399.          code can have up to two characters.  It is the primary key so
  3400.          nulls will not be allowed and each code will be unique.
  3401.          
  3402.          name can have up to fifteen characters. It must contain a value.
  3403.          
  3404.          st   is the state postal code and it can have up to 2 characters.
  3405.          It must contain a value.
  3406.          
  3407.          rating is the customer rating.  Since NOT NULL is not specified,
  3408.          the customer rating is optional - it does not have to be entered.
  3409.  
  3410.  
  3411.  
  3412.  
  3413.  
  3414.  
  3415.                                       CREATE-1
  3416.  
  3417.  
  3418.  
  3419.  
  3420.  
  3421.          COMPONENTS OF CREATE TABLE
  3422.          
  3423.          table_name
  3424.          A table name can be from 1 to 10 characters.  The first eight
  3425.          characters must be unique.  The first character must be a letter 
  3426.          of the alphabet.  The rest can be letters or digits, _
  3427.          (underscore), or #.  The table files are created on the disk as in
  3428.          table_name.DBF and table_name.SQD.  For example, the cust 
  3429.          table, created above would be stored as CUST.DBF and CUST.SQD 
  3430.          on your disk.  However, from SSQL, you would always refer to it 
  3431.          as CUST.  Since SSQL adds the extension of DBF and SQD to all 
  3432.          table files, you must not use a period in your table name.   
  3433.          
  3434.          column_definition
  3435.          
  3436.          column_name data_type [NOT NULL [UNIQUE]]
  3437.          
  3438.          column_name
  3439.          A column name can be from 1 to 10 characters.  The first
  3440.          character must be a letter of the alphabet.  The rest can be
  3441.          letters or digits.  In creating column names remember that when
  3442.          you display a table, the full column name is displayed too as the
  3443.          heading.  Long column names tend to fill the screen (or printer)
  3444.          very rapidly when you want to display many columns.  The column
  3445.          name is displayed exactly as you type it, retaining the uppercase
  3446.          and lowercase letters.  The create statement above created the
  3447.          column names and hence the headings in lowercase letters.  We
  3448.          could have written it as shown below.
  3449.          
  3450.          create table cust ( 
  3451.          CODE char(2) not null unique,   
  3452.          NAME char(15) not null,
  3453.          ST char(2) not null,
  3454.          RATING numeric(2)
  3455.          );
  3456.          
  3457.          With the statement above, whenever we display data from the
  3458.          table, the headings would in uppercase.
  3459.  
  3460.          data_type
  3461.          The data types in SQL fall into two broad categories - numbers
  3462.          and characters.  Within the numbers category, there are exact
  3463.          numeric types and approximate numeric types.  However, all 
  3464.          numeric data types are converted to the dBase numeric data 
  3465.          type.  The other types are included to maintain compatibility 
  3466.          with ANSI SQL.  Two non-standard data have been added to 
  3467.          maintain compatibility with dBase - Date and Logical.
  3468.  
  3469.  
  3470.  
  3471.          
  3472.  
  3473.                                 CREATE-2
  3474.  
  3475.  
  3476.  
  3477.  
  3478.  
  3479.          Numeric
  3480.          
  3481.           (Exact Numeric)
  3482.           NUMERIC[(length [,decimal_places])]
  3483.           DECIMAL[(length [,decimal_places])]
  3484.           DEC[(length [,decimal_places])]
  3485.           INTEGER
  3486.           INT
  3487.           SMALLINT
  3488.           (Approximate Numeric covered in the full documentation)
  3489.  
  3490.          When you use numeric data types the values are always
  3491.          right-justified (values are pushed to the right so all the
  3492.          decimal lines up).  Data must be of this type in order to use any
  3493.          of the numeric functions such as avg, max, min, and sum.  
  3494.          The first three data types in the exact numeric category -
  3495.          NUMERIC, DECIMAL, and DEC, can be used interchangeably.  It
  3496.          sounds a little odd that a data type called DECIMAL does not
  3497.          necessarily mean that the contents of the column will contain a
  3498.          decimal place since the modifier decimal_places is optional. 
  3499.          
  3500.          length (optional)
  3501.          The length of the column be up to 12 digits (including the
  3502.          decimal place. Without this specification, the length is 1.
  3503.          
  3504.          decimal_places (optional)
  3505.          The number of places to the right of the decimal.  For Example:  
  3506.          
  3507.          cost  numeric(5,2)
  3508.          
  3509.          This would allow a maximum of 99.99 to be stored in the column.
  3510.          
  3511.          This also could be defined as:
  3512.          
  3513.           cost decimal(5,2)
  3514.          or 
  3515.           cost dec(5,2)
  3516.          
  3517.          It is allowable to use whole numbers even though you define it as
  3518.          having decimals.  For example, you want to enter grades that are
  3519.          from  0 to 100 but when you calculate grade averages, you want it
  3520.          calculated to one tenth of a grade point.  You would define the
  3521.          column as:
  3522.          
  3523.           grade  numeric(5,1)
  3524.          
  3525.          You would enter the grades as whole numbers but when the average
  3526.          is calculated, the decimal would be included. 
  3527.          With NUMERIC, DECIMAL and DEC, if you omit the dec_places
  3528.          modifier, the result is a whole number.
  3529.          
  3530.          Data types such as INTEGER (which is the same as INT) and
  3531.          SMALLINT have lengths associated with them.  The ANSI standards
  3532.          state that the lengths shall be determined by the implementor so
  3533.          I have picked lengths of ten and five respectively.
  3534.  
  3535.                                CREATE-3
  3536.  
  3537.          
  3538.          INTEGER is the same as numeric(10) or decimal(10) or dec(10)
  3539.          SMALLINT is the same as numeric(5) or decimal(5) or dec(5)
  3540.          
  3541.          You can see that although there are six exact numeric data types,
  3542.          you only need one.
  3543.          
  3544.          Characters
  3545.          
  3546.           CHARACTER[(length)]
  3547.           CHAR[(length)]
  3548.  
  3549.          These data types can be used for any column that you do not use
  3550.          in a calculation.  Although the data is usually a combination of
  3551.          alphabetic and numeric data, it is alright if the column just
  3552.          contains digits.  The characters are left-justified.  The maximum
  3553.          column width is 80.  CHARACTER and CHAR can be used
  3554.          interchangeably.  If you omit the length modifier, then the
  3555.          length of the column is one.
  3556.          
  3557.          Date
  3558.            DATE
  3559.            The DATE data type creates a column of 8 characters.  Dates 
  3560.          are entered as mm/dd/yy.  
  3561.          
  3562.          Logical
  3563.            LOGICAL - A one character column that can store Y, y, N, n, 
  3564.          T, t, F, f, or ? 
  3565.  
  3566.          NOT NULL
  3567.          This modifier to a column definition will ensure that there is
  3568.          always a value for this column. It prevents the INSERT and UPDATE
  3569.          commands from allowing a column to contain a null value. When
  3570.          used with the UNIQUE modifier, it is used to specify a primary
  3571.          key.  There are special commands to retrieve rows that only have
  3572.          null values in a particular column or to exclude rows that have
  3573.          null values in a particular column.
  3574.          
  3575.          UNIQUE
  3576.          This ensures that each value in the column is unique.  If an
  3577.          attempt is made to enter a duplicate value when inserting a new
  3578.          row, an error results and the row is not added to the table.  If
  3579.          you use unique, it must be used with NOT NULL. This is typically
  3580.          used when a single column is a primary key. 
  3581.          
  3582.          Uniqueness_constraint (covered in full documentation)
  3583.          
  3584.          CHECKING THE STRUCTURE OF A TABLE
  3585.          In order to display the create command used to create the table,
  3586.          type STRUCT followed by the table name.
  3587.          
  3588.          EXERCISES (Answers in full documentation)
  3589.          The following are based on the law firm database as described in
  3590.          the chapter on the example databases.  
  3591.          1.  Create the SPCLTY table
  3592.          2.  Create the ATTORNEY table
  3593.          3.  Create the CLIENT table
  3594.          4.  Create the CASE table
  3595.          5.  Create the CASEWORK table
  3596.  
  3597.                                      CREATE-4
  3598.  
  3599.  
  3600.  
  3601.  
  3602.  
  3603.  
  3604.          CREATE A VIEW
  3605.  
  3606.          OVERVIEW
  3607.          A view is a derived table.  A view describes an alternative
  3608.          access to columns and tables that already exist.  The create
  3609.          table command allowed us to create a physical table that is also
  3610.          called a base table.  With a base table every column corresponds
  3611.          to the source of data.  We can always use the insert, update, and
  3612.          delete commands on base tables.  Views never exist as permanent
  3613.          tables.  Whenever you query a view, it creates a temporary table
  3614.          based on your view.  When the query is complete, the temporary
  3615.          table is erased.
  3616.          The view can reference an existing column under a different name
  3617.          or keep the original name. The view can be based on more than one
  3618.          table.  You can reference as few or as many columns you want in
  3619.          the tables you select.
  3620.          
  3621.          SYNTAX
  3622.          
  3623.          CREATE VIEW view_name [(column_name [,column_name...])] AS
  3624.          select_statement
  3625.          
  3626.          EXAMPLES:
  3627.          Create a view which only shows less than the full number of
  3628.          columns in a table.  In a commercial database system, this can be
  3629.          used to prevent users from seeing sensitive data.  For example,
  3630.          The employee table may include salary information.  A view of
  3631.          that table could have everything except the salary information.
  3632.          It is necessary to have some background in the use of the select
  3633.          command before attempting to create a view of a table.  The
  3634.          statement below shows the creation of a view called custa.   It
  3635.          is based on the cust table that has four columns:  code, name,
  3636.          st, and rating.
  3637.          
  3638.          create view custa as
  3639.          select code, name, st
  3640.          from cust;
  3641.          
  3642.          The column names are the same as they are in the original table. 
  3643.          The only difference is that rating is not part of the view.
  3644.          When you use view custa in a query, it acts just like a regular
  3645.          table.  When it displays all the columns as shown below, you only
  3646.          see the three columns specified in the view.  
  3647.          
  3648.          select * 
  3649.          from custa; 
  3650.          
  3651.          code name            st  
  3652.          ---- --------------- --  
  3653.          AA   Compugorp       WA  
  3654.          BB   Technoharps     OR  
  3655.          ZZ   Organomice      AZ  
  3656.          DD   QuarkCo         AZ  
  3657.          EE   Marswarp        CA  
  3658.          FF   Multicrud       NV  
  3659.          
  3660.                                      VIEW-1
  3661.  
  3662.  
  3663.  
  3664.  
  3665.  
  3666.          We have no access to the rating column when using the custa view.
  3667.          
  3668.          RULES FOR USING VIEWS
  3669.          
  3670.          It is used in a select statement, NOT an insert, update, or
  3671.          delete statement. To erase the view, use the drop view statement.
  3672.          
  3673.          Can contain any select statement except one that has redirectto. 
  3674.          
  3675.          Views should only be used when they are absolutely needed.  They
  3676.          can add much confusion to a database design because it is easy to
  3677.          forget which are tables and which are views.
  3678.          
  3679.          (MORE IN THE FULL DOCUMENTATION)
  3680.  
  3681.  
  3682.  
  3683.  
  3684.  
  3685.  
  3686.  
  3687.  
  3688.  
  3689.  
  3690.  
  3691.  
  3692.  
  3693.  
  3694.  
  3695.  
  3696.  
  3697.  
  3698.  
  3699.  
  3700.  
  3701.  
  3702.  
  3703.  
  3704.  
  3705.  
  3706.  
  3707.  
  3708.  
  3709.  
  3710.  
  3711.  
  3712.  
  3713.  
  3714.  
  3715.  
  3716.  
  3717.  
  3718.  
  3719.  
  3720.                                       VIEW-2
  3721.  
  3722.  
  3723.  
  3724.  
  3725.  
  3726.  
  3727.          DROP TABLE/VIEW
  3728.  
  3729.          OVERVIEW
  3730.          The drop command is used to erase a table or view from the disk.
  3731.          The drop table command is used to erase the description of the
  3732.          table from the disk.  The drop view command erases the view but
  3733.          can never alter data or tables since the view simply describes
  3734.          access to a table or tables.
  3735.          
  3736.          SYNTAX
  3737.          
  3738.          DROP TABLE table_name;
  3739.          or
  3740.          DROP VIEW view_name;
  3741.          
  3742.          EXAMPLES:
  3743.          
  3744.          drop table cust;
  3745.          drop view custa; 
  3746.          
  3747.          COMPONENTS OF THE DROP COMMAND
  3748.          
  3749.          table_name
  3750.          The table_name must be a valid table name which contains NO data. 
  3751.          In order to delete all the data you can first type:
  3752.          
  3753.          delete from table_name;
  3754.          
  3755.          The delete command erases the data (see chapter 10 for more
  3756.          details) and the drop table command erases the column
  3757.          descriptions.
  3758.          
  3759.          view_name
  3760.          The view_name must be a valid view name.  Since no data is
  3761.          affected it can be used at any time.
  3762.  
  3763.  
  3764.  
  3765.  
  3766.  
  3767.  
  3768.  
  3769.  
  3770.  
  3771.  
  3772.  
  3773.  
  3774.  
  3775.  
  3776.  
  3777.  
  3778.  
  3779.  
  3780.  
  3781.  
  3782.                                 DROP-1
  3783.  
  3784.  
  3785.  
  3786.  
  3787.  
  3788.  
  3789.           INSERT DATA INTO A TABLE
  3790.  
  3791.           OVERVIEW
  3792.           The ANSI standards define two ways to add data to a table. One
  3793.           requires us to type each value for each column in each row. The
  3794.           other copies data from one table and puts it in another. It is
  3795.           very useful for making corrections to tables. I only describe the
  3796.           first type in this documentation.  Registered users find out
  3797.           about the more advanced use of the insert command and a special,
  3798.           non-ANSI command which makes inserting data MUCH easier.
  3799.           
  3800.           Both insert commands maintain the restrictions we may have
  3801.           imposed through the CREATE TABLE command.  We could have
  3802.           specified NOT NULL and UNIQUE to prevent certain types of data
  3803.           errors in our tables.
  3804.           
  3805.           INSERT VALUES - SYNTAX
  3806.           
  3807.           INSERT INTO table_name [column_name [,column_name...]]
  3808.           VALUES (column_value [,column_value]);
  3809.           
  3810.           EXAMPLES:
  3811.           
  3812.           The first example shows the typical way to use the insert command
  3813.           since it does not reference any column names.  The insert command
  3814.           assumes that you will enter the data in the order that they were
  3815.           created in the table.  The second example shows that column names
  3816.           can be referenced. The third example shows that the data entry
  3817.           does not have to be in the order in which the columns were
  3818.           created.  Compare the order of the values to the first example.
  3819.           insert into cust values('AA','Compugorp','WA',20);
  3820.           or
  3821.           insert into cust code, name, st, rating
  3822.           values('AA','Compugorp','WA',20);
  3823.           or
  3824.           insert into cust name, code, rating, st 
  3825.           values('Compugorp','AA',20,'WA');
  3826.           
  3827.           MORE DETAIL ON THE COMPONENTS OF THE SYNTAX
  3828.           
  3829.           table_name
  3830.           
  3831.           Any existing table in your database.
  3832.           
  3833.           column_name
  3834.           
  3835.           Normally column names are not used with the insert command.  This
  3836.           is because the insert command assumes that we are going to insert
  3837.           values for all the columns in the table in the order in which
  3838.           they were created. You saw this in the first example.  It has the
  3839.           same effect as the second example.  In the third example we want
  3840.           to insert data in a different order.  This could be useful if the
  3841.           source document that we are getting the data from presents the
  3842.           data in a different order from which the table was designed.  
  3843.  
  3844.  
  3845.  
  3846.                                    INSERT-1
  3847.  
  3848.  
  3849.  
  3850.  
  3851.  
  3852.           column_value
  3853.           There are three types of column values: character, numeric and
  3854.           null.
  3855.           
  3856.           Character data
  3857.           Character data must be enclosed in quotes or apostrophes. The
  3858.           first example above could have been entered as:
  3859.           
  3860.           insert into cust values("AA","Compugorp","WA",20);
  3861.           
  3862.           Although you can enter SQL commands in uppercase or lowercase,
  3863.           care must be taken when entering character data since everything
  3864.           is stored exactly as entered.  This can create problems when we
  3865.           are retrieving data.  Let's assume that Compugorp purchased some
  3866.           Megawamps so we insert the following information into the sales
  3867.           table: date, branch code, customer code, employee number, product
  3868.           code and quantity.
  3869.           
  3870.           insert into sales values('04/01/87','1A','aa',10,'MW',947);
  3871.           
  3872.           Although SQL would accept the above insert command, we made a
  3873.           critical logical error.  The correct customer code for Compugorp
  3874.           is 'AA', NOT 'aa'.  This is an error that is rather difficult to
  3875.           detect.  Any time we relate the sales table with the cust table,
  3876.           the above row in the sales table will not be retrieved.  This
  3877.           could cause reports to disagree.  The section on correlated
  3878.           subqueries explains how to detect this problem.  Technically, it
  3879.           is called referential integrity.
  3880.           Another mistake that is easily made involves accidentally having
  3881.           leading or trailing spaces in a character value.  If we inserted
  3882.           the data for Compugorp with the following:
  3883.           
  3884.           insert into cust values('AA','Compugorp ','WA',20);
  3885.           
  3886.           You will notice the space after the last 'p' in Compugorp.  There
  3887.           will not be a problem with it until you try a query that includes
  3888.           the name of the company as shown below.
  3889.           select *
  3890.           from cust 
  3891.           where name = 'Compugorp';
  3892.           
  3893.           The row with Compugorp will not be found because the query did
  3894.           not contain the space after the last p!  This is a very
  3895.           frustrating error as I have seen quite a few students accuse me
  3896.           of having a serious malfunction in my program with the look on
  3897.           their faces suggesting what I could do with SSQL.
  3898.           All columns have a particular length associated with them.  This
  3899.           denotes the maximum number of characters that the column can
  3900.           contain.  When we created the cust table, the name column was
  3901.           defined as char(15) which means that the maximum number of
  3902.           characters it can contain is 15.  If more than 15 characters are
  3903.           in the name, only the first 15 are saved.
  3904.  
  3905.  
  3906.  
  3907.                                    INSERT-2
  3908.  
  3909.  
  3910.  
  3911.  
  3912.           
  3913.           insert into cust values('GG','Bizzlesnarf & Sons','CA',10);
  3914.           
  3915.           Although the above command would be allowed, when we retrieve the
  3916.           customer name, only "Bizzlesnarf & " would be displayed.
  3917.           
  3918.           Numeric Data
  3919.           The basic rule to remember with numeric data is not to enclose
  3920.           them in quotes as we can see above with the entry for rating. 
  3921.           The length of the column as specified in the create table command
  3922.           must not be exceeded.  Rating was created as numeric(2) which
  3923.           means that the maximum number of digits is 2.  The following
  3924.           insert command would cause an error because the entry for rating
  3925.           contains 3 digits instead of 2:
  3926.           
  3927.           insert into cust values('AA','Compugorp','WA',123);
  3928.           
  3929.           Because of the error, none of the values would be added to the
  3930.           table.
  3931.           
  3932.           Null data
  3933.           I really should not write "null data" or "null value" because the
  3934.           concept of the null means absence of a value.  If we add a
  3935.           customer that has no rating we would do something like:
  3936.           
  3937.           insert into cust values('HH','MagnaMice','AZ',null);
  3938.           
  3939.           There are NO QUOTES enclosing the word null.  Like all other
  3940.           keywords in SQL, it can be in uppercase or lowercase letters. 
  3941.           The null is different from a 0 (zero) in that a null means that
  3942.           there is no rating.  A 0 (zero) means that there IS a rating and
  3943.           the rating is 0.  This idea is further developed when discussing
  3944.           data retrieval in the section on the select command.          
  3945.  
  3946.  
  3947.  
  3948.  
  3949.  
  3950.  
  3951.  
  3952.  
  3953.  
  3954.  
  3955.  
  3956.  
  3957.  
  3958.  
  3959.  
  3960.  
  3961.  
  3962.  
  3963.  
  3964.  
  3965.  
  3966.  
  3967.                                    INSERT-3
  3968.  
  3969.  
  3970.  
  3971.  
  3972.  
  3973.  
  3974.          UPDATE DATA IN A TABLE
  3975.          
  3976.          OVERVIEW
  3977.          The update command is used to change column values in existing
  3978.          rows based on criteria in a search_condition that is accomplished
  3979.          through a where clause. If there where any constraints put on the
  3980.          column values when the table was created such as not null or not
  3981.          null unique, the update command makes sure that the constraints
  3982.          are maintained before the columns are actually updated.  Next to
  3983.          the insert command, it is the most cumbersome to use.  To make
  3984.          updates a bit easier, I made a non-ANSI command which is fully
  3985.          explained in the full documentation.
  3986.          
  3987.          UPDATE SYNTAX
  3988.          
  3989.          UPDATE table_name
  3990.          SET column_name = value [, column_name = value...]
  3991.          WHERE search_condition
  3992.  
  3993.          The value can be a mathmatical expression.
  3994.  
  3995.          EXAMPLES:
  3996.          In the first example, the rating for customer AA is increased 
  3997.          by 5.
  3998.          
  3999.          update cust
  4000.          set rating = rating + 5
  4001.          where code = 'AA';
  4002.          
  4003.          In the example below, the rating for customer CC is changed to a
  4004.          null.
  4005.          
  4006.          update cust
  4007.          set rating = null
  4008.          where code = 'CC';
  4009.          
  4010.          
  4011.          The example below shows how to change the percent defects to 10
  4012.          for a batch of product GC that occurred on July 21, 1987 in
  4013.          Idaho.
  4014.          
  4015.          update manu
  4016.          set defects = 10
  4017.          where date = '07/21/87' and code = 'GC' and mst = 'ID';
  4018.          
  4019.          In the following, two columns are changed.  For customer BB,
  4020.          rating is changed to 14 and the state column is changed to Idaho.
  4021.          
  4022.          update cust
  4023.          set rating = 14, st = 'ID'
  4024.          where code = 'BB';
  4025.          
  4026.  
  4027.                                       UPDATE-1
  4028.  
  4029.  
  4030.  
  4031.  
  4032.  
  4033.          The example below is a bit more involved.  We want to change the
  4034.          rating of branch 2A customers who have purchased above average
  4035.          quantities (overall) to 30. The use of the select command in a
  4036.          where clause is covered in a later section.  
  4037.  
  4038.          update cust
  4039.          set rating = 30 
  4040.          where code in 
  4041.           (select distinct code 
  4042.            from cust, sales
  4043.            where cc=code  
  4044.              and bc='2A'  
  4045.              and qty >
  4046.             (select avg(qty) 
  4047.              from sales)); 
  4048.          
  4049.          COMPONENTS OF UPDATE
  4050.          
  4051.          table_name
  4052.          Any existing table in your database.
  4053.          
  4054.          column_name
  4055.          Any valid column name in the table name accessed, including the
  4056.          column(s) which form the key. 
  4057.           
  4058.          value
  4059.          The value is anything appropriate for the data type of the
  4060.          column.  Any value that would work with the insert command will
  4061.          work with the update command.  As with the insert command, the
  4062.          word null is valid to show that there is no value for the column. 
  4063.          Remember to put quotes around character values.
  4064.          
  4065.          
  4066.          search_condition
  4067.          The section on the select command has more detail on the
  4068.          search_condition that is accomplished with the use of a where
  4069.          clause.  Typically, you want to update a single row.  To update a
  4070.          single row the search_condition must test for the primary key so
  4071.          that will be explained here.  The primary key allows you to
  4072.          uniquely define each row.  The cust table, which is used in most
  4073.          of the examples above, has a primary key based on code.  That is,
  4074.          based on the value in code, we know only a single row will be
  4075.          updated.  
  4076.          
  4077.          Compound keys are discussed in the full documentation.
  4078.  
  4079.  
  4080.  
  4081.  
  4082.  
  4083.  
  4084.  
  4085.                                 UPDATE-2
  4086.  
  4087.  
  4088.  
  4089.  
  4090.  
  4091.  
  4092.          DELETE ROWS FROM A TABLE
  4093.          
  4094.          OVERVIEW
  4095.          The delete command deletes rows based on the criteria in the
  4096.          where clause.  Without a where clause, all the data in the table
  4097.          is deleted.
  4098.          Refer to the section on the select statement for details on the
  4099.          where clause.  The basic difference between the select * and the
  4100.          delete is that with select, the rows are displayed, and with
  4101.          delete, the rows are deleted.  Because of this, it is a smart
  4102.          idea to use the select before you use the delete so you can see
  4103.          the rows that you are going to delete.
  4104.          You can only delete complete rows.  If you want to delete column
  4105.          data within a row, use the update command.
  4106.          
  4107.          SYNTAX
  4108.          
  4109.          DELETE FROM table_name
  4110.          [WHERE search_expression]
  4111.          
  4112.          EXAMPLES:
  4113.          
  4114.          The following shows how to delete all the data from the cust
  4115.          table.
  4116.          
  4117.          delete from cust;
  4118.          
  4119.          The following shows how to delete customers from the cust table
  4120.          whose rating is less than 10.
  4121.          
  4122.          delete from cust
  4123.          where rating < 10;
  4124.          
  4125.          COMPONENTS OF DELETE
  4126.          
  4127.          table_name
  4128.          Any existing table in your database.
  4129.          
  4130.          search_condition
  4131.          The section on the select statement has more detail on the
  4132.          search_condition that is accomplished with a where clause. A
  4133.          simple example is shown above. The command to delete customers
  4134.          whose rating is less than 10 could create some problems if there
  4135.          were customers in the sales table with customer codes that you
  4136.          deleted.  Whenever you delete a row that contains a primary key
  4137.          that is used in another table, problems can arise.  In order to
  4138.          determine whether there are any of those customers in the sales
  4139.          table, you could use the select command as shown below.
  4140.          
  4141.          select *
  4142.          from   sales
  4143.          where  cc in (
  4144.            select code
  4145.            from   cust
  4146.            where  rating < 10);
  4147.  
  4148.                                      DELETE-1
  4149.  
  4150.  
  4151.  
  4152.  
  4153.          
  4154.          To delete all the corresponding rows in the sales table for
  4155.          customers whose rating is less than 10, you would simply take the
  4156.          statement above and replace the "select * from" with "delete" as
  4157.          shown below.
  4158.          
  4159.          delete from sales
  4160.          where  cc in (
  4161.            select code
  4162.            from   cust
  4163.            where  rating < 10);
  4164.          
  4165.          The delete command below shows how to delete all rows in the
  4166.          sales table that do not have a corresponding customer code in the
  4167.          cust table. The not exists is covered in the full documentation.
  4168.          
  4169.          delete from sales
  4170.          where not exists
  4171.              (select *
  4172.               from cust
  4173.               where code = cc);
  4174.          
  4175.          HOW TO RESTORE DATA YOU HAVE JUST DELETED
  4176.          
  4177.          Type it in again!!! (Therefore be very careful when you use this
  4178.          command).
  4179.          
  4180.          EXERCISES (Answers in the full documentation)
  4181.          
  4182.          What are the commands to delete:
  4183.          
  4184.          1.  The attorneys with over 15 years of service.
  4185.          
  4186.          2.  The category 2 clients from Florida.
  4187.          
  4188.          3.  Client c2.
  4189.  
  4190.  
  4191.  
  4192.  
  4193.  
  4194.  
  4195.  
  4196.  
  4197.  
  4198.  
  4199.  
  4200.  
  4201.  
  4202.  
  4203.  
  4204.  
  4205.  
  4206.  
  4207.  
  4208.                                 DELETE-2
  4209.  
  4210.  
  4211.  
  4212.  
  4213.  
  4214.  
  4215.          TUTORIAL
  4216.          
  4217.          OVERVIEW
  4218.          This tutorial will take you through the basics creating a table,
  4219.          inserting data into a table, updating rows in a table, deleting
  4220.          rows from a table, retrieving data from a table, creating a view
  4221.          of a table and dropping a table.
  4222.          
  4223.          START SSQL
  4224.          From the DOS prompt type SSQL and press ENTER:
  4225.          
  4226.          CREATE THE TABLE
  4227.          Before we can put any data into the table, you need to describe
  4228.          the type(s) of data that it is going to contain.  Often the table
  4229.          we create is referred to as a base table.  Base tables contain
  4230.          actual data which is distinguished from a derived table (see
  4231.          Create a View). It is assumed that you have already read the
  4232.          introduction which contains the basic definitions of columns,
  4233.          rows, and tables. You will create a shortened version of a
  4234.          payroll table.  For each employee you will store the last name,
  4235.          the first name, salary, and city.  The key will be
  4236.          a combination of last name and first name. Type the following:
  4237.          
  4238.          create table pay (
  4239.          last char(15) not null,
  4240.          first char(15) not null,
  4241.          salary numeric(5),
  4242.          city char(12),
  4243.          unique(last, first)
  4244.          );
  4245.          
  4246.          Last name and first name can each be up to fifteen characters in
  4247.          length.  Since they are part of the primary key, nulls will not
  4248.          be allowed in the columns.  The combination of the column names
  4249.          last and first are set to unique so there can be no duplicate
  4250.          primary keys.  The values for city are limited to twelve
  4251.          characters.  Salary is limited to five digits which means up to
  4252.          $99,999.
  4253.          If you made a mistake, skip to the last part of the tutorial to 
  4254.          find out how to drop a table which erases it from the disk so 
  4255.          you can start over.
  4256.          
  4257.          INSERT DATA INTO THE TABLE
  4258.          The following has has the commands to insert four rows of data 
  4259.          into the table. When you type them, be careful to include all the
  4260.          appropriate quotes and commas.  And yes, include the in correct
  4261.          spelling of Scottsdale.  You will correct it in the next section.
  4262.          Note that Hippity Hopper has a null in place of his
  4263.          salary.  This means that the salary is not available which is
  4264.          different from putting a salary of zero.
  4265.          
  4266.          insert into pay values('Everski','Willy',45000,'Scottsdale');
  4267.          insert into pay values('Everski','Wilshe',60000,'Scootsdale');
  4268.          insert into pay values('Hopper','Hippity',null,'Phoenix');
  4269.          insert into pay values('Nosebleed','Harvey',20000,'Peoria');
  4270.  
  4271.  
  4272.                                 TUTORIAL-1
  4273.  
  4274.  
  4275.  
  4276.  
  4277.          If you made any mistakes, you should be able to correct them
  4278.          after going through the next section on updates.
  4279.          
  4280.          UPDATE DATA IN A TABLE
  4281.          You need to make two changes to the table.  Scootsdale needs to
  4282.          be changed to Scottsdale and the salary for Willy Everski needs
  4283.          to be changed to 55,000.  The where clause is needed to access 
  4284.          the specific row you need.
  4285.          
  4286.          update pay
  4287.          set city = 'Scottsdale'
  4288.          where city = 'Scootsdale';
  4289.          
  4290.          update pay
  4291.          set salary = 55000
  4292.          where last = 'Everski' and first = 'Willy';
  4293.          
  4294.          
  4295.          DELETE A ROW FROM A TABLE
  4296.          Harvey Nosebleed has been fired so you want to delete the row
  4297.          that contains the data for Harv.  Figure D-5 shows you how.  In a
  4298.          manner similar to the update, the where clause describes which
  4299.          row should be deleted.
  4300.          The where clause only includes the last name
  4301.          because I know that all the other Nosebleeds have been fired. 
  4302.          However, the primary key is both last and first names.  With the
  4303.          Everskis you had to include both last and first names otherwise
  4304.          both would have been updated.  OK, technically we could have
  4305.          gotten by with just the first names but I know you realize that
  4306.          in a real-world scenario that would not be acceptable.
  4307.          
  4308.          delete from pay
  4309.          where  last='Nosebleed';
  4310.          
  4311.          RETRIEVE DATA FROM A TABLE
  4312.          The select command is used to retrieve data from a table.  The
  4313.          following select command retrieves all the data in the table. 
  4314.          
  4315.          select *
  4316.          from pay;
  4317.          
  4318.          last          first         salary city         
  4319.          ------------- ------------- ------ ------------ 
  4320.          Everski       Willy         55000 Scottsdale
  4321.          Everski       Wilshe        60000 Scottsdale
  4322.          Hopper        Hippity             Phoenix   
  4323.          
  4324.          3 rows selected
  4325.  
  4326.  
  4327.  
  4328.  
  4329.  
  4330.  
  4331.                                 TUTORIAL-2
  4332.  
  4333.  
  4334.  
  4335.  
  4336.  
  4337.          The following shows how to retrieve the first name and last name for
  4338.          all the rows.
  4339.          
  4340.          select first, last
  4341.          from pay;
  4342.          
  4343.          first           last            
  4344.          --------------- --------------- 
  4345.          Willy           Everski         
  4346.          Wilshe          Everski         
  4347.          Hippity         Hopper          
  4348.          
  4349.          3 rows selected
  4350.          
  4351.          The following displays the rows of employees which
  4352.          have salaries of less than $58,000.  Notice that the row for
  4353.          Hippity Hopper is not displayed because all nulls are excluded. 
  4354.          
  4355.          select *
  4356.          from pay
  4357.          where salary > 58000;
  4358.          
  4359.          last          first         salary city         
  4360.          ------------- ------------- ------ ------------ 
  4361.          Everski       Willy          55000 Scottsdale   
  4362.          
  4363.          1 row selected
  4364.          
  4365.          CREATE A VIEW
  4366.          A view is an alternate way of looking at a table or even a
  4367.          combination of tables.  It is also known as a derived table
  4368.          because a view is derived from one or more base tables. We will
  4369.          create a view which excludes salary information.  The view is
  4370.          based on a select statement. The view accesses data in the base
  4371.          table every time it is used in a select statement.  There is
  4372.          never any data contained in the view.  The following creates the
  4373.          view.  
  4374.          
  4375.          create view pay1 as
  4376.          select last, first, city
  4377.          from pay;
  4378.          
  4379.          Now let's see what happens when all the columns of the view are 
  4380.          selected.
  4381.          
  4382.          select *
  4383.          from pay1;
  4384.          
  4385.          last            first           city
  4386.          --------------- --------------- ------------
  4387.          Everski         Willy           Scottsdale
  4388.          Everski         Wilshe          Scottsdale
  4389.          Hopper          Hippity         Phoenix
  4390.          
  4391.          3 rows selected
  4392.  
  4393.  
  4394.                                 TUTORIAL-3
  4395.  
  4396.  
  4397.  
  4398.  
  4399.  
  4400.          DROP A TABLE
  4401.          When a table is dropped, the description of the table is erased
  4402.          from the disk.  The drop command will only work on an empty
  4403.          table.  The delete command is needed to delete all the data in 
  4404.          the table then the drop command erases the table from the disk.  
  4405.          
  4406.          delete from pay;
  4407.          drop  pay;
  4408.          
  4409.          EXIT SSQL
  4410.          This is a tough one - type EXIT and press ENTER
  4411.  
  4412.  
  4413.  
  4414.  
  4415.  
  4416.  
  4417.  
  4418.  
  4419.  
  4420.  
  4421.  
  4422.  
  4423.  
  4424.  
  4425.  
  4426.  
  4427.  
  4428.  
  4429.  
  4430.  
  4431.  
  4432.  
  4433.  
  4434.  
  4435.  
  4436.  
  4437.  
  4438.  
  4439.  
  4440.  
  4441.  
  4442.  
  4443.  
  4444.  
  4445.  
  4446.  
  4447.  
  4448.  
  4449.  
  4450.  
  4451.  
  4452.  
  4453.  
  4454.  
  4455.                                 TUTORIAL-4
  4456.  
  4457.  
  4458.