1 / 56

# Chapter 7: Subquieres - PowerPoint PPT Presentation

SQL for SQL Server Bijoy Bordoloi and Douglas Bock. Chapter 7: Subquieres. Objectives. Learn the formal subquery definition and write a subquery. Learn the subquery restrictions. Use the IN operator when writing a subquery. Nest subqueries at multiple levels. Use comparison operators.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Chapter 7: Subquieres' - Patman

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

SQL for SQL ServerBijoy Bordoloi and Douglas Bock

### Chapter 7: Subquieres

• Learn the formal subquery definition and write a subquery.

• Learn the subquery restrictions.

• Use the IN operator when writing a subquery.

• Nest subqueries at multiple levels.

• Use comparison operators.

• Use the ALL and ANY keywords.

• Write correlated subqueries including the EXISTS operator.

• Use the ORDER BY clause in queries with a subquery.

/* SQL Example 7.1 */

SELECT CAST(emp_last_name As CHAR(12)) "Last Name",

CAST(emp_first_name As CHAR(12)) "First Name",

emp_dpt_number "Dept",

'\$' + CONVERT (CHAR (10), emp_salary, 1) "Salary"

FROM employee

WHERE emp_salary 25000 AND emp_dpt_number IN (3, 7);

Last Name First Name Dept Salary

––––-------- ––––-------- ––---- –––—--------

Bock Douglas 7 \$ 30,000.00

Amin Hyder 3 \$ 25,000.00

Joshi Dinesh 7 \$ 38,000.00

more rows are displayed . . .

• Here the emp_salary and emp_dpt_number are known in advance.

• Management needs a list of employee names who earn a salary equal to the minimum salary paid to any employee, and the minimum salary will certainly change over time. Step 1: Identify the minimum salary.

/* SQL Example 7.2 */

SELECT MIN(emp_salary) "Min Salary"

FROM employee;

Min Salary

--------------

25000.0000

• Add the query as a subquery in the WHERE clause.

/* SQL Example 7.3 */

SELECT CAST(emp_last_name As CHAR(12)) "Last Name",

CAST(emp_first_name As CHAR(12)) "First Name",

'\$' CONVERT(CHAR(10), emp_salary, 1) "Salary"

FROM employee

WHERE emp_salary =

(SELECT MIN(emp_salary)

FROM employee);

Last Name First Name Salary

––––-------- ––––-------- –––—--------

Amin Hyder \$ 25,000.00

Markis Marcia \$ 25,000.00

Prescott Sherri \$ 25,000.00

• Formal Definition: The use of a SELECT statement inside one of the clauses of another SELECT statement, e.g., a subquery is a query within a query.

• Subqueries enable you to write queries that select data rows for criteria that are actually developed while the query is executing at run time.

• The criteria values for a WHERE clause are unknown at design time.

• There are three basic types of subqueries. We will study each of these in the remainder of this chapter.

Type 1: Subqueries that operate on lists by use of the IN operator or with a comparison operator modified by the ANY or ALL optional keywords. These subqueries can return a group of values, but the values must be from a single column of a table. In other words, the SELECT clause of the subquery must contain only one expression or column name.

Type 2: Subqueries that use an unmodified comparison operator (=, <, >, <>) – these subqueries must return only a single, scalar value.

Type 3: Subqueries that use the EXISTS operator to test the existence of data rows satisfying specified criteria.

• A subquery SELECT statement is very similar to the SELECT statement used to begin a regular or outer query. The complete syntax of a subquery is shown below.

(SELECT [DISTINCT] subquery_select_parameter

FROM {table_name | view_name}

{table_name | view_name} ...

[WHERE search_conditions]

[GROUP BY column_name [,column_name ] ...]

[HAVING search_conditions] )

• The SELECT clause of a subquery must contain only one expression, only one aggregate function, or only one column name.

• The value(s) returned by a subquery must be join compatible with the WHERE clause of the outer query.

• The dep_emp_ssn and emp_ssn columns share a common domain of possible values.

/* SQL Example 7.4 */

SELECT CAST(emp_last_name As CHAR(12)) "Last Name",

CAST(emp_first_name As CHAR(12)) "First Name"

FROM employee

WHERE emp_ssn IN

(SELECT dep_emp_ssn

FROM dependent);

Last Name First Name

––––----- ––--------

Bock Douglas

Zhu Waiman

