Sams Teach Yourself SQL in 24 Hours, 3rd Edition

Sams Teach Yourself SQL in 24 Hours

By Ron Plew and Ryan Stephens

What Is a Subquery?

newterm_icon.gif

A subquery is a query embedded within the WHERE clause of another query to further restrict data returned by the query. A subquery is a query within another query, also known as a nested query. A subquery is used to return data that will be used in the main query as a condition to further restrict the data to be retrieved. Subqueries are used with the SELECT, INSERT, UPDATE, and DELETE statements.

A subquery can be used in some cases in place of a join operation by indirectly linking data between the tables based on one or more conditions. When a subquery is used in a query, the subquery is resolved first, and then the main query is resolved according to the condition(s) as resolved by the subquery. The results of the subquery are used to process expressions in the WHERE clause of the main query. The subquery can be used either in the WHERE clause or the HAVING clause of the main query. Logical and relational operators, such as =, >, <, <>, IN, NOT IN, AND, OR, and so on, can be used within the subquery as well to evaluate a subquery in the WHERE or HAVING clause.

There are a few rules that subqueries must follow:

  • Subqueries must be enclosed within parentheses.
  • A subquery can have only one column in the SELECT clause, unless multiple columns are in the main query for the subquery to compare its selected columns.
  • An ORDER BY cannot be used in a subquery, although the main query can use an ORDER BY. The GROUP BY can be used to perform the same function as the ORDER BY in a subquery.
  • Subqueries that return more than one row can only be used with multiple value operators, such as the IN operator.
  • The SELECT list cannot include any references to values that evaluate to a BLOB, ARRAY, CLOB, or NCLOB.
  • A subquery cannot be immediately enclosed in a set function.
  • The BETWEEN operator cannot be used with a subquery; however, the BETWEEN can be used within the subquery.

The basic syntax for a subquery is as follows:

   syntax_icon.gif
SELECT COLUMN_NAME
FROM TABLE
WHERE COLUMN_NAME = (SELECT COLUMN_NAME
                     FROM TABLE
                     WHERE CONDITIONS);

The following examples show how the BETWEEN operator can and cannot be used with a subquery.

The following is an example of a correct use of BETWEEN in the subquery:

   mysql_icon.gif
SELECT COLUMN_NAME
FROM TABLE
WHERE COLUMN_NAME OPERATOR (SELECT COLUMN_NAME
                            FROM TABLE)
                            WHERE VALUE BETWEEN VALUE)

The following is an example of an illegal use of BETWEEN with a subquery:

SELECT COLUMN_NAME 
FROM TABLE
WHERE COLUMN_NAME BETWEEN VALUE AND (SELECT COLUMN_NAME
                                     FROM TABLE)

BETWEEN cannot be used as an operator outside the subquery.

Subqueries with the SELECT Statement

Subqueries are most frequently used with the SELECT statement, although they can be used within a data manipulation statement as well. The subquery, when used with the SELECT statement, retrieves data for the main query to use to solve the main query.

The basic syntax is as follows:

   syntax_icon.gif
SELECT COLUMN_NAME [, COLUMN_NAME ]
FROM TABLE1 [, TABLE2 ]
WHERE COLUMN_NAME OPERATOR
                  (SELECT COLUMN_NAME [, COLUMN_NAME ]
                   FROM TABLE1 [, TABLE2 ]
                  [ WHERE ])

The following is an example:

   mysql_icon.gif
SELECT E.EMP_ID, E.LAST_NAME, E.FIRST_NAME, EP.PAY_RATE
FROM EMPLOYEE_TBL E, EMPLOYEE_PAY_TBL EP
WHERE E.EMP_ID = EP.EMP_ID
AND EP.PAY_RATE > (SELECT PAY_RATE
                   FROM EMPLOYEE_PAY_TBL
                   WHERE EMP_ID = '313782439');

analysis_icon.gif

The preceding SQL statement returns the employee identification, last name, first name, and pay rate for all employees who have a pay rate greater than that of the employee with the identification 313782439. In this case, you do not necessarily know (or care) what the exact pay rate is for this particular employee; you only care about the pay rate for the purpose of getting a list of employees who bring home more than the employee specified in the subquery.

The next query selects the pay rate for a particular employee. This query is used as the subquery in the following example.

   input_icon.gif

   SELECT PAY_RATE

   FROM EMPLOYEE_PAY_TBL

   WHERE EMP_ID = '220984332';

   output_icon.gif
  PAY_RATE
----------
      11

1 row selected.

The previous query is used as a subquery in the WHERE clause of the following query.

   input_icon.gif

   SELECT E.EMP_ID, E.LAST_NAME, E.FIRST_NAME, EP.PAY_RATE

   FROM EMPLOYEE_TBL E, EMPLOYEE_PAY_TBL EP

   WHERE E.EMP_ID = EP.EMP_ID
  
   AND EP.PAY_RATE > (SELECT PAY_RATE
                     
   FROM EMPLOYEE_PAY_TBL
                     
   WHERE EMP_ID = '220984332');

   output_icon.gif
EMP_ID    LAST_NAM FIRST_NA   PAY_RATE
--------- -------- ---------- --------
442346889 PLEW     LINDA         14.75
443679012 SPURGEON TIFFANY          15

2 rows selected.

The result of the subquery is 11 (shown in the last example), so the last condition of the WHERE clause is evaluated as

