First, you need to remember what you did with Project 3 when you used the Proj3data.mdb Access database. The data table within the "mdb" file is the "Orders" table (what we call a file). The field names in the Orders table are: DATE, STATE, CHANNEL, PRICE, CATEGORY, UNITS, NETAMT
SELECT STATE,CATEGORY,UNITS,NETAMT
FROM ORDERS;
The SELECT command specifies the fields within the ORDERS table to use.
The FROM parameter specifies that the ORDERS table be used as the data source.
Note that the SQL command ends with a semi-colon. It won't hurt if you forget it.
That's it! Access will return a table of data with these fields. Try it in Access.
For text data, the query search argument is within single quotes. The state of Washington is represented in the data as WA, so the argument is 'WA'. Numeric data query search arguments are not put in single quotes. For example, WHERE NETAMT > 500 will return all records with a NetAmt of more than $500.
The WHERE parameter uses the logical operators AND, OR, and NOT. For example, if you want data from both Washington and Arizona, the WHERE parameter is: WHERE STATE = 'WA' OR STATE = 'AZ' The variable STATE must be used for each search argument.
You must be careful when using logical operators. "OR" means "either one or the other condition is true", while "AND" means "both conditions must be true." This is incorrect: WHERE STATE = 'WA' AND STATE = 'AZ' There is no record in the data where the STATE is both "WA" and "AZ" (Taking a course in Logic is always a good time investment.)
The WHERE parameter can also use mathematical operators. For example, WHERE NETAMT > 10000 specifies that only records with NET AMT amounts greater than 10000 be included. Note that numeric search arguments are not enclosed within double quotes.
This table illustrates the logical operators that you can use with the WHERE parameter:
| AND | OR | NOT |
| = (equal) | <> (not equal) | < (less than) |
| <= (less than or equal to) | > (greater than) | >= (greater than or equal to) |
Combinations of logical operators must be grouped within parentheses in order for them to work. For example, if you want data from Oregon or Nevada or California and you want data only for NetAmt greater than or equal to $500, then the "State" WHERE must be all within parentheses and the "Net Amt" WHERE must also be within parentheses. Look at this example.
WHERE (STATE = 'OR' OR STATE = 'NV' OR STATE = 'CA') AND (NETAMT >= 500)
Note that parentheses around the "State" condition and around the "NetAmt" condition are required to logically group them. Note also that the $500 amount doesn't have to be within double quotes since it is an amount. Note also that the "OR" refers to the abbreviation for Oregon, and has nothing to do with the OR logical operator. The 'OR' within single quotes indicates a literal value to use in the WHERE parameter, while the OR not within single quotes is the logical OR operator.
SELECT STATE,CATEGORY,UNITS,NETAMT
FROM ORDERS
WHERE (STATE = 'OR' OR STATE = 'NV' OR STATE = 'CA') AND (NETAMT >= 500)
ORDER BY STATE;
That's basically it! There are lots and lots of SQL parameters, but these will get the job done for the vast majority of data queries that "normal" people construct.