Joyner Suzanne

Bordoloi Bijoy

• In addition to concerns about the domain of values returned from a subquery, the data type of the returned column value(s) must be join compatible.

• Join-compatible data types are data types that SQL Server will convert automatically when matching data in criteria conditions.

• Example: It would make no sense to compare the emp_ssn column to the dep_date_of_birth column.

• SQL Server maps ANSI standard data types to T-SQL data types.

• Conversion is automatic among any of these ANSI numeric data types when making numeric comparisons:

• int (integer)

• smallint (small integer)

• decimal

• float

• SQL Server does not make comparisons based on column names.

• Columns from two tables that are being compared may have different names as long as they have a shared domain and the same data type or convertible data types.

There are additional restrictions for subqueries.

• The DISTINCT keyword cannot be used in subqueries that include a GROUP BY clause.

• Subqueries cannot manipulate their results internally. This means that a subquery cannot include the ORDER BY clause (unless a TOP keyword is in the SELECT clause), the COMPUTE clause, or the INTO keyword.

• A result table can only include columns from a table named in the outer query’s FROM clause.

• Here, a subquery produces a list of values for the subquery result table.

• Subqueries that are introduced with the keyword IN take the general form:

• WHERE expression [NOT] IN (subquery)

• The only difference in the use of the IN operator with subqueries is that the list does not consist of hard-coded values.

/* SQL Example 7.6 */

SELECT CAST(emp_last_name As CHAR(12))

"Last Name", CAST(emp_first_name

As CHAR(12)) "First Name"

FROM employee

WHEREemp_ssn IN

(SELECT dep_emp_ssn

FROM dependent

WHERE dep_gender 'M');

Last Name First Name

––––-------- ––––---------

Bock Douglas

Zhu Waiman

Joyner Suzanne

Understanding the IN Operator with Subqueries

• Conceptually, this SELECT statement is evaluated in two steps. First, the inner query returns the identification numbers of those employees that have male dependents.

/* SQL Example 7.7 */

SELECT dep_emp_ssn

FROM dependent

WHERE dep_gender 'M';

dep_emp_ssn

–––—-------

999111111

999444444

999555555

Understanding the IN Operator with Subqueries Cont.

• Next, the list of social security number values is substituted into the outer query as the object of the IN operator. Conceptually, the outer query now looks like the following.

/* SQL Example 7.8 */

SELECT CAST(emp_last_name As CHAR(12)) "Last Name",

CAST(emp_first_name As CHAR(12)) "First Name"

FROM employee

WHERE emp_ssn IN (999444444, 999555555, 999111111);

Last Name First Name

––––-------- –---------–––

Bock Douglas

Zhu Waiman

Joyner Suzanne

• The previous subquery can also be written as a join query.

/* SQL Example 7.9 */

SELECT CAST(emp_last_name As CHAR(12)) "Last Name",

CAST(emp_first_name As CHAR(12)) "First Name"

FROM employee e JOIN dependent d ON

(e.emp_ssn = d.dep_emp_ssn)

WHERE d.dep_gender = 'M';

Last Name First Name

––––-------- ––––---------

Bock Douglas

Zhu Waiman

Joyner Suzanne

• Use a subquery when the result table displays columns from a single table.

• Use a join query when the result displays columns from two or more tables.

• Use a join query when the existence of values must be checked with the EXISTS operator—a join query may perform better than a subquery. The EXISTS operator is discussed later in the chapter.

• Like the IN operator, the NOT IN operator can take the result of a subquery as the operator object.

/* SQL Example 7.10 */

SELECT CAST(emp_last_name As CHAR(12)) "Last Name",

CAST(emp_first_name As CHAR(12)) "First Name"

FROM employee

WHERE emp_ssn NOT IN

(SELECT dep_emp_ssn

FROM dependent);

Last Name First Name

––––-------- –--------–––-

Amin Hyder

Joshi Dinesh

Markis Marcia

Prescott Sherri

• The inner query in SQL Example 7.10 produces an intermediate result table containing the social security numbers of employees who have dependents in the dependent table.

• Conceptually, the outer query compares each row of the employee table against the result table. If the employee social security number is NOT found in the result table produced by the inner query, then it is included in the final result table.

• Subqueries may themselves contain subqueries.

• When the WHERE clause of a subquery has as its object another subquery, these are termed nested subqueries.

• SQL Server has no practical limit on the number of queries that can be nested in a WHERE clause.

• Consider the problem of producing a listing of employees that worked more than 10 hours on the project named Order Entry.

/* SQL Example 7.11 */

SELECT CAST(emp_last_name As CHAR(12)) "Last Name",

CAST(emp_first_name As CHAR(12)) "First Name"

FROM employee

WHERE emp_ssn IN

(SELECT work_emp_ssn

FROM assignment

WHERE work_hours > 10 AND work_pro_number IN

(SELECT pro_number

FROM project

WHERE pro_name = 'Order Entry') );

Last Name First Name

––––-------- ---------––––

Bock Douglas

Prescott Sherri

• In order to understand this subquery’s execution, begin your examination with the lowest subquery.

• Executing it independently of the outer queries produces the following scalar result table.

/* SQL Example 7.12 */

SELECT pro_number

FROM project

WHERE pro_name ='Order Entry';

pro_number

–––-------

1

• Substitute the project number into the IN operator list for the intermediate subquery and execute it. The result table lists two employee SSN values for employees that worked more than 10 hours on project #1.

/* SQL Example 7.13 */

SELECT work_emp_ssn

FROM assignment

WHERE work_hours > 10 AND work_pro_numberIN (1);

work_emp_ssn

––––--------

999111111

999888888

• Now, substitute these two social security numbers into the IN operator listing for the outer query in place of the subquery.

/* SQL Example 7.14 */

SELECT CAST(emp_last_name As CHAR(12)) "Last Name",

CAST(emp_first_name As CHAR(12)) "First Name"

FROM employee

WHERE emp_ssn IN (999111111, 999888888);

Last Name First Name

––––-------- ---––––------

Bock Douglas

Prescott Sherri

• The general form of the WHERE clause with a comparison operator is similar to that used thus far in the text.

• Note that the syntax for the subquery is to be enclosed by parentheses.

WHERE <expression> <comparison_operator> (subquery)

• The most important point to remember when using a subquery with a comparison operator is that the subquery can only return a single or scalar value.

• This is also termed a scalar subquery because a single column of a single row is returned by the subquery.

• If a subquery returns more than one value, the SQL Server will generate the Msg 512: Subquery returned more than 1 row error message, and the query will fail to execute.

• SQL Example 7.16 violates the "single value" rule – this subquery returns multiple values for the emp_salary column.

/* SQL Example 7.16 */

SELECT emp_ssn

FROM employee

WHERE emp_salary >

(SELECT emp_salary

FROM employee

WHERE emp_salary > 40000);

Server: Msg 512, Level 16, State 1, Line 2 Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

• The aggregate functions (AVG, SUM, MAX, MIN, and COUNT) always return a scalar result table.

• Thus, a subquery with an aggregate function as the object of a comparison operator will always execute provided you have formulated the query properly.

• Consider a situation where a payroll manager needs an employee listing with a salary level greater than the average salary level for all employees.

/* SQL Example 7.17 */

SELECT CAST(emp_last_name As CHAR(12)) "Last Name",

CAST(emp_first_name As CHAR(12)) "First Name",

'\$' CONVERT (CHAR (10), emp_salary, 1) "Salary"

FROM employee

WHERE emp_salary >

(SELECT AVG(emp_salary)

FROM employee);

Last Name First Name Salary

––––-------- ––––--------- –––-—------

Joshi Dinesh \$ 38,000.00

Zhu Waiman \$ 43,000.00

Joyner Suzanne \$ 43,000.00

Bordoloi Bijoy \$ 55,000.00

• The ALL and ANY keywords modify a comparison operator to allow an outer query to accept multiple values from a subquery.

• The ALL keyword modifies the greater than comparison operator to mean greater than all values.

• The general form of the WHERE clause for this type of query is shown here.

WHERE <expression> <comparison_operator>

[ALL | ANY] (subquery)

• Subqueries that use these keywords may also include GROUP BY and HAVING clauses.

An ALLKeyword Example

• List all employees with a salary larger than the largest salary for an employee in dept 7.

/* SQL Example 7.18 */

SELECT CAST(emp_last_name As CHAR(12)) "Last Name",

CAST(emp_first_name As CHAR(12)) "First Name",

'\$' + CONVERT (CHAR (10), emp_salary, 1) "Salary"

FROM employee

WHERE emp_salary > ALL

