Subqueries and Query Expressions

The SQL subquery feature makes it easier to express certain types of queries in SQL; indeed, some queries can’t be expressed at all without using a subquery. Keep reading to learn more abou this useful feature. This article is excerpted from chapter 9 of MySQL The Complete Reference, written by Vikram Vaswani and published by McGraw Hill.


The SQL subquery feature lets you use the results of one query as part of another query. The ability to use a query within a query was the original reason for the word “structured” in the name Structured Query Language. The subquery feature is less well known than SQL’s join feature, but it plays an important role in SQL for three reasons:

  • A SQL statement with a subquery is often the most natural way to express a query, because it most closely parallels the English-language description of the query.

  • Subqueries make it easier to write SELECT statements, because they let you break a query down into pieces (the query and its subqueries) and then put the pieces back together.

  • Some queries cannot be expressed in the SQL language without using a subquery.

The first several sections of this chapter describe subqueries and show how they are used in the WHERE and HAVING clauses of a SQL statement. The later sections of this chapter describe the advanced query expression capabilities that have been added to the SQL2 standard, which substantially expands the power of SQL to perform even the most complex of database operations.

Using Subqueries

A subquery is a query within a query. The results of the subquery are used by the DBMS to determine the results of the higher-level query that contains the subquery. In the simplest forms of a subquery, the subquery appears within the WHERE or HAVING clause of another SQL statement. Subqueries provide an efficient, natural way to handle query requests that are themselves expressed in terms of the results of other queries. Here is an example of such a request:

List the offices where the sales target for the office exceeds the sum of the individual salespeople’s quotas.

The request asks for a list of offices from the OFFICES table, where the value of the TARGET column meets some condition. It seems reasonable that the SELECT statement that expresses the query should look something like this:

SELECT CITY FROM OFFICES WHERE TARGET > ???

The value “???” needs to be filled in and should be equal to the sum of the quotas of the salespeople assigned to the office in question. How can you specify that value in the query? From Chapter 8, you know that the sum of the quotas for a specific office (say, office number 21) can be obtained with this query:

SELECT SUM (QUOTA)
FROM SALESREPS
WHERE REP_OFFICE = 21

But it would be inefficient to have to type in this query, write down the results, and then type in the previous query with the correct amount. How can you put the results of this query into the earlier query in place of the question marks? It would seem reasonable to start with the first query and replace the “???” with the second query, as follows:

SELECT CITY
FROM OFFICES
WHERE TARGET > (SELECT SUM(QUOTA)
                         FROM SALESREPS
                         WHERE REP_OFFICE = OFFICE)

In fact, this is a correctly formed SQL query. For each office, the inner query (the subquery) calculates the sum of the quotas for the salespeople working in that office. The outer query (the main query) compares the office’s target to the calculated total and decides whether to add the office to the main query results. Working together, the main query and the subquery express the original request and retrieve the requested data from the database.

SQL subqueries typically appear as part of the WHERE clause or the HAVING clause. In the WHERE clause, they help to select the individual rows that appear in the query results. In the HAVING clause, they help to select the row groups that appear in the query results.

This article is excerpted from MySQL The Complete Reference by Vikram Vaswani (McGraw-Hill, 2003; ISBN 0072224770). Check it out at your favorite bookstore today. Buy this book now.

{mospagebreak title=What Is a Subquery?} 

Figure 9-1 shows the form of a SQL subquery. The subquery is enclosed in parentheses, but otherwise it has the familiar form of a SELECT statement, with a FROM clause and optional WHERE, GROUP BY, and HAVING clauses. The form of these clauses in a subquery is identical to that in a SELECT statement, and they perform their normal functions when used within a subquery. There are, however, a few differences between a subquery and an actual SELECT statement:

  • In the most common uses, a subquery must produce a single column of data as its query results. This means that a subquery almost always has a single select item in its SELECT clause.

  • The ORDER BY clause cannot be specified in a subquery. The subquery results are used internally by the main query and are never visible to the user, so it makes little sense to sort them anyway.


Figure 9-1.  Basic subquery syntax diagram    

Column names appearing in a subquery may refer to columns of tables in the main query. These outer references are described in detail later in the “Outer References” section.

  • In most implementations, a subquery cannot be the UNION of several different SELECT statements; only a single SELECT is allowed. (The SQL2 standard allows much more powerful query expressions and relaxes this restriction, as described later in the section “Advanced Queries in SQL2.”)

Subqueries in the WHERE Clause 

Subqueries are most frequently used in the WHERE clause of a SQL statement. When a subquery appears in the WHERE clause, it works as part of the row selection process. The very simplest subqueries appear within a search condition and produce a value that is used to test the search condition. Here is an example of a simple subquery:

List the salespeople whose quota is less than 10 percent of the companywide sales target.

SELECT NAME
FROM SALESREPS
WHERE QUOTA < (.1 * (SELECT SUM(TARGET) FROM OFFICES))

NAME

Bob Smith

In this case, the subquery calculates the sum of the sales targets for all of the offices to determine the companywide target, which is multiplied by 10 percent to determine the cutoff sales quota for the query. That value is then used in the search condition to check each row of the SALESREPS table and find the requested names. In this simple case, the subquery produces the same value for every row of the SALESREPS table; the QUOTA value for each salesperson is compared to the same companywide number. In fact, you could carry out this query by first performing the subquery, to calculate the cutoff quota amount ($275,000 in the sample database), and then carry out the main query using this number in a simple WHERE clause:

WHERE QUOTA < 275000

It’s more convenient to use the subquery, but it’s not essential. Usually, subqueries are not this simple. For example, consider once again the query from the previous section:

List the offices where the sales target for the office exceeds the sum of the individual salespeople’s quotas.

 

SELECT CITY

FROM OFFICES

WHERE TARGET > (SELECT SUM(QUOTA)

                FROM SALESREPS

                WHERE REP_OFFICE = OFFICE)

CITY

Chicago

 Los Angeles

In this (more typical) case, the subquery cannot be calculated once for the entire query. The subquery produces a different value for each office, based on the quotas of the salespeople in that particular office. Figure 9-2 shows conceptually how SQL carries out the query. The main query draws its data from the OFFICES table, and the WHERE clause selects which offices will be included in the query results. SQL goes through the rows of the OFFICES table one by one, applying the test stated in the WHERE clause.


Figure 9-2.  Subquery operation in the WHERE clause

The WHERE clause compares the value of the TARGET column in the current row to the value produced by the subquery. To test the TARGET value, SQL carries out the subquery, finding the sum of the quotas for salespeople in the current office. The subquery produces a single number, and the WHERE clause compares the number to the TARGET value, selecting or rejecting the current office based on the comparison. As the figure shows, SQL carries out the subquery repeatedly, once for each row tested by the WHERE clause of the main query.

This article is excerpted from MySQL The Complete Reference by Vikram Vaswani (McGraw-Hill, 2003; ISBN 0072224770). Check it out at your favorite bookstore today. Buy this book now.

{mospagebreak title=Outer References}

Within the body of a subquery, it’s often necessary to refer to the value of a column in the current row of the main query. Consider once again the query from the previous sections:


Figure 9-3.
Subquery comparison test syntax diagram

List the offices where the sales target for the office exceeds the sum of the individual salespeople’s quotas.

SELECT CITY
FROM OFFICES
WHERE TARGET > (SELECT SUM(QUOTA)
                                 FROM SALESREPS
                                 WHERE REP_OFFICE = OFFICE)

The role of the subquery in this SELECT statement is to calculate the total quota

for those salespeople who work in a particular office—specifically, the office currently being tested by the WHERE clause of the main query. The subquery does this by scanning the SALESREPS table. But notice that the OFFICE column in the WHERE clause of the subquery doesn’t refer to a column of the SALESREPS table; it refers to a column of the OFFICES table, which is a part of the main query. As SQL moves through each row of the OFFICES table, it uses the OFFICE value from the current row when it carries out the subquery.

The OFFICE column in this subquery is an example of an outer reference, which is a column name that does not refer to any of the tables named in the FROM clause of the subquery in which the column name appears. Instead, the column name refers to a column of a table specified in the FROM clause of the main query. As the previous example shows, when the DBMS examines the search condition in the subquery, the value of the column in an outer reference is taken from the row currently being tested by the main query.

Subquery Search Conditions

A subquery usually appears as part of a search condition in the WHERE or HAVING clause. Chapter 6 described the simple search conditions that can be used in these clauses. In addition, most SQL products offer these subquery search conditions:

  • Subquery comparison test. Compares the value of an expression to a single value produced by a subquery. This test resembles the simple comparison test.

  • Subquery set membership test. Checks whether the value of an expression matches one of the set of values produced by a subquery. This test resembles the simple set membership test.

  • Existence test. Tests whether a subquery produces any rows of query results.

  • Quantified comparison test. Compares the value of an expression to each of the set of values produced by a subquery.

The Subquery Comparison Test (=, <>, <, <=, >, >=)

The subquery comparison test is a modified form of the simple comparison test, as shown in Figure 9-3. It compares the value of an expression to the value produced by a subquery and returns a TRUE result if the comparison is true. You use this test to compare a value from the row being tested to a single value produced by a subquery, as in this example:  

List the salespeople whose quotas are equal to or higher than the target of the Atlanta sales office.

SELECT NAME
FROM SALESREPS
WHERE QUOTA >=    (SELECT TARGET
                                     FROM OFFICES
                                     WHERE CITY = ‘Atlanta’)

NAME

Bill Adams
Sue Smith
Larry Fitch

The subquery in the example retrieves the sales target of the Atlanta office. The value is then used to select the salespeople whose quotas are higher than the retrieved target.

The subquery comparison test offers the same six comparison operators (=, <>, <, <=, >, >=) available with the simple comparison test. The subquery specified in this test must produce a single value of the appropriate data type—that is, it must produce a single row of query results containing exactly one column. If the subquery produces multiple rows or multiple columns, the comparison does not make sense, and SQL reports an error condition. If the subquery produces no rows or produces a NULL value, the comparison test returns NULL (unknown).

Here are some additional examples of subquery comparison tests:

List all customers served by Bill Adams.

 SELECT COMPANY 
 FROM CUSTOMERS 
 WHERE CUST_REP =   (SELECT EMPL_NUM
                                         FROM SALESREPS
                                         WHERE NAME = ‘Bill Adams’)

COMPANY

Acme Mfg.
Three-Way Lines

List all products from manufacturer ACI where the quantity on hand is above the quantity on hand of product ACI-41004.

SELECT DESCRIPTION, QTY_ON_HAND
FROM PRODUCTS
WHERE MFR_ID = ‘ACI’
AND QTY_ON_HAND >    (SELECT QTY_ON_HAND
                                            FROM PRODUCTS
                                           WHERE MFR_ID = ‘ACI’
                                           AND PRODUCT_ID = ’41004′)

DESCRIPTION QTY_ON_HAND

Size 3 Widget 207
Size 1 Widget 277
Size 2 Widget 167

The subquery comparison test specified by the SQL1 standard and supported by all of the leading DBMS products allows a subquery only on the right side of the comparison operator. This comparison:

A < (subquery)

is allowed, but this comparison:

(subquery) > A

is not permitted. This doesn’t limit the power of the comparison test, because the operator in any unequal comparison can always be turned around so that the subquery is put on the right side of the inequality. However, it does mean that you must sometimes turn around the logic of an English-language request to get a form of the request that corresponds to a legal SQL statement.

The SQL2 standard eliminated this restriction and allows the subquery to appear on either side of the comparison operator. In fact, the SQL2 standard goes considerably further and allows a comparison test to be applied to an entire row of values instead of a single value. This and other more advanced query expression features of the SQL2 standard are described in the latter sections of this chapter. However, they are not uniformly supported by the current versions of the major SQL products. For portability, it’s best to write subqueries that conform to the SQL1 restrictions, as described previously.

This article is excerpted from MySQL The Complete Reference by Vikram Vaswani (McGraw-Hill, 2003; ISBN 0072224770). Check it out at your favorite bookstore today. Buy this book now.

{mospagebreak title=The Set Membership Test (IN)}

The subquery set membership test (IN) is a modified form of the simple set membership test, as shown in Figure 9-4. It compares a single data value to a column of data values produced by a subquery and returns a TRUE result if the data value matches one of the values in the column. You use this test when you need to compare a value from the row being tested to a set of values produced by a subquery. Here is a simple example:

List the salespeople who work in offices that are over target.

SELECT NAME
FROM SALESREPS
WHERE REP_OFFICE IN (SELECT OFFICE
                                           FROM OFFICES
                                           WHERE SALES > TARGET)

NAME

Mary Jones
Sam Clark
Bill Adams
Sue Smith
Larry Fitch

The subquery produces a set of office numbers where the sales are above target. (In the sample database, there are three such offices, numbered 11, 13, and 21.) The main query then checks each row of the SALESREPS table to determine whether that particular salesperson works in an office with one of these numbers. Here are some other examples of subqueries that test set membership:


Figure 9-4.  Subquery set membership test (IN) syntax diagram

List the salespeople who do not work in offices managed by Larry Fitch (employee 108).

 SELECT NAME
  FROM SALESREPS
  WHERE REP_OFFICE NOT IN (SELECT OFFICE
                       FROM OFFICES
                       WHERE MGR = 108)

NAME

————

Bill Adams
Mary Jones
Sam Clark
Bob Smith
Dan Roberts
Paul Cruz

List all of the customers who have placed orders for ACI Widgets (manufacturer ACI, product numbers starting with 4100) between January and June 1990.

SELECT COMPANY
 FROM CUSTOMERS
 WHERE CUST_NUM IN (SELECT DISTINCT CUST
                      FROM ORDERS
                   WHERE MFR = ‘ACI’
                    AND PRODUCT LIKE ’4100%’
                   AND ORDER_DATE BETWEEN ’01-JAN-90′
                                     AND ’30-JUN-90′)

COMPANY

——————

Acme Mfg.
Ace International
Holm & Landis
JCP Inc

In each of these examples, the subquery produces a column of data values, and the WHERE clause of the main query checks to see whether a value from a row of the main query matches one of the values in the column. The subquery form of the IN test thus works exactly like the simple IN test, except that the set of values is produced by a subquery instead of being explicitly listed in the statement.

The Existence Test (EXISTS)

The existence test (EXISTS) checks whether a subquery produces any rows of query results, as shown in Figure 9-5. There is no simple comparison test that resembles the existence test; it is used only with subqueries.

Here is an example of a request that can be expressed naturally using an existence test:

List the products for which an order of $25,000 or more has been received.

The request could easily be rephrased as:

List the products for which there exists at least one order in the ORDERS table (a) that is for the product in question and (b) that has an amount of at least $25,000.

The SELECT statement used to retrieve the requested list of products closely resembles the rephrased request:

SELECT DISTINCT DESCRIPTION
  FROM PRODUCTS
  WHERE EXISTS  (SELECT ORDER_NUM
                                    FROM ORDERS
                               WHERE PRODUCT = PRODUCT_ID
                                  AND MFR = MFR_ID
                                  AND AMOUNT >= 25000.00)

DESCRIPTION
———-
500-lb Brace
Left Hinge
Right Hinge
Widget Remover

Conceptually, SQL processes this query by going through the PRODUCTS table and performing the subquery for each product. The subquery produces a column containing the order numbers of any orders for the “current” product that are over $25,000. If there are any such orders (that is, if the column is not empty), the EXISTS test is TRUE. If the subquery produces no rows, the EXISTS test is FALSE. The EXISTS test cannot produce a NULL value.


Figure 9-5.  Existence test (EXISTS) syntax diagram

You can reverse the logic of the EXISTS test using the NOT EXISTS form. In this case, the test is TRUE if the subquery produces no rows, and FALSE otherwise.

Notice that the EXISTS search condition doesn’t really use the results of the subquery
at all. It merely tests to see whether the subquery produces any results. For this reason, SQL relaxes the rule that “subqueries must return a single column of data” and allows you to use the SELECT* form in the subquery of an EXISTS test. The previous subquery could thus have been written:

List the products for which an order of $25,000 or more has been received.

SELECT DESCRIPTION
 FROM PRODUCTS
WHERE EXISTS (SELECT *
                FROM ORDERS
               WHERE PRODUCT = PRODUCT_ID
                 AND MFR = MFR_ID
                 AND AMOUNT >= 25000.00)

In practice, the subquery in an EXISTS test is always written using the SELECT * notation.

Here are some additional examples of queries that use EXISTS:

List any customers assigned to Sue Smith who have not placed an order for over $3000.

SELECT COMPANY
  FROM CUSTOMERS
 WHERE CUST_REP = (SELECT EMPL_NUM
                      FROM SALESREPS
                   WHERE NAME = ‘Sue Smith’)
 AND NOT EXISTS (SELECT *
                  FROM ORDERS
                 WHERE CUST = CUST_NUM
                   AND AMOUNT > 3000.00)

COMPANY

—————–

Carter & Sons
Fred Lewis Corp.

List the offices where there is a salesperson whose quota represents more than 55 percent of the office’s target.

SELECT CITY
   FROM OFFICES
 WHERE EXISTS (SELECT * 
                 FROM SALESREPS 
                WHERE REP_OFFICE = OFFICE 
                  AND QUOTA > (.55 * TARGET))

CITY
———
Denver
Atlanta

Note that in each of these examples, the subquery includes an outer reference to a column of the table in the main query. In practice, the subquery in an EXISTS test will always contain an outer reference that links the subquery to the row currently being tested by the main query.

Quantified Tests (ANY and ALL) *

The subquery version of the IN test checks whether a data value is equal to some value in a column of subquery results. SQL provides two quantified tests, ANY and ALL, that extend this notion to other comparison operators, such as greater than (>) and less than (<). Both of these tests compare a data value to the column of data values produced by a subquery, as shown in Figure 9-6.

 

Figure 9-6.  Quantified comparison tests (ANY and ALL) syntax diagrams 

This article is excerpted from MySQL The Complete Reference by Vikram Vaswani (McGraw-Hill, 2003; ISBN 0072224770). Check it out at your favorite bookstore today. Buy this book now.

{mospagebreak title=The ANY Test *}

The ANY test is used in conjunction with one of the six SQL comparison operators (=, <>, <, <=, >, >=) to compare a single test value to a column of data values produced by a subquery. To perform the test, SQL uses the specified comparison operator to compare the test value to each data value in the column, one at a time. If any of the individual comparisons yield a TRUE result, the ANY test returns a TRUE result.

Here is an example of a request that can be handled with the ANY test:

List the salespeople who have taken an order that represents more than 10 percent of their quota.

SELECT NAME
  FROM SALESREPS
 WHERE (.1 * QUOTA) < ANY (SELECT AMOUNT
                           FROM ORDERS
                          WHERE REP = EMPL_NUM)

NAME

————–

Sam Clark
Larry Fitch
Nancy Angelli

Conceptually, the main query tests each row of the SALESREPS table, one by one. The subquery finds all of the orders taken by the current salesperson and returns a column containing the order amounts for those orders. The WHERE clause of the main query then computes 10 percent of the current salesperson’s quota and uses it as a test value, comparing it to every order amount produced by the subquery. If any order amount exceeds the calculated test value, the ANY test returns TRUE, and the salesperson is included in the query results. If not, the salesperson is not included inthe query results. The keyword SOME is an alternative for ANY specified by the ANSI/ISO SQL standard. Either keyword can generally be used, but some DBMS brands do not support SOME.

The ANY test can sometimes be difficult to understand because it involves an entire set of comparisons, not just one. It helps if you read the test in a slightly different way than it appears in the statement. If this ANY test appears:

  WHERE X < ANY (SELECT Y …)


instead of reading the test like this:


“where X is less than any select Y…”

try reading it like this:

“where, for some Y, X is less than Y”

When you use this trick, the preceding query becomes:

Select the salespeople where, for some order taken by the salesperson, 10 percent of the salesperson’s quota is less than the order amount.

 If the subquery in an ANY test produces no rows of query results, or if the query results include NULL values, the operation of the ANY test may vary from one DBMS to another. The ANSI/ISO SQL standard specifies these detailed rules describing the results of the ANY test when the test value is compared to the column of subquery results:

  • If the subquery produces an empty column of query results, the ANY test returns FALSE—there is no value produced by the subquery for which the comparison test holds.

  • If the comparison test is TRUE for at least one of the data values in the column, then the ANY search condition returns TRUE—there is indeed some value produced by the subquery for which the comparison test holds.

  • If the comparison test is FALSE for every data value in the column, then the ANY search condition returns FALSE. In this case, you can conclusively state that there is no value produced by the subquery for which the comparison test holds.

  • If the comparison test is not TRUE for any data value in the column, but it is NULL (unknown) for one or more of the data values, then the ANY search condition returns NULL. In this situation, you cannot conclusively state whether there is a value produced by the subquery for which the comparison test holds; there may or may not be, depending on the “actual” (but currently unknown) values for the NULL data.

The ANY comparison operator can be very tricky to use in practice, especially in conjunction with the inequality (<>) comparison operator. Here is an example that shows the problem:

List the names and ages of all the people in the sales force who do not manage an office.

It’s tempting to express this query as shown in this example:

 SELECT NAME, AGE
  FROM SALESREPS
 WHERE EMPL_NUM <> ANY (SELECT MGR
                                                        FROM OFFICES)

The subquery:

SELECT MGR
   FROM OFFICES

obviously produces the employee numbers of the managers, and therefore the query seems to be saying:

Find each salesperson who is not the manager of any office.

But that’s not what the query says! What it does say is this:

Find each salesperson who, for some office, is not the manager of that office.

