This chapter introduces the student to the the join command, and introduces several new functions. The objectives important to this chapter are:
Concepts:Retreiving data from multiple tables also begins with the SELECT command. A query on mulitple tables may come in three parts: SELECT column list
The WHERE clause must contain a statement of the table relationship, but it may also contain restrictions. If it does, the statement of relationship is followed by AND, which is followed by your list of restrictions. Format: SELECT column list The IN operator can be used to match against any of a set of values, as shown in chapter 3. The text shows an example that is a variation of the above syntax. It is not as clear, but it does about the same thing. The difference is that we are running a nested query. Format: SELECT column list for second table This query can be done with the EXISTS operator. Format: SELECT column list for second table This runs the inner query on both tables, looking for rows that meet the condition in the final line. The outer query then runs, selecting the columns desired from the second table, retrieving only the rows that match the output of the first query. You can run a subquery in a subquery, as an extension of the format already shown for one nested query. Note that the innermost query runs first, then the query that it was inside, and finally the outermost query. This follows the standard computer principle of evaluating the expressions inside parentheses first. When you have nested parentheses, the innermost phrases evaluate first, followed by the next innermost, working our way to the outside. An alias can be used to serve as shorthand notation for the name of a table. The syntax is odd, in that we can use the alias before we define it in the command. Example: SELECT CustomerNum, CustomerName, R.RepNum In this example, C was made an alias for Customer, and R was made an alias for Rep in the FROM clause. The aliases can be used in the SELECT clause and in the WHERE clause. Note that the alias stands for the table, not the series of letters in the table name. This comparison operator in this example has a field name on its left, as is typical, but the comparison value on its right is also a field name. This is unlike most comparisons. Usually, the comparison value is a value, enclosed in what some people call single quotes. Other people call these apostrophes. The purpose of the quotes is twofold: to show that the value is not the name of a field, and to enclose the exact value to compare. Compound conditions are just two or more conditions linked by logical operators: AND, OR, and NOT. These are also called Boolean operators. One way to use the NOT operator is to compose a comparison, and to precede it with NOT. Example: SELECT CustomerNum, CustomerName In this example, the query is looking for the customers whose balances are not equal to 3000. It is not required to put the condition in parentheses, but it helps make the meaning of the operation clear. Computed columns are created by using math operators with the names of fields. The standard math operators are used: + for addition, - for subtraction, * for multiplication, and / for division. The example in the text suggests that we create a column that computes the available balance for a customer by subtracting their balance from their credit limit. SELECT CustomerNum, CustomerName, (CreditLimit - Balance) AS AvailableCredit This command would create a computed column, and display the results under the heading "AvailableCredit". A useful operator for times when you are searching for a partial match is the LIKE operator. When searching for a character string that occurs somewhere in a field, use the LIKE operator with a comparison value enclosed in percent signs. Example: SELECT CustomerNum, CustomerName This command would retrieve the selected fields from all records having Sport anywhere in their names. The % character placed like this will match any number of characters before or after the comparison value. Placing it only before or only after the comparison value would match any characters at that position. (In some systems, you will use the * instead of the %.) Sorting is accomplished with the ORDER BY clause. ORDER BY is followed by the names of the fields on which to sort the output. If sorting on more than one field, the first listed is the primary key, the second is the secondary key, and so on. Example: SELECT CustomerNum, CustomerName, CreditLimit A sort will display results sorted in ascending order, unless the DESC switch is used. This switch follows the name of field to be sorted in descending order. Example: SELECT CustomerNum, CustomerName, CreditLimit A list of functions common to spreadsheets is offered, called aggregate
functions. They give us the ability to provide a summary report from
a query, rather than a detail report.
These functions can be combined with WHERE criteria, in order to apply them to subsets of the data. Example: SELECT COUNT(*) FROM Customer WHERE CustomerName LIKE ('%Sport%'); This query would reply with the number 2, which is the number of records matching the criteria. MySQL works with this syntax, and with the alternate syntax in the text, replacing the * with the name of any field. It is possible to run a query on the results of another query by nesting the first within the second. Figure out what you want to do for each query. The first one you need to run must be enclosed in parentheses, it must follow the IN operator, and that operator comes at the end of the second query to run. Example (colors are added for reference): SELECT OrderNum FROM OrderLine In this example the blue query runs first, and results in a list of part numbers that fall in the AP class. The red query then runs, matching records in the OrderLine table against the list of part numbers from the first query. I have made the semi-colon red because the command does not end until the second query runs. The text continues with an example of linking multiple tables in a SELECT command. Although lengthy, the examples show the same syntax as other examples. SELECT column list The idea is to link the tables in the WHERE clause, continuing with as many linking statements as there are relationships between the tables you are using. Set operations relate to math concepts. Three set operations are discussed.
Set operations can only be carried out on tables with the same columns. One way to make these commands available to you is to use queries to draw out the required columns from as many tables as needed, then use the set operator on the results. The general syntax is: query set_operator query ; The ANY and ALL operators are used to match a query against
the results of another query. The inner query produces a set of data,
and the outer query matches against the result. If the match is like this: Three more types of queries are defined in the text.
|