(SELECT emp_salary

FROM employee

WHERE emp_dpt_number = 7);

Last Name First Name Salary

––––-------- ––––--------- –––—-------

Bordoloi Bijoy \$ 55,000.00

An ALLKeyword Example Cont.

• Conceptually, for each row in the employee table, the inner query creates a list of employee salaries for those in department 7. The outer query finds the largest salary for department 7 – \$43,000 – only Bordoloi has a larger salary.

/* SQL Example 7.19 */

SELECT emp_salary

FROM employee

WHERE emp_dpt_number = 7;

emp_salary

–––––––--------------

30000.0000

38000.0000

43000.0000

25000.0000

• The ANY keyword is not as restrictive as the ALL keyword.

• When used with the greater than comparison operator, "> ANY" means greater than some value.

• Management needs the employee name and salary of any employee with a salary greater than that of any employee with a salary that exceeds \$30,000.

/* SQL Example 7.20 */

SELECT CAST(emp_last_name As CHAR(12)) "Last Name",

CAST(emp_first_name As CHAR(12)) "First Name",

'\$' + CONVERT (CHAR (10), emp_salary, 1) "Salary"

FROM employee

WHERE emp_salary > ANY

(SELECT emp_salary

FROM employee

WHERE emp_salary > 30000);

Last Name First Name Salary

––––-------- ––––--------- –––—-------

Zhu Waiman \$ 43,000.00

Joyner Suzanne \$ 43,000.00

Bordoloi Bijoy \$ 55,000.00

• The "= ANY" operator is exactly equivalent tothe IN operator.

• For example, to find the names of employees that have male dependents, you can use either IN or "= ANY" – SQL Examples 7.22 and 7.23 will produce an identical result table.

/* SQL Example 7.22 */

SELECT CAST(emp_last_name As CHAR(12)) "Last Name",

CAST(emp_first_name As CHAR(12)) "First Name"

FROM employee

WHERE emp_ssn IN

(SELECT dep_emp_ssn

FROM dependent

WHERE dep_gender = 'M');

/* SQL Example 7.23 */

SELECT CAST(emp_last_name As CHAR(12)) "Last Name",

CAST(emp_first_name As CHAR(12)) "First Name"

FROM employee

WHERE emp_ssn = ANY

(SELECT dep_emp_ssn

FROM dependent

WHERE dep_gender = 'M');

Last Name First Name

––––-------- ––––---------

Bock Douglas

Zhu Waiman

Joyner Suzanne

• While the "= ANY" is identical to the IN operator, the "!= ANY" (not equal any) is not equivalent to the NOT IN operator.

• If a subquery of employee salaries produces an intermediate result table with the salaries \$38,000, \$43,000, and \$55,000, then the WHERE clause shown here means "NOT \$38,000" AND "NOT \$43,000" AND "NOT \$55,000".

WHERE NOT IN (38000, 43000, 55000);

• However, the "!= ANY" comparison operator and keyword combination shown in this next WHERE clause means "NOT \$38,000" OR "NOT \$43,000" OR "NOT \$55,000".

WHERE != ANY (38000, 43000, 55000);

• This query tries to produce a listing of employees with no dependents; however the query fails – all employees of the firm will be listed because each emp_ssn in the employee table will NOT match at least one dep_emp_ssn in the dependent table.

/* SQL Example 7.24 */

SELECT CAST(emp_last_name As CHAR(12)) “Last Name”,

CAST(emp_first_name As CHAR(12)) “First Name”

FROM employee

WHERE emp_ssn != ANY

(SELECT DISTINCT dep_emp_ssn

FROM dependent);

• These employees have no dependents.

/* SQL Example 7.25 */

SELECT CAST(emp_last_name As CHAR(12)) "Last Name",

CAST(emp_first_name As CHAR(12)) "First Name"

FROM employee

WHERE emp_ssn NOT IN

(SELECT DISTINCT dep_emp_ssn

FROM dependent);

Last Name First Name

––––-------- ––––---------

Amin Hyder

Joshi Dinesh

Markis Marcia

Prescott Sherri

A correlated subquery is one where the inner query depends on values provided by the outer query.

• This means the inner query is executed repeatedly, once for each row that might be selected by the outer query.

• Suppose management needs a listing of the most highly paid employee for each department.

