580 likes | 954 Views
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.
E N D
SQL for SQL ServerBijoy Bordoloi and Douglas Bock Chapter 7: Subquieres Prentice Hall © 2004
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. • Use the ALL and ANY keywords. • Write correlated subqueries including the EXISTS operator. • Use the ORDER BY clause in queries with a subquery. Prentice Hall © 2004
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. Prentice Hall © 2004
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. /* SQL Example 7.2 */ SELECT MIN(emp_salary) "Min Salary" FROM employee; Min Salary -------------- 25000.0000 Prentice Hall © 2004
Task 2: Writing the Subquery • 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 Prentice Hall © 2004
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. • 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. Prentice Hall © 2004
SUBQUERY TYPES • 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. Prentice Hall © 2004
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. Prentice Hall © 2004
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. (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] ) Prentice Hall © 2004
Clause Restrictions • 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. Prentice Hall © 2004
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 Prentice Hall © 2004
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. • 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. Prentice Hall © 2004
Data-Type Join Compatibility Contd. • 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 Prentice Hall © 2004
Data-Type Join Compatibility Contd. • 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. Prentice Hall © 2004
Other Restrictions 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. Prentice Hall © 2004
SUBQUERIES AND THE IN Operator • 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. Prentice Hall © 2004
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 WHEREemp_ssn IN (SELECT dep_emp_ssn FROM dependent WHERE dep_gender 'M'); Last Name First Name ––––-------- ––––--------- Bock Douglas Zhu Waiman Joyner Suzanne Prentice Hall © 2004
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 Prentice Hall © 2004
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 Prentice Hall © 2004
Comparing a Subquery to 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 Prentice Hall © 2004
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. Prentice Hall © 2004
The NOT IN Operator • 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 Prentice Hall © 2004
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. • 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. Prentice Hall © 2004
MULTIPLE LEVELS OF NESTING • 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. Prentice Hall © 2004
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 Prentice Hall © 2004
Understanding Nested Subqueries • 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 Prentice Hall © 2004
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. /* SQL Example 7.13 */ SELECT work_emp_ssn FROM assignment WHERE work_hours > 10 AND work_pro_numberIN (1); work_emp_ssn ––––-------- 999111111 999888888 Prentice Hall © 2004
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. /* 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 Prentice Hall © 2004
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. • Note that the syntax for the subquery is to be enclosed by parentheses. WHERE <expression> <comparison_operator> (subquery) Prentice Hall © 2004
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. • 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. Prentice Hall © 2004
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 */ 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. Prentice Hall © 2004
Aggregate Functions and Comparison Operators • 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. Prentice Hall © 2004
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 Prentice Hall © 2004
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 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. Prentice Hall © 2004
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 Prentice Hall © 2004
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 Prentice Hall © 2004
The ANY 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. • 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. Prentice Hall © 2004
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 Prentice Hall © 2004
An "= ANY" (Equal Any) Example • 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'); Prentice Hall © 2004
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 Prentice Hall © 2004
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. • 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); Prentice Hall © 2004
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". WHERE != ANY (38000, 43000, 55000); Prentice Hall © 2004
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. /* 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); Prentice Hall © 2004
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 Prentice Hall © 2004
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. • Suppose management needs a listing of the most highly paid employee for each department. Prentice Hall © 2004
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); Prentice Hall © 2004
Correlated Subquery Explanation Cont. • 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. Prentice Hall © 2004
Subqueries and the EXISTS Operator • 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. Prentice Hall © 2004
Subqueries and the EXISTS Operator Contd. • 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) Prentice Hall © 2004
An EXISTS Operator Example • 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 Prentice Hall © 2004