Of course for any given salesperson, it’s possible to find some office where that salesperson is not the manager. The query results would include all the salespeople and therefore fail to answer the question that was posed! The correct query is:

 SELECT NAME,  AGE
   FROM SALESREPS
  WHERE NOT  (EMPL_NUM = ANY  (SELECT MGR
                                                         FROM OFFICES

 

  NAME AGE          AGE

  —————   —–

Mary Jones            31
Sue Smith              48
Dan Roberts          45
Tom Snyder           41
Paul Cruz                29
Nancy Angelli        49

 

The ALL Test *

Like the ANY test, the ALL test is used in conjunction with one of the six SQL comparison operators (=, <>, <, <=, >, >=) to compare a single test value to a column of data values produced by a subquery. To perform the test, SQL uses the specified comparison operator to compare the test value to each data value in the column, one at a time. If all of the individual comparisons yield a TRUE result, the ALL test returns a TRUE result

Here is an example of a request that can be handled with the ALL test:

List the offices and their targets where all of the salespeople have sales that exceed 50 percent of the office’s target.

SELECT CITY,  TARGET
  FROM OFFICES
 WHERE (.50 * TARGET) < ALL (SELECT SALES
                                                            FROM SALESREPS
                                                       WHERE REP_OFFICE = OFFICE)

         CITY                      TARGET

        ———–   ————–

Denver $300,000.00
New York $575,000.00
Atlanta $350,000.00

Conceptually, the main query tests each row of the OFFICES table, one by one. The subquery finds all of the salespeople who work in the current office and returns a column containing the sales for each salesperson. The WHERE clause of the main query then computes 50 percent of the office’s target and uses it as a test value, comparing it to every sales value produced by the subquery. If all of the sales values exceed the calculated test value, the ALL test returns TRUE, and the office is included in the query results. If not, the office is not included in the query results.

Like the ANY test, the ALL test can be difficult to understand because it involves an entire set of comparisons, not just one. Again, it helps if you read the test in a slightly different way than it appears in the statement. If this ALL test appears:

WHERE X < ALL (SELECT Y …)

instead of reading it like this:

“where X is less than all select Y…”

try reading the test like this:

“where, for all Y, X is less than Y”

When you use this trick, the preceding query becomes:

Select the offices where, for all salespeople who work in the office, 50 percent of the office’s target is less than the salesperson’s sales.

If the subquery in an ALL test produces no rows of query results, or if the query results include NULL values, the operation of the ALL test may vary from one DBMS to another. The ANSI/ISO SQL standard specifies these detailed rules describing the results of the ALL test when the test value is compared to the column of subquery results:

  • If the subquery produces an empty column of query results, the ALL test returns TRUE. The comparison test does hold for every value produced by the subquery; there just aren’t any values.

  • If the comparison test is TRUE for every data value in the column, then the ALL search condition returns TRUE. Again, the comparison test holds true for every value produced by the subquery.

  • If the comparison test is FALSE for any data value in the column, then the ALL search condition returns FALSE. In this case, you can conclusively state that the comparison test does not hold true for every data value produced by the query.

  • If the comparison test is not FALSE for any data value in the column, but it is NULL for one or more of the data values, then the ALL search condition returns NULL. In this situation, you cannot conclusively state whether there is a value produced by the subquery for which the comparison test does not hold true; there may or may not be, depending on the “actual” (but currently unknown) values for the NULL data.

The subtle errors that can occur when the ANY test is combined with the inequality (<>) comparison operator also occur with the ALL test. As with the ANY test, the ALL test can always be converted into an equivalent EXISTS test by moving the comparison inside the subquery.

This article is excerpted from MySQL The Complete Reference by Vikram Vaswani (McGraw-Hill, 2003; ISBN 0072224770). Check it out at your favorite bookstore today. Buy this book now.

{mospagebreak title=Subqueries and Joins}

You may have noticed as you read through this chapter that many of the queries that were written using subqueries could also have been written as multitable queries, or joins. This is often the case, and SQL allows you to write the query either way. This example illustrates the point:

List the names and ages of salespeople who work in offices in the Western region.

SELECT NAME, AGE
   FROM SALESREPS
  WHERE REP_OFFICE IN (SELECT OFFICE
                                                  FROM OFFICES
                                             WHERE REGION = ‘Western’)

   NAME                                 AGE

  ————–                  ——

Sue Smith                            48
Larry Fitch                           62
Nancy Angelli                      49

This form of the query closely parallels the stated request. The subquery yields a list of offices in the Western region, and the main query finds the salespeople who work in one of the offices in the list. Here is an alternative form of the query, using a two-table join:

List the names and ages of salespeople who work in offices in the Western region.

SELECT NAME, AGE
    FROM SALESREPS, OFFICES
  WHERE REP_OFFICE = OFFICE
   AND REGION = ‘Western’

                                   NAME                 AGE

                                   ————–   —–

Sue Smith 48
Larry Fitch 62
Nancy Angelli 49

This form of the query joins the SALESREPS table to the OFFICES table to find the region where each salesperson works, and then eliminates those who do not work in the Western region.

Either of the two queries will find the correct salespeople, and neither one is right or wrong. Many people will find the first form (with the subquery) more natural, because the English request doesn’t ask for any information about offices, and because it seems a little strange to join the SALESREPS and OFFICES tables to answer the request. Of course if the request is changed to ask for some information from the OFFICES table:

List the names and ages of the salespeople who work in offices in the Western region and the cities where they work.

the subquery form will no longer work, and the two-table query must be used. Conversely, many queries with subqueries cannot be translated into an equivalent join. Here is a simple example:

List the names and ages of salespeople who have above average quotas.

SELECT NAME,  AGE
      FROM SALESREPS
  WHERE QUOTA > (SELECT AVG (QUOTA)
                                        FROM SALESREPS)

   NAME                     AGE

  ——-                    ——

   Bill Adams             37
   Sue Smith              48
   Larry Fitch              62

In this case, the inner query is a summary query and the outer query is not, so there is no way the two queries can be combined into a single join.

Nested Subqueries

All of the queries described thus far in this chapter have been two-level queries, involving a main query and a subquery. Just as you can use a subquery inside a main query, you can use a subquery inside another subquery. Here is an example of a request that is naturally represented as a three-level query, with a main query, a subquery, and a sub-subquery:

List the customers whose salespeople are assigned to offices in the Eastern sales region.

  SELECT COMPANY
       FROM CUSTOMERS
   WHERE CUST_REP IN (SELECT EMPL_NUM
  
    

                                            FROM SALESREPS
                                          WHERE REP_OFFICE IN (SELECT OFFICE
                                                         FROM OFFICES
                                                        WHERE REGION = ‘Eastern’))

COMPANY

———–

First Corp.
Smithson Corp.
AAA Investments
JCP Inc.
Chen Associates
QMA Assoc.
Ian & Schmidt
Acme Mfg.

. . .

In this example, the innermost subquery:

SELECT OFFICE
    FROM OFFICES
 WHERE REGION = ‘Eastern’

produces a column containing the office numbers of the offices in the Eastern region. The next subquery:

  SELECT EMPL_NUM
       FROM SALESREPS
WHERE REP_OFFICE IN (subquery)

produces a column containing the employee numbers of the salespeople who work in one of the selected offices. Finally, the outermost query:

  SELECT COMPANY
      FROM CUSTOMERS
 WHERE CUST_REP IN (subquery)

finds the customers whose salespeople have one of the selected employee numbers.

The same technique used in this three-level query can be used to build queries with four or more levels. The ANSI/ISO SQL standard does not specify a maximum number of nesting levels, but in practice, a query becomes much more time-consuming as the number of levels increases. The query also becomes more difficult to read, understand, and maintain when it involves more than one or two levels of subqueries. Many SQL implementations restrict the number of subquery levels to a relatively small number.

Correlated Subqueries *

In concept, SQL performs a subquery over and over again—once for each row of the main query. For many subqueries, however, the subquery produces the same results for every row or row group. Here is an example:

List the sales offices whose sales are below the average target.

 SELECT CITY
     FROM OFFICES
 WHERE SALES < (SELECT AVG(TARGET)
                                     FROM OFFICES)

CITY

———

Denver
Atlanta

In this query, it would be silly to perform the subquery five times (once for each office). The average target doesn’t change with each office; it’s completely independent of the office currently being tested. As a result, SQL can handle the query by first performing the subquery, yielding the average target ($550,000), and then converting the main query into:

SELECT CITY
    FROM OFFICES
  WHERE SALES < 550000.00

Commercial SQL implementations automatically detect this situation and use this shortcut whenever possible to reduce the amount of processing required by a subquery. However, the shortcut cannot be used if the subquery contains an outer reference, as in this example:

List all of the offices whose targets exceed the sum of the quotas of the salespeople who work in them:

SELECT CITY
    FROM OFFICES

WHERE TARGET > (SELECT SUM(QUOTA)
                                      FROM SALESREPS
                                WHERE REP_OFFICE = OFFICE)

CITY

————

Chicago
Los Angeles

For each row of the OFFICES table to be tested by the WHERE clause of the main query, the OFFICE column (which appears in the subquery as an outer reference) has a different value. Thus, SQL has no choice but to carry out this subquery five times—once for each row in the OFFICES table. A subquery containing an outer reference is called a correlated subquery because its results are correlated with each individual row of the main query. For the same reason, an outer reference is sometimes called a correlated reference.

A subquery can contain an outer reference to a table in the FROM clause of any query that contains the subquery, no matter how deeply the subqueries are nested. A column name in a fourth-level subquery, for example, may refer to one of the tables named in the FROM clause of the main query, or to a table named in the FROM clause of the second-level subquery or the third-level subquery that contains it. Regardless of the level of nesting, an outer reference always takes on the value of the column in the current row of the table being tested.

Because a subquery can contain outer references, there is even more potential for ambiguous column names in a subquery than in a main query. When an unqualified column name appears within a subquery, SQL must determine whether it refers to a table in the subquery’s own FROM clause, or to a FROM clause in a query containing the subquery. To minimize the possibility of confusion, SQL always interprets a column reference in a subquery using the nearest FROM clause possible. To illustrate this point, in this example, the same table is used in the query and in the subquery:

List the salespeople who are over 40 and who manage a salesperson over quota.

SELECT NAME
    FROM SALESREPS
  WHERE AGE > 40
       AND EMPL_NUM IN (SELECT MANAGER
                                               FROM SALESREPS
                                         WHERE SALES > QUOTA)

NAME

Sam Clark
Larry Fitch

The MANAGER, QUOTA, and SALES columns in the subquery are references to the

SALESREPS table in the subquery’s own FROM clause; SQL does not interpret them as outer references, and the subquery is not a correlated subquery. SQL can perform the subquery first in this case, finding the salespeople who are over quota and generating a list of the employee numbers of their managers. SQL can then turn its attention to the main query, selecting managers whose employee numbers appear in the generated list.

If you want to use an outer reference within a subquery like the one in the previous example, you must use a table alias to force the outer reference. This request, which adds one more qualifying condition to the previous one, shows how:

List the managers who are over 40 and who manage a salesperson who is over quota and who does not work in the same sales office as the manager.

SELECT NAME
    FROM SALESREPS MGRS
 WHERE AGE > 40
       AND MGRS.EMPL_NUM IN (SELECT MANAGER
                                                          FROM SALESREPS EMPS
                                                      WHERE EMPS.QUOTA > EMPS.SALES
                                  AND EMPS.REP_OFFICE <> MGRS.REP_OFFICE)

NAME

————-

Sam Clark
Larry Fitch

The copy of the SALESREPS table used in the main query now has the tag MGRS, and the copy in the subquery has the tag EMPS. The subquery contains one additional search condition, requiring that the employee’s office number does not match that of the manager. The qualified column name MGRS.OFFICE in the subquery is an outer reference, and this subquery is a correlated subquery.

This article is excerpted from MySQL The Complete Reference by Vikram Vaswani (McGraw-Hill, 2003; ISBN 0072224770). Check it out at your favorite bookstore today. Buy this book now.

{mospagebreak title=Subqueries in the HAVING Clause *}

Although subqueries are most often found in the WHERE clause, they can also be used in the HAVING clause of a query. When a subquery appears in the HAVING clause, it works as part of the row group selection performed by the HAVING clause. Consider this query with a subquery:

List the salespeople whose average order size for products manufactured by ACI is higher than overall average order size.

  SELECT NAME, AVG(AMOUNT)
       FROM SALESREPS, ORDERS
  WHERE EMPL_NUM = REP
     AND MFR = ‘ACI’
  GROUP BY NAME
HAVING AVG(AMOUNT) > (SELECT AVG(AMOUNT)
                                                     FROM ORDERS)

NAME              AVG (AMOUNT)

———-       —————-

Sue Smith        $15,000.00
Tom Snyder     $22,500.00

Figure 9-7 shows conceptually how this query works. The subquery calculates the overall average order size. It is a simple subquery and contains no outer references, so SQL can calculate the average once and then use it repeatedly in the HAVING clause. The main query goes through the ORDERS table, finding all orders for ACI products, and groups them by salesperson. The HAVING clause then checks each row group to see whether the average order size in that group is bigger than the average for all orders, calculated earlier. If so, the row group is retained; if not, the row group is discarded. Finally, the SELECT clause produces one summary row for each group, showing the name of the salesperson and the average order size for each.

You can also use a correlated subquery in the HAVING clause. Because the subquery is evaluated once for each row group, however, all outer references in the correlated subquery must be single-valued for each row group. Effectively, this means that the outer reference must either be a reference to a grouping column of the outer query or be contained within a column function. In the latter case, the value of the column function for the row group being tested is calculated as part of the subquery processing.

If the previous request is changed slightly, the subquery in the HAVING clause becomes a correlated subquery:

List the salespeople whose average order size for products manufactured by ACI is at least as big as that salesperson’s overall average order size.

SELECT NAME,  AVG(AMOUNT)
     FROM SALESREPS, ORDERS

 WHERE EMPL_NUM = REP
      AND MFR = ‘ACI’
  GROUP BY NAME, EMPL_NUM
HAVING AVG(AMOUNT) >= (SELECT AVG(AMOUNT)
                                                       FROM ORDERS
                                                   WHERE REP = EMPL_NUM)

                  NAME            AVG(AMOUNT)

                  ——–         —————

Bill Adams $7,865.40
Sue Smith $15,000.00
Tom Snyder $22,500.00

In this new example, the subquery must produce the overall average order size for the salesperson whose row group is currently being tested by the HAVING clause. The subquery selects orders for that particular salesperson, using the outer reference EMPL_NUM. The outer reference is legal because EMPL_NUM has the same value in all rows of a group produced by the main query.


Figure 9-7.  Subquery operation in the HAVING clause

Subquery Summary

This chapter has described subqueries, which allow you to use the results of one query to help define another query. Before moving on to the advanced query facilities of the SQL2 specification, let’s summarize subqueries:

  • A subquery is a “query within a query.” Subqueries appear within one of the subquery search conditions in the WHERE or HAVING clause.

  • When a subquery appears in the WHERE clause, the results of the subquery are used to select the individual rows that contribute data to the query results.

  • When a subquery appears in the HAVING clause, the results of the subquery are used to select the row groups that contribute data to the query results.

  • Subqueries can be nested within other subqueries.

  • The subquery form of the comparison test uses one of the simple comparison operators to compare a test value to the single value returned by a subquery.

  • The subquery form of the set membership test (IN) matches a test value to the set of values returned by a subquery.

  • The existence test (EXISTS) checks whether a subquery returns any values.

  • The quantified tests (ANY and ALL) use one of the simple comparison operators to compare a test value to all of the values returned by a subquery, checking to see whether the comparison holds for some or all of the values.

  • A subquery may include an outer reference to a table in any of the queries that contain it, linking the subquery to the current row of that query.

Figure 9-8 shows the final version of the rules for SQL query processing, extended to include subqueries. It provides a complete definition of the query results produced by a SELECT statement.


Figure 9-8.  SQL query processing rules (final version)

Advanced Queries in SQL2 *

The SQL queries described thus far in Chapters 6–9 are the mainstream capabilities provided by most SQL implementations. The combination of features they represent— column selection in the SELECT clause, row selection criteria in the WHERE clause, multitable joins in the FROM clause, summary queries in the GROUP BY and HAVING clauses, and subqueries for more complex requests—give the user a powerful set of data retrieval and data analysis capabilities. However, database experts have pointed out many limitations of these mainstream query capabilities, including these:

    • No decision making within queries. Suppose you wanted to generate a two-column report from the sample database showing the name of each sales office and either its annual sales target or its year-to-date sales, whichever is larger. With standard SQL query features, this is hard to do. Or suppose you had a database that kept track of sales by quarter (four columns of data for each office) and wanted to write a program that displayed offices and their sales for

    • a specific (user-supplied) quarter. Again, this program is more difficult to write using standard SQL queries. You must include four separate SQL queries (one for each quarter), and the program logic must select which query to run, based on user input. This simple case isn’t too difficult, but in a more general case, the program could become much more complex.
  • Limited use of subqueries. The simplest example of this limitation is the SQL1 restriction that a subquery can appear only on the right side of a comparison test in a WHERE clause. The database request “list the offices where the sum of the salesperson’s quotas is greater than the office target” is most directly expressed as this query:

SELECT OFFICE
 FROM OFFICES
WHERE (SELECT SUM(QUOTA)
                  FROM SALESREPS
             WHERE REP_OFFICE = OFFICE) > TARGET

But this isn’t a legal SQL1 statement. Instead, you must turn the inequality around:

  • SELECT OFFICE
       FROM OFFICES
    WHERE TARGET > (SELECT SUM(QUOTA)
                                         FROM SALESREPS
                                    WHERE REP_OFFICE = OFFICE)

In this simple example, it isn’t hard to turn the logic around, but the restriction is a nuisance at best, and it does prevent you from comparing the results of two subqueries, for example.

  • Limited-row expressions. Suppose you wanted to list the suppliers, item numbers, and prices for a set of products that are substitutes for one another. Conceptually, these are a set of products whose identification (a manufacturer-ID/product-ID pair) matches one of a set of values, and it would be natural to write the query using a set membership test as:

 SELECT MFR_ID, PRODUCT_ID, PRICE
      FROM PRODUCTS
 WHERE (MFR_ID, PRODUCT_ID) IN ((‘ACI’,41003),(‘BIC’,41089), …)

The SQL1 standard doesn’t permit this kind of set membership test. Instead, you must construct the query as a long set of individual comparisons, connected by ANDs and ORs.

  •  Limited-table expressions. SQL allows you to define a view like this one for large orders:

SELECT *
    FROM PRODUCTS
WHERE AMOUNT > 10000

and then use the view as if it were a real table in the FROM clause of a query to find out which products, in which quantities, were ordered in these large orders:

SELECT MFR, PRODUCT, SUM(QTY)
   FROM BIGORDERS
GROUP BY MFR,  PRODUCT

Conceptually, SQL should let you substitute the view definition right into the query, like this:

  SELECT MFR,  PRODUCT,  SUM (QTY)
  FROM (SELECT * FROM ORDERS WHERE AMOUNT > 10000)
  GROUP BY MFR,  PRODUCT

But the SQL1 standard doesn’t allow a subquery in this position in the WHERE clause. Yet clearly, the DBMS should be able to determine the meaning of this query, since it must basically do the same processing to interpret the BIGORDERS view definition.

As these examples show, the SQL1 standard and mainstream DBMS products that implement to this level of the standard are relatively restrictive in their permitted use of expressions involving individual data items, sets of data items, rows, and tables. The SQL2 standard includes a number of advanced query capabilities that are focused on removing these restrictions and making the SQL language more general. The spirit of these SQL2 capabilities tends to be that a user should be able to write a query expression that makes sense and have the query expression be a legal SQL query. Because these SQL2 capabilities constitute a major expansion of the language over the SQL1 standard, most of them are required only at a full level of the standard.

Scalar-Valued Expressions (SQL2)

The simplest extended query capabilities in SQL2 are those that provide more data manipulation and calculation power involving individual data values (called scalars in the SQL2 standard). Within the SQL language, individual data values tend to have three sources:

  • The value of an individual column within an individual row of a table

  • A literal value, such as 125.7 or ABC

  • A user-supplied data value, entered into a program

In this SQL query:

 SELECT NAME, EMPL_NUM, HIRE_DATE, (QUOTA * .9)
    FROM SALESREPS
 WHERE (REP_OFFICE = 13) OR TITLE = ‘VP SALES’

the column names NAME, EMPL_NUM, HIRE_DATE, and QUOTA generate individual data values for each row of query results, as do the column names REP_OFFICE and TITLE in the WHERE clause. The numbers .9 and 13 and the character string “VP SALES” similarly generate individual data values. If this SQL statement appeared within an embedded SQL program (described in Chapter 17), the program variable office_num might contain an individual data value, and the query might appear as:

  SELECT NAME, EMPL_NUM, HIRE_DATE, (QUOTA * .9)
           FROM SALESREPS
  WHERE (REP_OFFICE = :o ffice_num) OR TITLE = ‘VP SALES’

As this query and many previous examples have shown, individual data values can be combined in simple expressions, like the calculated value QUOTA*.9. To these basic SQL1 expressions, SQL2 adds the CAST operator for explicit data type conversion, the CASE operator for decision making, the NULLIF operation for conditionally creating a NULL value, and the COALESCE operator for conditionally creating non-NULL values.

This article is excerpted from MySQL The Complete Reference by Vikram Vaswani (McGraw-Hill, 2003; ISBN 0072224770). Check it out at your favorite bookstore today. Buy this book now.

{mospagebreak title=The CAST Expression (SQL2)}

The SQL standard has fairly restrictive rules about combining data of different types in expressions. It specifies that the DBMS shall automatically convert among very similar data types, such as 2-byte and 4-byte integers. However, if you try to compare numbers and character data, for example, the standard says that the DBMS should generate an error. The standard considers this an error condition even if the character string contains numeric data. You can, however, explicitly ask the DBMS to convert among data types using the CAST expression, whose syntax is shown in Figure 9-9.


Figure 9-9.  SQL2 CAST expression syntax diagram

The CAST expression tends to be of little importance when you are typing SQL statements directly into an interactive SQL interface. However, it can be critical when using SQL from within a programming language whose data types don’t match the data types supported by the SQL standard. For example, the CAST expression in the SELECT clause of this query converts the values for REP_OFFICE (integers in the sample database) and HIRE_DATE (a date in the sample database) into character strings for the returned query results:

  SELECT NAME, CAST REP_OFFICE AS VARCHAR, HIRE_DATE AS VARCHAR
   FROM SALESREPS

The CAST expression can generally appear anywhere that a scalar-valued expression

can appear within a SQL statement. In this example, it’s used in the WHERE clause to convert a character string customer number into an integer, so that it can be compared with the data in the database:

SELECT PRODUCT, QTY, AMOUNT
   FROM ORDERS
  WHERE CUST = CAST ’2107′ AS INTEGER

Instead of specifying a data type in the CAST expression, you can specify a SQL2 domain. Domains are specific collections of legal data values that can be defined in the database under the SQL2 standard. They are fully described in Chapter 11 because of the role they play in SQL data integrity. Note that you can also generate a NULL value of the appropriate data type for use in SQL expressions using the CAST expression.

The most common uses for the CAST expression are:

  • To convert data from within a database table where the column is defined with the wrong data type. For example, when a column is defined as a character string, but you know it actually contains numbers (that is, strings of digits) or dates (strings that can be interpreted as a month/day/year).

  • To convert data from data types supported by the DBMS that are not supported by a host programming language. For example, most host programming languages do not have explicit date and time data types and require that date/time values be converted into character strings for handling by a program.

  • To eliminate differences between data types in two different tables. For example, if an order date is stored in one table as DATE data, but a product availability date is stored in a different table as a character string, you can still compare the columns from the two tables by CASTing one of the columns into the data type of the other. Similarly, if you want to combine data from two different tables with a UNION operation, their columns must have identical data types. You can achieve this by CASTing the columns of one of the tables. 

                 
           Figure 9-10.  SQL2 CASE expression syntax diagram

The CASE Expression (SQL2)

The SQL2 CASE expression provides for limited decision making within SQL expressions. Its basic structure, shown in Figure 9-10, is similar to the IF…THEN…ELSE statement found in many programming languages. When the DBMS encounters a CASE expression, it evaluates the first search condition, and if it is TRUE, then the value of the CASE expression is the value of the first result expression. If the result of the first search condition is not TRUE, the DBMS proceeds to the second search condition and checks whether it is TRUE.If so, the value of the CASE expression is the value of the second result expression, and so on.

Here is a simple example of the use of the CASE expression. Suppose you want to do an A/B/C analysis of the customers from the sample database according to their credit limits. The A customers are the ones with credit limits over $60,000, the B customers are those with limits over $30,000 and the C customers are the others. Using SQL1, you would have to retrieve customer names and credit limits from the database and then rely on an application program to look at the credit limit values and assign an A, B, or C rating. Using a SQL2 CASE expression, you can have the DBMS do the work for you:

SELECT COMPANY,CASE WHEN CREDIT_LIMIT > 60000 THEN
A’                                        WHEN CREDIT_LIMIT > 30000 THEN ‘B’
                                            ELSE ‘C’
  FROM CUSTOMERS

For each row of query results, the DBMS evaluates the CASE expression by first comparing the credit limit to $60,000, and if the comparison is TRUE, returns an A in the second column of query results. If that comparison fails, the comparison to $30,000 is made and returns a B if this second comparison is TRUE. Otherwise, the third column of query results will return a C.

This is a very simple example of a CASE expression. The results of the CASE expression are all literals here, but in general, they can be any SQL expression. Similarly, there is no requirement that the tests in each WHEN clause are similar, as they are here. The CASE expression can also appear in other clauses of a query. Here is an example of a query where it’s useful in the WHERE clause. Suppose you want to find the total of the salesperson’s sales, by office. If a salesperson is not yet assigned to an office, that person should be included in the total for his or her manager’s office. Here is a query that generates the appropriate office groupings:

SELECT CITY, SUM(SALES)
   FROM OFFICES, SALESREPS
  WHERE OFFICE =

CASE WHEN (REP_OFFICE IS NOT NULL) THEN REP_OFFICE
           ELSE (SELECT REP_OFFICE
                          FROM SALESREPS AS MGRS
                              WHERE MGRS.EMPL_NUM = MANAGER)

The SQL2 standard provides a shorthand version of the CASE expression for the

common situation where you want to compare a test value of some kind to a sequence of data values (usually literals). This version of the CASE syntax is shown in Figure 9-11. Instead of repeating a search condition of the form:

test_value = value1

in each WHEN clause, it lets you specify the test_valuecalculation once. For example, suppose you wanted to generate a list of all of the offices, showing the names of their managers and the cities and states where they are located. The sample database doesn’t include state names, so the query must generate this information itself. Here is a query, with a CASE expression in the SELECT list, that does the job:

SELECT NAME, CITY, CASE OFFICE WHEN 11 THEN ‘New York’
                                                               WHEN 12 THEN ‘Illinois’
                                                               WHEN 13 THEN ‘Georgia’
                                                               WHEN 21 THEN ‘California’
                                                               WHEN 22 THEN ‘Colorado’
     FROM OFFICES, SALESREPS
  WHERE MGR = EMPL_NUM

The COALESCE Expression (SQL2)


Figure 9-11.  SQL2 CASE expression alternative syntax

The COALESCE Expression (SQL2)

One of the most common uses for the decision-making capability of the CASE expression is for handling NULL values within the database. For example, it’s frequently desirable to have a NULL value from the database represented by some literal value (such as the word “missing”) or by some default value when using SQL to generate a report. Here is a report that lists the salespeople and their quotas. If a salesperson has not yet been assigned a quota, assume that the salesperson’s actual year-to-date sales should be listed instead. If for some reason the actual year-to-date sales are also NULL (unknown), then a zero amount should be listed. The CASE statement generates the desired IF…THEN… ELSE logic:

  SELECT NAME, CASE WHEN (QUOTA IS NOT NULL) THEN QUOTA                           WHEN (SALES IS NOT NULL) THEN SALES
                                        ELSE 0.00
   FROM SALESREPS

This type of NULL-handling logic is needed frequently, so the SQL2 standard includes a specialized form of the CASE expression, the COALESCE expression, to handle it. The syntax for the COALESCE expression is shown in Figure 9-12. The processing rules for the COALESCE expression are very straightforward. The DBMS examines the first value in the list. If its value is not NULL, it becomes the value of the COALESCE expression. If the first value is NULL, the DBMS moves to the second value and checks to see whether it is NULL. If not, it becomes the value of the expression. Otherwise, the DBMS moves to the third value, and so on. Here is the same example just given, expressed with the COALESCE expression instead of a CASE expression:

  SELECT NAME, COALESCE (QUOTA, SALES, 0.00)
    FROM SALESREPS

As you can see by comparing the two queries, the simplicity of the COALESCE syntax makes it easier to see, at a glance, the meaning of the query. However, the operation of the two queries is identical. The COALESCE expression adds simplicity, but no new capability, to the SQL2 language.

This article is excerpted from MySQL The Complete Reference by Vikram Vaswani (McGraw-Hill, 2003; ISBN 0072224770). Check it out at your favorite bookstore today. Buy this book now.

{mospagebreak title=The NULLIF Expression (SQL2)}

Just as the COALESCE expression is used to eliminate NULL values when they are not desired for processing, sometimes you may need to create NULL values. In many data processing applications (especially older ones that were developed before relational databases were popular), missing data is not represented by NULL values. Instead, some special code value that is otherwise invalid is used to indicate that the data is missing.

Figure 9-12.  SQL2 COALESCE expression syntax diagram

For example, suppose that in the sample database, the situation where a salesperson had not yet been assigned a manager was indicated by a zero (0) value in the MANAGER column instead of a NULL value. In some circumstances, you will want to detect this situation within a SQL query and substitute the NULL value for the zero “code.” The NULLIF expression, shown in Figure 9-13, is used for this purpose. When the DBMS encounters a NULLIF expression, it examines the first value (usually a column name) and compares it to the second value (usually the code value used to indicate missing data). If the two values are equal, the expression generates a NULL value. Otherwise, the expression generates the first value.

 Here is a query that handles the case where missing office numbers are represented by a zero:

   SELECT CITY, SUM(SALES)
      FROM OFFICES, SALESREPS
   WHERE OFFICE = (NULLIF REP_OFFICE, 0)
     GROUP BY CITY

Row-Valued Expressions (SQL2)

Although columns and the scalar data values they contain are the atomic building blocks of a relational database, the structuring of columns into rows that represent real-world entities, such as individual offices or customers or orders, is one of the most important features of the relational model. The SQL1 standard, and most mainstream commercial database products, certainly reflect this row/column structure, but they provide very limited capability to actually manipulate rows and groups of rows. Basically, SQL1 operations allow you to insert a row into a table, or to retrieve, update, or delete groups of rows from a database (using the SELECT, UPDATE,or DELETE statements).


Figure 9-13.  SQL2 NULLIF expression syntax diagram

The SQL2 standard goes well beyond these capabilities, allowing you to generally use rows in SQL expressions in much the same way that you can use scalar values. It provides a syntax for constructing rows of data. It allows row-valued subqueries. And it defines row-valued meanings for the SQL comparison operators and other SQL structures.

The Row-Value Constructor (SQL2)

SQL2 allows you to specify a row of data values by using a row-value constructor expression, whose syntax is shown in Figure 9-14. In its most common form, the row constructor is a comma-separated list of literal values, or expressions. For example, here is a row-value constructor for a row of data whose structure matches the OFFICES table in the sample database:

 
Figure 9-14.  SQL2 row-value constructor syntax diagram

(23, ‘San Diego’, ‘Western’, NULL, DEFAULT, 0.00)

The result of this expression is a single row of data with six columns. The NULL keyword in the fourth column position indicates that the fourth column in the constructed row should contain a NULL (unknown) value. The DEFAULT keyword in the fifth column position indicates that the fifth column in the constructed row should contain the default value for the column. This keyword may appear in a row-value constructor only in certain situations—for example, when the row-value constructor appears in an INSERT statement to add a new row to a table.

When a row constructor is used in the WHERE clause of a SQL statement, column names can also appear as individual data items within the row constructor, or as part of an expression within the row constructor. For example, consider this query:

List the order number, quantity, and amount of all orders for ACI-41002 widgets.

SELECT ORDER_NUM, QTY, AMOUNT
  FROM ORDERS
WHERE (MFR, PRODUCT) = (‘ACI’, ’41002′)

Under the normal rules of SQL query processing, the WHERE clause is applied to each row of the ORDERS table, one by one. The first row-value constructor in the WHERE clause (to the left of the equal sign) generates a two-column row, containing the manufacturer code and the product number for the current order being considered. The second row-value constructor (to the right of the equal sign) generates a two-column row, containing the (literal) manufacturer code ACI and product number 41002. The equal sign is now comparing two rows of values, not two scalar values. The SQL2 standard defines this type of row-valued comparison for equality, which is processed by comparing, pairwise, each of the columns in the two rows. The result of the comparison is TRUE only if all of the pairwise column comparisons are TRUE. Of course, it’s possible to write the query without the row-value constructors, like this:

List the order number, quantity, and amount of all orders for ACI-41002 widgets.

SELECT ORDER_NUM, QTY, AMOUNT
  FROM ORDERS
WHERE (MFR = ‘ACI’) AND (PRODUCT = ’41002′)

and in this simple example, the meaning of the query is probably equally clear with either form. However, row-value constructors can be very useful in simplifying the appearance of more complex queries, and they become even more useful when combined with row-valued subqueries.

Row-Valued Subqueries (SQL2)

As described throughout the earlier parts of this chapter, the SQL1 standard provides a subquery capability for expressing more complex database queries. The subquery takes the same form as a SQL query (that is, a SELECT statement), but a SQL1 subquery must be scalar-valued—that is, it must produce a single data value as its query results. The value generated by the subquery is then used as part of an expression within the main SQL statement that contains the subquery. This use of subqueries is supported by the major enterprise-class relational database systems today.

The SQL2 standard dramatically expands the subquery facility, including support for row-valued subqueries. A row-valued subquery returns not just a single data item, but a row of data items, which can be used in SQL2 expressions and compared to other rows. For example, suppose you wanted to show the order numbers and dates for all of the orders placed against the highest-priced product in the sample database. A logical way to start building the appropriate SQL query is to find an expression that will give you the identity (manufacturer ID and product ID) of the high-priced product in question. Here is a query that finds the right product:

Find the manufacturer ID and product ID of the product with the highest unit price.

SELECT MFR_ID,  PRODUCT_ID
    FROM PRODUCTS
  WHERE PRICE = (SELECT MAX (PRICE)
                                      FROM PRODUCTS

Ignoring the possibility of a tie for the most expensive product for a moment, this query will generate a single row of query results, consisting of two columns. Using SQL2’s row-valued subquery capability, you can embed this entire query as a subquery within a SELECT statement to retrieve the order information:

List the order numbers and dates of all orders placed for the highest-priced product.

SELECT ORDER_NUM, ORDER_DATE
FROM ORDERS
WHERE (MFR, PRODUCT) = (SELECT MFR_ID, PRODUCT_ID
                                                     FROM PRODUCTS
                                                 WHERE PRICE = (SELECT MAX(PRICE)
                                                         FROM PRODUCTS))

The top-level WHERE clause in this query contains a row-valued comparison. On the left side of the equal sign is a row-value constructor consisting of two column names. Each time the WHERE clause is examined to carry out the top-level query, the value of this row-valued expression is a manufacturer-ID/product-ID pair from a row of the ORDERS table. On the right side of the equal sign is the subquery that generates the identity of the product with the highest dollar value. The result of this subquery is again a row value, with two colu

mns, whose data types match those of the row-valued expression on the left side of the equal sign.

It’s possible to express this query without the row-valued subquery, but the resulting query will be much less straightforward:

List the order numbers and dates of all orders placed for the highest-priced product.

  SELECT ORDER_NUM, ORDER_DATE
    FROM ORDERS
    WHERE (MFR = (SELECT MFR_ID
                               FROM PRODUCTS
                                WHERE PRICE = (SELECT MAX(PRICE)
                                FROM PRODUCTS)))
  AND (PRODUCT = (SELECT PRODUCT_ID
                                   FROM PRODUCTS
                                 WHERE PRICE = (SELECT MAX(PRICE)
                               FROM PRODUCTS)))

Instead of a single row-valued comparison in the WHERE clause, the resulting query has two separate scalar-valued comparisons, one for the manufacturer ID and one for the

product ID. Because the comparison must be split, the lower-level subquery to find the maximum price must be repeated twice as well. Overall, the form of the query using the row-valued expression is a more direct translation of the English-language request, and it’s easier to read and understand.

This article is excerpted from MySQL The Complete Reference by Vikram Vaswani (McGraw-Hill, 2003; ISBN 0072224770). Check it out at your favorite bookstore today. Buy this book now.

{mospagebreak title=Row-Valued Comparisons (SQL2)}

The most common use of row-valued expressions in the WHERE or HAVING clause is within a test for equality, as illustrated by the last few examples. A constructed row (often consisting of column values from a candidate row of query results) is compared to another constructed row (perhaps a row of subquery results or a row of literal values), and if the rows are equal, the candidate row is included in the query results. The SQL2 standard also provides for row-valued forms of the inequality comparison tests and the range test. When comparing two rows for inequality, SQL2 uses the same rules that it would use if the columns were being used to sort the rows. It compares the contents of the first column in the two rows, and if they are unequal, uses them to order the rows. If they are equal, the comparison moves to the second column, and then the third, and so on. Here are the resulting comparisons for some three-column constructed rows derived from the ORDERS table:

(‘ACI’,’41002’,54) < (‘REI’,’2A44R’,5)—based on first column(‘ACI’,’41002’,54) < (‘ACI’,’41003’,35)—based on second column(‘ACI’,’41002’,10) < (‘ACI’,’41002’,54)—based on third column

Table-Valued Expressions (SQL2)

In addition to its extended capabilities for expressions involving simple scalar data values and row values, the SQL2 standard dramatically extends the SQL capabilities for table processing. It provides a mechanism for constructing a table of data values in place within a SQL statement. It allows table-valued subqueries and extends the SQL1 subquery tests to handle them. It also allows subqueries to appear in many more places within a SQL statement—for example, a subquery can appear in the FROM clause of a SELECT statement as of its source tables. Finally, it provides expanded capabilities for combining tables, including the UNION, INTERSECTION, and DIFFERENCE operations.

The Table-Value Constructor (SQL2)

SQL2 allows you to specify a table of data values within a SQL statement by using a table-value constructor expression, whose syntax is shown in Figure 9-15. In its simplest form, the table-value constructor is a comma-separated list of row-value constructors, each of which contains a comma-separated set of literals that form individual column values. For example, the SQL2 INSERT statement uses a table-value constructor as the source of the data to be inserted into a database. While the SQL1 INSERT statement (described in Chapter 10) allows you to insert only a single row of data, the next SQL2 INSERT statement inserts three rows into the OFFICES table.


Figure 9-15.  SQL2 table-value constructor syntax diagram

Add three offices to the OFFICES table.

  INSERT INTO OFFICES (OFFICE,CITY,REGION,MGR,SALES)
             VALUES (23, ‘San Diego’, ‘Western’, 108, 0.00),
                            (24, ‘Seattle’, ‘Western’, 104, 0.00),
                            (14, ‘Boston’, ‘Eastern, NULL, 0.00)

Note that the individual rows in the table-value constructor are not restricted to contain only literal values. The source of a data value can be a scalar-valued subquery, or an entire row can be the result of a row-valued subquery. Although it doesn’t make much sense in the sample database, this is a legal SQL2 INSERT statement that illustrates these capabilities:

Add three offices to the OFFICES table.

  INSERT INTO OFFICES (OFFICE,CITY,REGION,MGR,SALES)
             VALUES (23, ‘San Diego’, ‘Western’, 108, 0.00),
                            (24, ‘Seattle’, ‘Western’, (SELECT MANAGER
                                                         FROM SALESREPS
                                                        WHERE EMPL_NUM 105),
     0.00),            (SELECT ‘BOSTON’, ‘EASTERN’, REGION, MGR, 0.00
                                  FROM OFFICES
                            WHERE OFFICE = 12)

Like the preceding example, the VALUES clause in this INSERT statement generates a three-row table to be inserted. The first row is specified with literal values. In the second row, the fourth column is specified as a scalar-valued subquery that retrieves the manager of employee number 105. In the third row, the entire row is generated by a row-valued subquery. In this case, three of the column values in the subquery’s SELECT clause are actually literal values, but the third and fourth columns are produced by the subquery, which retrieves the manager and region for the New York office (number 12).

Table-Valued Subqueries (SQL2)

Just as SQL2 expands the use of scalar subqueries into row-valued subqueries, it also extends the SQL subquery facility to support table-valued subqueries—that is, subqueries that return a full table of results. One useful role for table-valued subqueries is within the WHERE or HAVING clause, where it is combined with extended forms of the subquery tests. For example, suppose you wanted to list the descriptions and prices of all products with orders exceeding $20,000 in the sample database. Perhaps the most straightforward way to express this request is in this SQL2 statement that uses a table-valued subquery:

List the description and price of all products with individual orders over $20,000.

  SELECT DESCRIPTION, PRICE
     FROM PRODUCTS
  WHERE (MFR_ID,PRODUCT_ID) IN (SELECT MFR, PRODUCT
                                                         FROM ORDERS
                                                        WHERE AMOUNT > 20000.00)

The top-level query is a straightforward statement of the English-language request—it asks for the description and price of those products whose identification (as in previous examples, a manufacturer-ID/product-ID pair) matches some set of products. This is expressed as a subquery set membership test in the WHERE clause. The subquery generates a two-column table of subquery results, which are the identifications of the products that meet the stated order size criterion.

It’s certainly possible to express this query in other ways. From the discussion in Chapter 7, you probably recognize that it can be stated as a join of the PRODUCTS and ORDERS tables with a compound search condition:

List the description and price of all products with individual orders over $20,000.

SELECT DESCRIPTION, PRICE
      FROM PRODUCTS,  ORDERS
WHERE (MFR_ID = MFR)
      AND (PRODUCT_ID = PRODUCT)
      AND (AMOUNT > 20000.00)

This is an equally valid statement of the query, but it’s a lot further removed from the English-language request, and therefore more difficult to understand for most people. As queries become more complex, the ability to use table-valued subqueries becomes even more useful to simplify and clarify SQL requests.

This article is excerpted from MySQL The Complete Reference by Vikram Vaswani (McGraw-Hill, 2003; ISBN 0072224770). Check it out at your favorite bookstore today. Buy this book now.

{mospagebreak title=The SQL2 Query Specification}


Figure 9-16.  SQL2 query specification: formal definition

The SQL2 standard formalizes the definition of what we have loosely been calling a SELECT statement or a query in the last three chapters into a basic building block called a query specification. For a complete understanding of the SQL2 table expression capabilities in the next section, it’s useful to understand this formal definition. The form of a SQL2 query specification is shown in Figure 9-16. Its components should be familiar from the earlier chapters:

  • A select list specifies the columns of query results. Each column is specified by an expression that tells the DBMS how to calculate its value. The column can be assigned an optional alias with the AS clause.

  • The keywords ALL or UNIQUE control duplicate row elimination in the query results.

  • The FROM clause specifies the tables that contribute to the query results.

  • The WHERE clause describes how the DBMS should determine which rows are included in the query results and which should be discarded.

  • The GROUP BY and HAVING clauses together control the grouping of individual query results rows in a grouped query, and the selection of row groups for inclusion or exclusion in the final results.

The query specification is the basic query building block in the SQL2 standard. Conceptually, it describes the process of combining data from the tables in the FROM clause into a row/column table of query results. The value of the query specification is a table of data. In the simplest case, a SQL2 query consists of a simple query specification. In a slightly more complex case, a query specification is used to describe a subquery, which appears within another (higher-level) query specification. Finally, query specifications can be combined using table-valued operations to form general-purpose query expressions, as described in the next section.

Query Expressions (SQL2)

The SQL2 standard defines a query expression as the full, general-purpose way that you can specify a table of query results in the SQL2 language. The basic building blocks you can use to create a query expression are the following:

  • A query specification, as described in the preceding section (SELECT…FROM…). Its value is a table of query results.

  • A table-value constructor, as previously described (VALUES …). Its value is a table of constructed values.

  • An explicit table reference (TABLE tblname). Its value is the contents of the named table.

Using these building blocks, SQL2 lets you combine their table values using the following operations:

    • JOIN. SQL2 provides explicit support for full cross-product joins (cross joins), natural joins, inner joins, and all types of outer joins (left, right, and full), as described in Chapter 7. A JOIN operation takes two tables as its input and produces a table of combined query results according to the join specification.
    • UNION. The SQL2 UNION operation provides explicit support for merging the rows of two compatible tables (that is, two tables having the same number of columns and with corresponding columns having the same data types). The UNION operation takes two tables as its input and produces a single merged table of query results.

  • DIFFERENCE. The SQL2 EXCEPT operation takes two tables as its input and produces as its output a table containing the rows that appear in the first table but that do not appear in another table—that is, the rows that are missing from the second table. Conceptually, the EXCEPT operation is like table subtraction. The rows of the second table are taken away from the rows of the first table, and the answer is the remaining rows of the first table.

  • INTERSECT. The SQL2 INTERSECT operation takes two tables as its input and produces as its output a table containing the rows that appear in both input tables.

SQL2 UNION, INTERSECT, and DIFFERENCE Operations

The SQL2 UNION, INTERSECT, and DIFFERENCE operations provide set operations for combining two input tables to form an output table. All three of the operations require that the two input tables be union-compatible—they must have the same number of columns, and the corresponding columns of each table must have identical data types. Here are some simple examples of SQL2 query expressions involving UNION, INTERSECT, and DIFFERENCE operations based on the sample database:

Show all products for which there is an order over $30,000 or more than $30,000 worth of inventory on hand.

  (SELECT MFR, PRODUCT
      FROM ORDERS
  WHERE AMOUNT > 30000.00)
   UNION
(SELECT MFR_ID, PRODUCT_ID
      FROM PRODUCTS
 WHERE (PRICE * QTY_ON_HAND) > 30000)

Show all products for which there is an order over $30,000 and more than $30,000 worth of inventory on hand.

  (SELECT MFR, PRODUCT
       FROM ORDERS
   WHERE AMOUNT > 30000.00)
   INTERSECT
   SELECT MFR_ID,  PRODUCT_ID
        FROM PRODUCTS
     WHERE (PRICE * QTY_ON_HAND > 30000)

Show all products for which there is an order over $30,000 except for those products that sell for under $1000.

  (SELECT MFR, PRODUCT
        FROM ORDERS
 WHERE AMOUNT > 30000.00)
EXCEPT
(SELECT MFR_ID, PRODUCT_ID
     FROM PRODUCTS
  WHERE PRICE < 100.00)

By default, the UNION, INTERSECT, and EXCEPT operations eliminate duplicate rows during their processing. This is usually the desired result, as it is in these examples, but occasionally, you may need to suppress the elimination of duplicate rows. You can do this by specifying the UNION ALL, INTERSECT ALL,or EXCEPT ALL forms of the operations.

Note each of these examples produces a two-column table of query results. The results come from two different source tables within the database—the ORDERS table and the PRODUCTS table. However, the columns selected from these tables have the same corresponding data types, so they can be combined using these operations. In the sample database, the corresponding columns have different names in the two tables. (The manufacturer-ID column is named MFR in the ORDERS table but named MFR_ID in the PRODUCTS table.)

However, corresponding columns such as these will often have the same name in each of the tables being combined. As a convenience, SQL2 lets you specify the corresponding columns in a CORRESPONDING clause attached to the UNION, INTERSECT, or EXCEPT operation. Here is the preceding UNION example, changed for the situation where the ORDERS and PRODUCTS tables have parallel column names for manufacturer ID and product ID:

Show all products for which there is an order over $30,000 or more than $30,000 worth of nventory on hand.

  (SELECT *
        FROM ORDERS
   WHERE AMOUNT > 30000.00)
   UNION CORRESPONDING BY (MFR, PRODUCT)
(SELECT *
      FROM PRODUCTS
 WHERE (PRICE * QTY_ON_HAND) > 30000)

In a case like this one where all of the corresponding (that is, identically named) columns from the two tables participate in the UNION operation, SQL2 even allows you to leave off the explicit list of column names:

Show all products for which there is an order over $30,000 or more than $30,000 worth of inventory on hand.

  (SELECT *
       FROM ORDERS
  WHERE AMOUNT > 30000.00)
  UNION CORRESPONDING
(SELECT *
     FROM PRODUCTS
 WHERE (PRICE * QTY_ON_HAND) > 30000)

Finally, it’s worth noting that the column alias capability of the query specification can be used to rename or assign names to the columns from the individual query results that are being combined with the UNION operation. If we eliminate the assumption that the PRODUCTS and ORDERS tables use the same column names, it’s still possible to use the CORRESPONDING form of the UNION operation in this query simply by renaming the columns in one of the tables:

Show all products for which there is an order over $30,000 or more than $30,000 worth of inventory on hand.

SELECT *
    FROM ORDERS
 WHERE AMOUNT > 30000.00)

 

    UNION CORRESPONDING
SELECT MFR_ID AS MFR,  PRODUCT_ID AS PRODUCT
     FROM PRODUCTS
WHERE  (PRICE * QTY_ON_HAND > 30000)

In this simple example, there is not much advantage in this construct, but in the more general case where the individual queries involve calculated columns or are grouped queries, the CORRESPONDING clause and column aliases can help to clarify the meaning of the query.

This article is excerpted from MySQL The Complete Reference by Vikram Vaswani (McGraw-Hill, 2003; ISBN 0072224770). Check it out at your favorite bookstore today. Buy this book now.

{mospagebreak title=Query Expressions in the FROM Clause}

SQL2 query expressions provide a much more powerful and flexible method for generating and combining tables of query results than the simple subquery and UNION operations provided by the SQL1 standard. To make query expressions even more useful and more general-purpose, the SQL2 standard allows them to appear almost anywhere that a table reference could appear in a SQL1 query. In particular, a query expression can appear in place of a table name in the FROM clause. Here is a simple example of a SQL2 query for the sample database that uses this feature:

Show the names and total outstanding orders of all customers with credit limits over $50,000.

  SELECT COMPANY, TOT_ORDERS
  FROM CUSTOMER, (SELECT CUST, SUM(AMOUNT) AS TOT_ORDERS            FROM ORDERS
                                   GROUP BY CUST),
   WHERE (CREDIT_LIMIT > 50000.00)
        AND (CUST_NUM = CUST)

The second “table name” in the FROM clause of the main query is not a table name at all, but a full-blown query expression. In fact, the expression could have been much more complex, involving UNION or JOIN operations. When a query expression appears in the FROM clause, as it does here, the DBMS conceptually carries it out first, before any other processing of the query, and creates a temporary table of the query results generated by the query expression. In this case, this temporary table consists of two columns, listing each customer number and the total of orders for that customer number. This temporary table then acts as one of the source tables for the main query. In this example, its contents are joined to the CUSTOMER table to obtain the company name and generate the answer to the main question.

There are many other ways in which this query could be written. The entire query could be written as one top-level grouped query that joins the CUSTOMER and ORDERS table. The join operation could be made explicit with a SQL2 JOIN operator, and then the results of the join could be grouped in the top-level query. As this example shows, one of the benefits of the SQL2 query expression capabilities is that they typically provide several different ways to obtain the same query results.

The general philosophy behind the SQL2 capabilities in this area is that the SQL language should provide the flexibility to express a query in the most natural form. The underlying DBMS must be able to take the query, however expressed, break it down into its fundamentals, and then determine the most efficient way to carry out the query. This internal query execution plan may be quite different than the apparent plan called for by the actual SQL statement, but as long as it produces the same query results, the net effect is to shift the optimization workload from the human user or programmer to the DBMS.

SQL Queries: A Final Summary

This concludes the discussion of the SQL queries and the SELECT statement that began in Chapter 6. As described in Chapters 6–9, the clauses of the SELECT statement provide a powerful, flexible set of capabilities for retrieving data from the database. Each clause plays a specific role in data retrieval:

  • The FROM clause specifies the source tables that contribute data to the query results. Every column name in the body of the SELECT statement must unambiguously identify a column from one of these tables, or it must be an outer reference to a column from a source table of an outer query.

  • The WHERE clause, if present, selects individual combinations of rows from the source tables to participate in the query results. Subqueries in the WHERE clause are evaluated for each individual row.

  • The GROUP BY clause, if present, groups the individual rows selected by the WHERE clause into row groups.

  • The HAVING clause, if present, selects row groups to participate in the query results. Subqueries in the HAVING clause are evaluated for each row group.

  • The SELECT clause determines which data values actually appear as columns in the final query results.

  • The DISTINCT keyword, if present, eliminates duplicate rows of query results.

  • The UNION operator, if present, merges the query results produced by individual SELECT statements into a single set of query results.

  • The ORDER BY clause, if present, sorts the final query results based on one or more columns.

  • The SQL2 query expression capabilities add row-valued and table-valued expressions and INTERSECT and EXCEPT operations to the SQL1 capabilities. The fundamental flow of query processing is not changed, but the capability to express queries within queries is greatly enhanced.

This article is excerpted from MySQL The Complete Reference by Vikram Vaswani (McGraw-Hill, 2003; ISBN 0072224770). Check it out at your favorite bookstore today. Buy this book now.

Join the conversation
about this article

View thread