• The inner query references the outer query’s employee table with the alias e1, so e1.emp_dpt_number is treated like a variable—it changes as SQL Server examines each row of the employee table in the outer query.

/* SQL Example 7.26 */

SELECT CAST(emp_last_name As CHAR(12)) "Last Name",

CAST(emp_first_name As CHAR(12)) "First Name",

emp_dpt_number "Dept",

'\$' + CONVERT (CHAR (10), emp_salary, 1) "Salary"

FROM employee e1

WHERE emp_salary =

(SELECT MAX(emp_salary)

FROM employee

WHERE emp_dpt_number = e1.emp_dpt_number);

• The subquery in this SELECT statement cannot be resolved independently of the main query.

• The inner query compares the employee department number column (emp_dpt_number) of the employee table to the same column for the alias table name e1.

• The subquery's results are correlated with each individual row of the main query – thus, the term correlated subquery.

• When a subquery uses the EXISTS operator, the subquery functions as an existencetest.

• The WHERE clause of the outer query tests for the existence of rows returned by the inner query.

• The subquery does not actually produce any data; rather, it returns a value of TRUE or FALSE.

• The general format of a subquery WHERE clause with an EXISTS operator is shown here.

• Note that the NOT operator can also be used to negate the result of the EXISTS operator.

WHERE [NOT] EXISTS (subquery)

• This correlated subquery also produces a listing of employees that have dependents.

/* SQL Example 7.27 */

SELECT CAST(emp_last_name As CHAR(12)) "Last Name",

CAST(emp_first_name As CHAR(12)) "First Name"

FROM employee

WHERE EXISTS

(SELECT *

FROM dependent

WHERE emp_ssn = dep_emp_ssn);

Last Name First Name

––––-------- ––––---------

Bock Douglas

Zhu Waiman

Joyner Suzanne

Bordoloi Bijoy

• The keyword EXISTS is not preceded by a column name, constant, or other expression.

• The parameter in the SELECT clause of a subquery that uses an EXISTS operator almost always consists of an asterisk (*). This is because there is no real point in listing any column name because you are simply testing for the existence of rows that meet the conditions specified in the subquery.

• The subquery evaluates to TRUE or FALSE instead of returning any data.

• A subquery that uses an EXISTS operator always is a correlated subquery.

• The EXISTS operator is very important, because there is often no alternative to its use.

• All queries that use the IN operator or a modified comparison operator (=, <, >, and so on, modified by ANY or ALL) can be expressed with the EXISTS operator.

• Use of the EXISTS operator often produces a LESS efficient query than one that can be written in some other way. The next slide compares two subqueries—the first is more efficient. Can you determine why?

/* SQL Example 7.29 */

SELECT emp_last_name

FROM employee

WHERE EXISTS

(SELECT *

FROM dependent

WHERE emp_ssn =

dep_emp_ssn);

EMP_LAST_NAME

----------------

Bock

Zhu

Joyner

/* SQL Example 7.28 */

SELECT emp_last_name

FROM employee

WHERE emp_ssn = ANY

(SELECT dep_emp_ssn

FROM dependent);

EMP_LAST_NAME

-------------

Bock

Zhu

Joyner

• The NOT EXISTS operator is the mirror-image of the EXISTS operator.

• A query that uses NOT EXISTS in the WHERE clause is satisfied if the subquery returns no rows.

• This SELECT statement orders the result table by emp_last_name, then emp_first_name.

• An ORDER BY clause must appear after the WHERE clause, and the subquery is considered to be part of the WHERE clause.

/* SQL Example 7.30 */

SELECT CAST(emp_last_name As CHAR(12)) "Last Name",

CAST(emp_first_name As CHAR(12)) "First Name"

FROM employee

WHERE EXISTS

(SELECT *

FROM dependent

WHERE emp_ssn = dep_emp_ssn)

ORDER BY emp_last_name, emp_first_name;

• A subquery is simply a query inside another query as an object of the outer query’s WHERE clause.

• The data type produced by the subquery must be join compatible with the expression of the WHERE clause of the outer query.

• The IN, NOT IN, ALL, and ANY operators can modify the effect of a comparison operator.

• Aggregate functions require a subquery to return a scalar result table.

• A correlated subquery is one where the inner query depends on values provided by the outer query.

• The EXISTS and NOT EXISTS operators test for row existence and return TRUE/FALSE values.

• The ORDER BY clause placement must be after the WHERE clause of the outer query.