AND EP.PAY_RATE > 11 

You did not know the value of the pay rate for the given individual when you executed the query. However, the main query was able to compare each individual's pay rate to the subquery results.

Subqueries with the INSERT Statement

Subqueries also can be used in conjunction with data manipulation language (DML) statements. The INSERT statement is the first instance you will examine. The INSERT statement uses the data returned from the subquery to insert into another table. The selected data in the subquery can be modified with any of the character, date, or number functions.

The basic syntax is as follows:

   syntax_icon.gif
INSERT INTO TABLE_NAME [ (COLUMN1 [, COLUMN2 ]) ]
SELECT [ *|COLUMN1 [, COLUMN2 ]
FROM TABLE1 [, TABLE2 ]
[ WHERE VALUE OPERATOR ]

The following is an example of the INSERT statement with a subquery:

   mysql_icon.gif
   input_icon.gif

   INSERT INTO RICH_EMPLOYEES

   SELECT E.EMP_ID, E.LAST_NAME, E.FIRST_NAME, EP.PAY_RATE

   FROM EMPLOYEE_TBL E, EMPLOYEE_PAY_TBL EP

   WHERE E.EMP_ID = EP.EMP_ID
  
   AND EP.PAY_RATE > (SELECT PAY_RATE
                     
   FROM EMPLOYEE_PAY_TBL
                     
   WHERE EMP_ID = '220984332');

   output_icon.gif
2 rows created.

This INSERT statement inserts the EMP_ID, LAST_NAME, FIRST_NAME, and PAY_RATE into a table called RICH_EMPLOYEES for all records of employees who have a pay rate greater than the pay rate of the employee with identification 220984332.

Subqueries with the UPDATE Statement

The subquery can be used in conjunction with the UPDATE statement. Either single or multiple columns in a table can be updated when using a subquery with the UPDATE statement.

The basic syntax is as follows:

   syntax_icon.gif
UPDATE TABLE
SET COLUMN_NAME [, COLUMN_NAME) ] =
    (SELECT ]COLUMN_NAME [, COLUMN_NAME) ]
    FROM TABLE
    [ WHERE ]

Examples showing the use of the UPDATE statement with a subquery follow. The first query returns the employee identification of all employees who reside in Indianapolis. You can see that there are four individuals who meet this criteria.

   input_icon.gif

   SELECT EMP_ID

   FROM EMPLOYEE_TBL

   WHERE CITY = 'INDIANAPOLIS';

   output_icon.gif
EMP_ID
---------
442346889
313782439
220984332
443679012

4 rows selected.

The first query is used as the subquery in the following UPDATE statement. The first query proves how many employee identifications are returned by the subquery. The following is the UPDATE with the subquery:

   mysql_icon.gif
   input_icon.gif

   UPDATE EMPLOYEE_PAY_TBL

   SET PAY_RATE = PAY_RATE * 1.1

   WHERE EMP_ID IN (SELECT EMP_ID
                 
   FROM EMPLOYEE_TBL
                 
   WHERE CITY = 'INDIANAPOLIS');

   output_icon.gif
4 rows updated.

As expected, four rows are updated. One very important thing to notice is that, unlike the example in the first section, this subquery returns multiple rows of data. Because you expect multiple rows to be returned, you have used the IN operator instead of the equal sign. Remember that IN is used to compare an expression to values in a list. If the equal sign were used, an error would have been returned.

Subqueries with the DELETE Statement

The subquery also can be used in conjunction with the DELETE statement.

The basic syntax is as follows:

   syntax_icon.gif
DELETE FROM TABLE_NAME
[ WHERE OPERATOR [ VALUE ]
                (SELECT COLUMN_NAME
                FROM TABLE_NAME)
                [ WHERE) ]

In this example, you delete BRANDON GLASS's record from the EMPLOYEE_PAY_TBL table. You do not know Brandon's employee identification number, but you can use a subquery to get his identification from the EMPLOYEE_TBL table, which contains the FIRST_NAME and LAST_NAME columns.

   mysql_icon.gif
   input_icon.gif

   DELETE FROM EMPLOYEE_PAY_TBL

   WHERE EMP_ID = (SELECT EMP_ID
                
   FROM EMPLOYEE_TBL
                
   WHERE LAST_NAME = 'GLASS'
                  
   AND FIRST_NAME = 'BRANDON');

   output_icon.gif
1 row deleted.

Subquery Workaround Example with MySQL

Although subqueries are not currently supported by MySQL, the same results can sometimes be achieved from MySQL by using temporary tables.

Consider the following example that lists all employees that have an above-average PAY_RATE:

CREATE TABLE AVG_PAY_TMP AS 
SELECT AVG(PAY_RATE) PAY_RATE
FROM EMPLOYEE_PAY_TBL;

SELECT E.LAST_NAME, E.FIRST_NAME, EP.PAY_RATE
FROM EMPLOYEE_TBL E,
     EMPLOYEE_PAY_TBL EP,
     AVG_PAY_TMP AP
WHERE E.EMP_ID = EP.EMP_ID
  AND EP.PAY_RATE > AP.PAY_RATE;

First, a temporary table is created to hold the criteria that would normally be resolved by the subquery. Then, the main query uses the data in the temporary table as a condition in the WHERE clause. This is the same as a subquery, and a good way to understand the concepts of subqueries.

+ Share This