Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prentice Hall © 20041 Chapter 7: Subquieres SQL for SQL Server Bijoy Bordoloi and Douglas Bock.

Similar presentations


Presentation on theme: "Prentice Hall © 20041 Chapter 7: Subquieres SQL for SQL Server Bijoy Bordoloi and Douglas Bock."— Presentation transcript:

1 Prentice Hall © 20041 Chapter 7: Subquieres SQL for SQL Server Bijoy Bordoloi and Douglas Bock

2 Prentice Hall © 20042 Objectives Learn the formal subquery definition and write a subquery.Learn the formal subquery definition and write a subquery. Learn the subquery restrictions.Learn the subquery restrictions. Use the IN operator when writing a subquery.Use the IN operator when writing a subquery. Nest subqueries at multiple levels.Nest subqueries at multiple levels. Use comparison operators.Use comparison operators. Use the ALL and ANY keywords.Use the ALL and ANY keywords. Write correlated subqueries including the EXISTS operator.Write correlated subqueries including the EXISTS operator. Use the ORDER BY clause in queries with a subquery.Use the ORDER BY clause in queries with a subquery.

3 Prentice Hall © 20043 Example Query /* 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.

4 Prentice Hall © 20044 Breaking a Query into Subtasks 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.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

5 Prentice Hall © 20045 Task 2: Writing the Subquery Add the query as a subquery in the WHERE clause.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

6 Prentice Hall © 20046 SUBQUERY 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.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.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.The criteria values for a WHERE clause are unknown at design time.

7 Prentice Hall © 20047 SUBQUERY TYPES There are three basic types of subqueries. We will study each of these in the remainder of this chapter.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.

8 Prentice Hall © 20048 SUBQUERY TYPES 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.

9 Prentice Hall © 20049 SUBQUERY – General Rules 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.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] )

10 Prentice Hall © 200410 Clause Restrictions The SELECT clause of a subquery must contain only one expression, only one aggregate function, or only one column name.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 value(s) returned by a subquery must be join compatible with the WHERE clause of the outer query.

11 Prentice Hall © 200411 Example of Join Compatibility 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

12 Prentice Hall © 200412 Data-Type Join Compatibility 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.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.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.Example: It would make no sense to compare the emp_ssn column to the dep_date_of_birth column.

13 Prentice Hall © 200413 Data-Type Join Compatibility Contd. SQL Server maps ANSI standard data types to T-SQL data types.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:Conversion is automatic among any of these ANSI numeric data types when making numeric comparisons: –int (integer) –smallint (small integer) –decimal –float

14 Prentice Hall © 200414 Data-Type Join Compatibility Contd. SQL Server does not make comparisons based on column names.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.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.

15 Prentice Hall © 200415 Other Restrictions There are additional restrictions for subqueries. The DISTINCT keyword cannot be used in subqueries that include a GROUP BY clause.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.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.A result table can only include columns from a table named in the outer query’s FROM clause.

16 Prentice Hall © 200416 SUBQUERIES AND THE IN Operator Here, a subquery produces a list of values for the subquery result table.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: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.The only difference in the use of the IN operator with subqueries is that the list does not consist of hard-coded values.

17 Prentice Hall © 200417 IN Operator Example /* 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 WHERE emp_ssn IN (SELECT dep_emp_ssn FROM dependent WHERE dep_gender 'M'); Last Name First Name ––––-------- ––––--------- Bock Douglas Zhu Waiman Joyner Suzanne

18 Prentice Hall © 200418 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.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

19 Prentice Hall © 200419 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.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

20 Prentice Hall © 200420 Comparing a Subquery to a Join Query The previous subquery can also be written as a join query.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

21 Prentice Hall © 200421 When to Use a Subquery vs. a Join Query 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.

22 Prentice Hall © 200422 The NOT IN Operator Like the IN operator, the NOT IN operator can take the result of a subquery as the operator object.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

23 Prentice Hall © 200423 The NOT IN Operator Contd. 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.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.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.

24 Prentice Hall © 200424 MULTIPLE LEVELS OF NESTING Subqueries may themselves contain subqueries.Subqueries may themselves contain subqueries. When the WHERE clause of a subquery has as its object another subquery, these are termed nested 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.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.Consider the problem of producing a listing of employees that worked more than 10 hours on the project named Order Entry.

25 Prentice Hall © 200425 Nested Subquery Example /* 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

26 Prentice Hall © 200426 Understanding Nested Subqueries In order to understand this subquery’s execution, begin your examination with the lowest subquery.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.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

27 Prentice Hall © 200427 Understanding Nested Subqueries Contd. 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.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_number IN (1); work_emp_ssn ––––-------- 999111111 999888888

28 Prentice Hall © 200428 Understanding Nested Subqueries Contd. Now, substitute these two social security numbers into the IN operator listing for the outer query in place of the subquery.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

29 Prentice Hall © 200429 SUBQUERIES AND COMPARISON OPERATORS The general form of the WHERE clause with a comparison operator is similar to that used thus far in the text.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.Note that the syntax for the subquery is to be enclosed by parentheses. WHERE (subquery)

30 Prentice Hall © 200430 SUBQUERIES AND COMPARISON OPERATORS Contd. 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.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.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. 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.

31 Prentice Hall © 200431 Subquery Errors SQL Example 7.16 violates the "single value" rule – this subquery returns multiple values for the emp_salary column.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.

32 Prentice Hall © 200432 Aggregate Functions and Comparison Operators The aggregate functions (AVG, SUM, MAX, MIN, and COUNT) always return a scalar result table.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.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.Consider a situation where a payroll manager needs an employee listing with a salary level greater than the average salary level for all employees.

33 Prentice Hall © 200433 AVG Aggregate Function /* 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

34 Prentice Hall © 200434 Comparison Operators Modified with the ALL or ANY Keywords The ALL and ANY keywords modify a comparison operator to allow an outer query to accept multiple values from a subquery.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 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.The general form of the WHERE clause for this type of query is shown here. WHERE WHERE [ALL | ANY] (subquery) [ALL | ANY] (subquery) Subqueries that use these keywords may also include GROUP BY and HAVING clauses.Subqueries that use these keywords may also include GROUP BY and HAVING clauses.

35 Prentice Hall © 200435 An ALL Keyword Example List all employees with a salary larger than the largest salary for an employee in dept 7.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

36 Prentice Hall © 200436 An ALL Keyword 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.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

37 Prentice Hall © 200437 The ANY Keyword The ANY keyword is not as restrictive as the ALL keyword.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. 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.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.

38 Prentice Hall © 200438 The ANY Keyword Example /* 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

39 Prentice Hall © 200439 An "= ANY" (Equal Any) Example The "= ANY" operator is exactly equivalent to the IN operator.The "= ANY" operator is exactly equivalent to the 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.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');

40 Prentice Hall © 200440 An "= ANY" (Equal Any) Example Cont. /* 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

41 Prentice Hall © 200441 A "!= ANY" (Not Equal Any) Example Contd. While the "= ANY" is identical to the IN operator, the "!= ANY" (not equal any) is not equivalent to the NOT IN operator.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".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);

42 Prentice Hall © 200442 A "!= ANY" (Not Equal Any) Example Contd. 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".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);

43 Prentice Hall © 200443 An Erroneous != ANY Subquery 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.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);

44 Prentice Hall © 200444 Solution – Use the NOT IN Operator 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

45 Prentice Hall © 200445 CORRELATED SUBQUERIES 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.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.Suppose management needs a listing of the most highly paid employee for each department.

46 Prentice Hall © 200446 A Correlated Subquery Example 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);

47 Prentice Hall © 200447 Correlated Subquery Explanation Cont. The subquery in this SELECT statement cannot be resolved independently of the main query.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 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.The subquery's results are correlated with each individual row of the main query – thus, the term correlated subquery.

48 Prentice Hall © 200448 Subqueries and the EXISTS Operator When a subquery uses the EXISTS operator, the subquery functions as an existence test.When a subquery uses the EXISTS operator, the subquery functions as an existence test. The WHERE clause of the outer query tests for the existence of rows returned by the inner query.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 subquery does not actually produce any data; rather, it returns a value of TRUE or FALSE.

49 Prentice Hall © 200449 Subqueries and the EXISTS Operator Contd. The general format of a subquery WHERE clause with an EXISTS operator is shown here.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.Note that the NOT operator can also be used to negate the result of the EXISTS operator. WHERE [NOT] EXISTS (subquery)

50 Prentice Hall © 200450 An EXISTS Operator Example This correlated subquery also produces a listing of employees that have dependents.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

51 Prentice Hall © 200451 Rules for Subqueries with the EXISTS Operator 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.

52 Prentice Hall © 200452 Other EXISTS Operator Facts The EXISTS operator is very important, because there is often no alternative to its use.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.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?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?

53 Prentice Hall © 200453 Comparing Two Subqueries /* SQL Example 7.28 */ SELECT emp_last_name FROM employee WHERE emp_ssn = ANY (SELECT dep_emp_ssn (SELECT dep_emp_ssn FROM dependent); FROM dependent); EMP_LAST_NAME-------------BockZhuJoyner /* SQL Example 7.29 */ SELECT emp_last_name FROM employee WHERE EXISTS (SELECT * (SELECT * FROM dependent FROM dependent WHERE emp_ssn = WHERE emp_ssn = dep_emp_ssn); dep_emp_ssn); EMP_LAST_NAME----------------BockZhuJoyner

54 Prentice Hall © 200454 Subqueries and the NOT EXISTS Operator The NOT EXISTS operator is the mirror-image of the EXISTS operator.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.A query that uses NOT EXISTS in the WHERE clause is satisfied if the subquery returns no rows.

55 Prentice Hall © 200455 Subqueries and the ORDER BY Clause This SELECT statement orders the result table by emp_last_name, then emp_first_name.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.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;

56 Prentice Hall © 200456 Summary A subquery is simply a query inside another query as an object of the outer query’s WHERE clause.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 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.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.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.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 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.The ORDER BY clause placement must be after the WHERE clause of the outer query.


Download ppt "Prentice Hall © 20041 Chapter 7: Subquieres SQL for SQL Server Bijoy Bordoloi and Douglas Bock."

Similar presentations


Ads by Google