Tài liệu Oracle SQL Jumpstart with Examples- P7 pdf

50 342 0
Tài liệu Oracle SQL Jumpstart with Examples- P7 pdf

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

270 12.4 Demonstrating Subqueries A subquery itself is generally syntactically equivalent to a SELECT state- ment. Chapters 4, 5, 6, and 11 apply to subqueries in this respect. So far in this chapter, we have seen a lot of information. The easiest way to explain subqueries is simply to demonstrate. 12.4 Demonstrating Subqueries This section demonstrates use of the different types of subqueries:  Single-row subqueries.  Multiple-row subqueries.  Multiple-column subqueries.  Regular versus correlated subqueries.  Nested subqueries.  Inline views or FROM clause embedded subqueries.  Subqueries can be used in numerous SQL code commands and their subset clauses. 12.4.1 Single-Row Subqueries A single-row subquery is exactly as its name implies: a subquery that returns a single row. If more than one row is returned, an error will result (ORA- 01427: single-row subquery returns more than one row). Simple (equality), LIKE, and Range (BETWEEN) comparison conditions are restricted to single-row subquery results. See the syntax diagram in Figure 12.1. Figure 12.1 Subquery Comparison Condition Syntax. Chap12.fm Page 270 Thursday, July 29, 2004 10:09 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 12.4 Demonstrating Subqueries 271 Chapter 12 Here is an easy way of understanding the concept of the single-row sub- query. You can ask if “Apple Pie” equals “Apple Pie,” but you cannot ask if “Apple Pie” is equal to both “Apple Pie” and “Pumpkin Pie” because you get two different answers at once. Apple pie is equal to apple pie but not equal to pumpkin pie. The same applies to testing for a number, say 10, being BETWEEN five other numbers because it does not make sense. For exam- ple, 10 BETWEEN 5 AND (20, 4, 30) cannot be evaluated because it is both true and false. The same applies to the LIKE clause because a single LIKE comparison condition can only be used to match a single pattern, not many patterns. Following is an example of a single-row subquery. The ROWNUM pseudocolumn is used to restrict the subquery to a single row no matter how many rows it returns. See the result in Figure 12.2. SELECT SONG_ID, GUESTARTIST_ID, INSTRUMENT_ID FROM INSTRUMENTATION WHERE INSTRUMENT_ID = (SELECT INSTRUMENT_ID FROM INSTRUMENT WHERE NAME = 'Acoustic Guitar'); In the next example, the query in Figure 12.2 is altered to ensure that multiple rows are returned from the subquery. Removing the WHERE clause filter from the query in Figure 12.2 results in an error, as shown in Figure 12.3. The subquery in Figure 12.3 returns all rows in the INSTRU- MENT table. Figure 12.2 A Single-Row Subquery. Chap12.fm Page 271 Thursday, July 29, 2004 10:09 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 272 12.4 Demonstrating Subqueries 12.4.2 Multiple-Row Subqueries A multiple-row subquery returns multiple rows. The IN, EXISTS, and Group (ANY, ALL, SOME) comparison conditions allow multiple-row subquery results. See the syntax diagram in Figure 12.1.  A multiple-row subquery can provide the set of values needed for the IN comparison condition.  The EXISTS comparison condition usually uses indexes to match values in the subquery to values in the calling query. Regardless of correlated indexed columns between calling and subquery, EXISTS will stop execution of the subquery when the appropriate value is found. IN will build all values in the set for the subquery before pass- ing its result back to the calling query. Using EXISTS rather than IN often results in better performance of the query. EXISTS may not perform better than IN when the set produced by the subquery is a limited set of literal values or a very small number of rows.  ANY, ALL, and SOME imply any value, all values, and some values, respectively. Because these subquery comparison conditions test against a set of values, a multiple-row query can in reality return zero, one, or many rows. Note: It is important to note that a multiple-row subquery can return zero rows because the Membership, Exists, and Group comparison conditions return a set of values. That set of values can be an empty set. An empty set is a valid set. Figure 12.3 A Single-Row Subquery Returning More Than One Row Returns an Error. Chap12.fm Page 272 Thursday, July 29, 2004 10:09 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 12.4 Demonstrating Subqueries 273 Chapter 12 Following are some multiple-row subquery examples, perhaps allowing for a better understanding of multiple-row subquery comparison conditions. This query returns the names of all instruments in the INSTRUMENT table that are used by artists doing guest appearances. The subquery is a reg- ular (noncorrelated) multiple-row subquery using the IN comparison con- dition. The result is shown in Figure 12.4. SELECT NAME FROM INSTRUMENT WHERE INSTRUMENT_ID IN (SELECT INSTRUMENT_ID FROM INSTRUMENTATION); This query returns the name of instruments played when ARTIST_ID of 1 made a guest appearance. Because ARTIST_ID 1 made no guest appearances on any songs, no rows are returned by the subquery. This shows that a subquery returning a NULL set of rows is valid. The subquery is a regular, multiple-row subquery using the IN comparison condition. The result is shown in Figure 12.5. SELECT NAME FROM INSTRUMENT WHERE INSTRUMENT_ID IN (SELECT INSTRUMENT_ID FROM INSTRUMENTATION WHERE GUESTARTIST_ID = 1); Figure 12.4 The IN Comparison Condition. Chap12.fm Page 273 Thursday, July 29, 2004 10:09 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 274 12.4 Demonstrating Subqueries This query lists artists who never made guest appearances on any songs. The subquery is a correlated multiple-row subquery and uses the NOT EXISTS comparison condition. The result is shown in Figure 12.6. SELECT NAME FROM ARTIST A WHERE NOT EXISTS (SELECT GA.GUESTARTIST_ID FROM GUESTAPPEARANCE GA WHERE GA.GUESTARTIST_ID = A.ARTIST_ID); This query returns the names of artists who recorded songs before May 1, 2001. The subquery is a regular multiple-row subquery using the ANY comparison condition. If you want to list the recording date in your query results, you must use a join or a FROM clause subquery. The result is shown in Figure 12.7. SELECT NAME FROM ARTIST A WHERE A.ARTIST_ID = ANY (SELECT S.ARTIST_ID FROM SONG S WHERE S.RECORDING_DATE < '01-MAY-2001'); This query returns the titles of CDs that have songs with a guest appear- ance. The subquery is a regular multiple-row subquery using the SOME comparison condition (SOME is identical to ANY). The result is shown in Figure 12.8. Figure 12.5 The IN Comparison Condition with No Rows Returned in the Subquery. Chap12.fm Page 274 Thursday, July 29, 2004 10:09 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 12.4 Demonstrating Subqueries 275 Chapter 12 SELECT DISTINCT M.TITLE FROM MUSICCD M JOIN CDTRACK CT ON (M.MUSICCD_ID = CT.MUSICCD_ID) WHERE CT.SONG_ID = SOME (SELECT SONG_ID FROM GUESTAPPEARANCE GA); Figure 12.6 NOT EXISTS with a Correlated Subquery. Figure 12.7 = ANY with a Subquery. Chap12.fm Page 275 Thursday, July 29, 2004 10:09 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 276 12.4 Demonstrating Subqueries This example returns the names of artists who have not been in the stu- dio after January 1, 2000. The subquery is a correlated multiple-row sub- query using the ALL comparison condition. Note: Note: If you want to list the session date in your query results, you must use a join or a FROM clause subquery (inline view) instead of a sub- query in the WHERE clause. The result is shown in Figure 12.9. SELECT A.NAME FROM ARTIST A WHERE '01-JAN-2000' > ALL (SELECT ST.SESSION_DATE FROM STUDIOTIME ST WHERE ST.ARTIST_ID = A.ARTIST_ID); 12.4.3 Multiple-Column Subqueries A multiple-column subquery can return a single or multiple rows. It simply returns more than one column for each row. Typically, a multiple-column subquery is used to validate a set of columns against another set of columns in a WHERE clause or as a tuned FROM clause row filter (inline view), as shown in the two examples following. The first example following uses the IN set membership comparison to find a row set of two columns from the ARTIST table where the name of the Figure 12.8 = SOME with a Subquery. Chap12.fm Page 276 Thursday, July 29, 2004 10:09 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 12.4 Demonstrating Subqueries 277 Chapter 12 artist contains a lowercase letter “u”. See the result in Figure 12.10. Notice that the subquery SELECT clause contains two columns. Notice also that the calling query WHERE clause filter has a list, in parentheses, of two columns that are to be compared to the two columns returned by the subquery. SELECT A.ARTIST_ID, A.NAME, S.TITLE FROM ARTIST A, SONG S WHERE (A.ARTIST_ID, A.NAME) IN (SELECT ARTIST_ID, NAME FROM ARTIST WHERE NAME LIKE '%u%') AND A.ARTIST_ID = S.ARTIST_ID; The next and second example of a multiple-column subquery will pro- duce the same result as shown in Figure 12.10. In Figure 12.11, an element of the FROM clause contains the same subquery as in the first example in Figure 12.10. Note: This example is better than the previous one for a very large ARTIST table because the filter is executed before the join of the ARTIST rows with the SONG rows. The query will perform better because the join occurs on a smaller number of rows. 1 Figure 12.9 > ALL with a Subquery. Chap12.fm Page 277 Thursday, July 29, 2004 10:09 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 278 12.4 Demonstrating Subqueries SELECT A.ARTIST_ID, A.NAME, S.TITLE FROM SONG S, (SELECT ARTIST_ID, NAME FROM ARTIST WHERE NAME LIKE '%u%') A WHERE A.ARTIST_ID = S.ARTIST_ID; Figure 12.10 The WHERE Clause Contains a Multiple-Column Subquery. Figure 12.11 The FROM Clause Contains a Multiple-Column Subquery. Chap12.fm Page 278 Thursday, July 29, 2004 10:09 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 12.4 Demonstrating Subqueries 279 Chapter 12 12.4.4 Regular versus Correlated Subqueries This section discusses the pros and cons of using regular or correlated sub- queries. A correlated subquery allows the correlation or matching of a column between a calling query and a subquery. The calling query can pass an aliased column name into the subquery, not the other way around. Queries are parsed from left to right and from top to bottom. The SQL parser will not understand what to do with an attempt to pass a column alias from bottom to top and will produce a syntax (SQL parse) error. A subquery is parsed and executed before its calling query or subquery. For example, the following query has a SELECT clause that references a column from a cor- related subquery found in the WHERE clause. The following query passes the ARTIST_ID column value from the calling query into the subquery, matching each ARTIST table row with related STUDIOTIME table rows. SELECT A.NAME FROM ARTIST A WHERE '01-JAN-2000' > ALL (SELECT ST.SESSION_DATE FROM STUDIOTIME ST WHERE ST.ARTIST_ID = A.ARTIST_ID); The most common use for correlated subqueries is using the EXISTS comparison condition as in the script shown following. The ARTIST_ID column value is passed from the calling query into the subquery. A correla- Figure 12.12 Values Can Be Passed from a Calling Query into a Correlated Subquery. Chap12.fm Page 279 Thursday, July 29, 2004 10:09 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... and examples on Oracle Partitioning and parallel queries can be found in my other book, Oracle Performance Tuning for 9i and 10g (ISBN 1-55558-305-9) Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 13.5 Endnotes 299 That completes this chapter on composite, hierarchical, flashback, and parallel queries The next chapter examines Oracle Expressions, new to Oracle Database... 13.5 Endnotes 1 Oracle Performance Tuning for 9i and 10g (ISBN: 1-55558-305-9) Chapter 13 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark This page intentionally left blank Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 14 Expressions In this chapter: How can expressions be classified? What are regular expressions? What is the Oracle Expression... statement can be used as an inline expression within an SQL query, similar to an IF-THEN-ELSE programming construct The syntax for an inline CASE statement is as Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 14.1 Types of Expressions 303 Figure 14.1 SQL Inline CASE Statement Syntax shown in Figure 14.1 See Chapter 24 for the PL /SQL version of the CASE statement Chapter 9... SELECT GENRE_ID, GENRE FROM GENRE MINUS SELECT * FROM GENRES; Chapter 13 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 288 13.1 Composite Queries Figure 13.2 Removing Duplicates with UNION Figure 13.3 INTERSECT Returns Rows Common to Both Queries Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 13.2 Hierarchical Queries 289 Figure 13.4 MINUS Returns... parallel queries Figure 12.15 Subqueries in INSERT and UPDATE Statements Chapter 12 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 284 12.5 Endnotes 12.5 Endnotes 1 Oracle Performance Tuning for 9i and 10g (ISBN: 1-55558-305-9) Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 13 Unusual Query Types In this chapter: What is a composite query... interested in for this chapter are regular expressions and the Oracle Expression Filter Let’s start with regular expressions 14.2 Regular Expressions Oracle regular expressions conform to and enhance POSIX1 standards, much like regular expression support in a programming language such as Perl There are two parts to using regular expressions in Oracle SQL The first part is the functions that execute regular... commands can be used with the following syntax: { CREATE | ALTER } TABLE … [ NOPARALLEL | PARALLEL [n] ]; Thus the SALES table could be altered to have a degree of parallelism of 2 with the following command: ALTER TABLE SALES PARALLEL 2; Note: No parallel query examples are given in this book, because parallel queries tend to execute with improved performance only when using Oracle Partitioning Further... words, pairs can be matched and returned where those pairs are not directly related within a hierarchy but related from the top to the bottom of a hierarchy Chapter 13 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 290 13.2 Hierarchical Queries 13.2.1 Hierarchical Query Operators PRIOR Used with the CONNECT BY condition evaluating the subsequent expression for each parent... this first example, PRIOR is used with the CONNECT BY condition evaluating the subsequent expression for each parent row of each current row, using a current row column to hook into a parent row column Figure 13.5 shows the result The START WITH modifier simply begins at a specific point within the hierarchy SELECT INSTRUMENT_ID, NAME, SECTION_ID, LEVEL FROM INSTRUMENT START WITH INSTRUMENT_ID = 10 CONNECT... WHERE CONTINENT_ID IN (SELECT CONTINENT_ID FROM CONTINENT); 301 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 302 14.1 Types of Expressions Brackets, or, as mathematically termed, parentheses, are used to change the sequence of evaluation within expressions, effectively creating expressions within expressions The sequence of evaluation is called precedence See Chapter . Condition with No Rows Returned in the Subquery. Chap12.fm Page 274 Thursday, July 29, 2004 10:09 PM Please purchase PDF Split-Merge on www.verypdf.com to. NOT EXISTS with a Correlated Subquery. Figure 12.7 = ANY with a Subquery. Chap12.fm Page 275 Thursday, July 29, 2004 10:09 PM Please purchase PDF Split-Merge

Ngày đăng: 24/12/2013, 12:17

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan