Consider the following queries: Query 1: select * from jobs cross join job_history; Query 2: select * from jobs j cross join job_history jh where j.job_id='AD_PRES'; Query 1 takes the 19
Trang 1SELECT table1.column, table2.column
FROM table1
CROSS JOIN table2;
It is important to observe that no join condition is specified using the ON or USING keyword A Cartesian product associates the rows from table1 with every row in table2
Conditions that limit the results are permitted in the form of WHERE clause
restrictions If table1 and table2 contain x and y number of rows, respectively, the Cartesian product will contain x times y number of rows The results from a cross join
may be used to identify orphan rows or generate a large dataset for use in application testing Consider the following queries:
Query 1: select * from jobs cross join job_history;
Query 2: select * from jobs j cross join job_history jh
where j.job_id='AD_PRES';
Query 1 takes the 19 rows and 4 columns from the JOBS table and the 10 rows and 5 columns from the JOB_HISTORY table and generates one large set of 190 records with 9 columns SQL*Plus shows all instances of identically named columns
as separate headings labeled with the column name (unless they are aliased) SQL Developer appends an underscore and number to each shared column name and uses it as the heading The JOB_ID column is common to both the JOBS and JOB_ HISTORY tables The headings in SQL Developer are labeled JOB_ID and JOB_ID_1, respectively Query 2 generates the same Cartesian product as the first, but the 190 rows are constrained by the WHERE clause condition and only 10 rows are returned
Figure 12-13 shows a cross join between the REGIONS and COUNTRIES tables
There are 4 rows in REGIONS and 25 rows in COUNTRIES Since the WHERE clause limits the REGIONS table to 2 of 4 rows, the Cartesian product produces 50 (25 × 2) records The results are sorted alphabetically, first on the REGION_NAME and then on the COUNTRY_NAME The first record has the pair of values, Asia and Argentina When the REGION_NAME changes, the first record has the pair of values, Africa and Argentina Notice that the COUNTRY_NAME values are repeated for every REGION_NAME
EXAM TIP When using the cross join syntax, a Cartesian product is
intentionally generated Inadvertent Cartesian products are created when there are insufficient join conditions in a statement Joins that specify fewer
than N – 1 join conditions when joining N tables or that specify invalid join
conditions may inadvertently create Cartesian products A pure natural join
between two tables sharing no identically named columns results in a Cartesian join, since two tables are joined but less than one condition
is available
Trang 2Exercise 12-4: Work with Joins Using SQL Developer or SQL*Plus, connect
to the WEBSTORE schema and produce a report of customers who purchased the 11G
All-in-One Guide (PRODUCT_ID=2) The report must contain the customer’s name,
the product description, and the quantity ordered There are several approaches to
solving this question Your approach may differ from this solution
1 Start SQL*Plus and connect to the WEBSTORE schema
Figure 12-13 The cross join
Trang 3Two-Minute Drill
Write SELECT Statements to Access Data from More Than One Table Using Equijoins and Nonequijoins
• Equijoining occurs when one query fetches column values from multiple tables in which the rows fulfill an equality-based join condition
• A pure natural join is performed using the NATURAL JOIN syntax when the source and target tables are implicitly equijoined using all identically named columns
• The JOIN USING syntax allows a natural join to be formed on specific columns with shared names
Trang 4• Dot notation refers to qualifying a column by prefixing it with its table name
and a dot or period symbol This designates the table a column originates
from and differentiates it from identically named columns from other tables
• The JOIN ON clause allows the explicit specification of join columns
regardless of their column names This provides a flexible joining format
• The ON, USING, and NATURAL keywords are mutually exclusive and
therefore cannot appear together in a join clause
• A nonequijoin is performed when the values in the join columns fulfill the
join condition based on an inequality operator
Join a Table to Itself Using a Self-Join
• A self-join is required when the join columns originate from the same table
Conceptually, the source table is duplicated and a target table is created The
self-join then works as a regular join between two discrete tables
• Storing hierarchical data in a relational table requires a minimum of two
columns per row One column stores an identifier of the row’s parent record,
and the second stores the row’s identifier
View Data That Does Not Meet a Join Condition
Using Outer Joins
• When equijoins and nonequijoins are performed, rows from the source and
target tables are matched These are referred to as inner joins
• An outer join is performed when rows that are not retrieved by an inner join
are included for retrieval
• A left outer join between the source and target tables returns the results of an
inner join and the missing rows it excluded from the source table
• A right outer join between the source and target tables returns the results of an
inner join and the missing rows it excluded from the target table
• A full outer join returns the combined results of a left outer join and a right
outer join
Generate a Cartesian Product of Two or More Tables
• A Cartesian product is sometimes called a cross join and refers to the set of
data created by merging the rows from two or more tables with each other
• The count of the rows returned from a Cartesian product is equal to the
number of rows in the source table multiplied by the number of rows in the
target table
• Joins that specify fewer than N – 1 join conditions when joining N tables, or
that specify invalid join conditions, inadvertently create Cartesian products
Trang 5A SELECT * FROM EMPLOYEES NATURAL JOIN DEPARTMENTS;
B SELECT * FROM EMPLOYEES E NATURAL JOIN DEPARTMENTS D ON E.DEPARTMENT_ID=D.DEPARTMENT_ID;
C SELECT * FROM EMPLOYEES NATURAL JOIN DEPARTMENTS USING (DEPARTMENT_ID);
D None of the above
2 The EMPLOYEES and DEPARTMENTS tables have two identically named columns: DEPARTMENT_ID and MANAGER_ID Which statements join
these tables based on both column values? (Choose all that apply.)
A SELECT * FROM EMPLOYEES NATURAL JOIN DEPARTMENTS;
B SELECT * FROM EMPLOYEES JOIN DEPARTMENTS USING (DEPARTMENT_ID,MANAGER_ID);
C SELECT * FROM EMPLOYEES E JOIN DEPARTMENTS D ON E.DEPARTMENT_ID=D.DEPARTMENT_ID AND E.MANAGER_ID=D MANAGER_ID;
D None of the above
3 Which join is performed by the following query?
SELECT E.JOB_ID ,J.JOB_ID FROM EMPLOYEES E JOIN JOBS J ON (E.SALARY < J.MAX_SALARY);
(Choose the best answer.)
A Equijoin
B Nonequijoin
C Cross join
D Outer join
4 Which of the following statements are syntactically correct? (Choose all that apply.)
A SELECT * FROM EMPLOYEES E JOIN DEPARTMENTS D USING (DEPARTMENT_ID);
B SELECT * FROM EMPLOYEES JOIN DEPARTMENTS D USING (D.DEPARTMENT_ID);
Trang 6C SELECT D.DEPARTMENT_ID FROM EMPLOYEES JOIN DEPARTMENTS D
USING (DEPARTMENT_ID);
D None of the above
5 Which of the following statements are syntactically correct? (Choose all that
apply.)
A SELECT E.EMPLOYEE_ID, J.JOB_ID PREVIOUS_JOB, E.JOB_ID
CURRENT_JOB FROM JOB_HISTORY J CROSS JOIN EMPLOYEES E ON
(J.START_DATE=E.HIRE_DATE);
B SELECT E.EMPLOYEE_ID, J.JOB_ID PREVIOUS_JOB, E.JOB_ID
CURRENT_JOB FROM JOB_HISTORY J JOIN EMPLOYEES E ON
(J.START_DATE=E.HIRE_DATE);
C SELECT E.EMPLOYEE_ID, J.JOB_ID PREVIOUS_JOB, E.JOB_ID
CURRENT_JOB FROM JOB_HISTORY J OUTER JOIN EMPLOYEES E ON
(J.START_DATE=E.HIRE_DATE);
D None of the above
6 Choose one correct statement regarding the following query:
SELECT * FROM EMPLOYEES E
JOIN DEPARTMENTS D ON (D.DEPARTMENT_ID=E.DEPARTMENT_ID)
JOIN LOCATIONS L ON (L.LOCATION_ID =D.LOCATION_ID);
A Joining three tables is not permitted
B A Cartesian product is generated
C The JOIN ON clause may be used for joins between multiple tables
D None of the above
7 How many rows are returned after executing the following statement?
SELECT * FROM REGIONS R1 JOIN REGIONS R2 ON (R1.REGION_
ID=LENGTH(R2.REGION_NAME)/2);
The REGIONS table contains the following row data (Choose the best answer.)
1 Europe
2 Americas
3 Asia
4 Middle East and Africa
A 2
B 3
C 4
D None of the above
Trang 7B The rows returned represent those locations that have no COUNTRY_ID.
C The rows returned represent the COUNTRY_ID values for all the rows in the LOCATIONS table
D None of the above
9 Which of the following statements are syntactically correct? (Choose all that apply.)
A SELECT JH.JOB_ID FROM JOB_HISTORY JH RIGHT OUTER JOIN JOBS J
ON JH.JOB_ID=J.JOB_ID
B SELECT JOB_ID FROM JOB_HISTORY JH RIGHT OUTER JOIN JOBS J ON (JH.JOB_ID=J.JOB_ID)
C SELECT JOB_HISTORY.JOB_ID FROM JOB_HISTORY OUTER JOIN JOBS
ON JOB_HISTORY.JOB_ID=JOBS.JOB_ID
D None of the above
10 If the REGIONS table, which contains 4 rows, is cross joined to the COUNTRIES
table, which contains 25 rows, how many rows appear in the final results set? (Choose the best answer.)
A 100 rows
B 4 rows
C 25 rows
D None of the above
Self Test Answers
1 þ D The queries in B and C incorrectly contain the NATURAL keyword If
this is removed, they will join the DEPARTMENTS and EMPLOYEES tables based on the DEPARTMENT_ID column
ý A, B, and C A performs a pure natural join that implicitly joins the
two tables on all columns with identical names, which, in this case, are DEPARTMENT_ID and MANAGER_ID
2 þ A, B, and C These clauses demonstrate different techniques to join the
tables on both the DEPARTMENT_ID and MANAGER_ID columns
ý D.
Trang 83 þ B The join condition is an expression based on the less than inequality
operator Therefore, this join is a nonequijoin
ý A, C, and D A would be correct if the operator in the join condition
expression was an equality operator The CROSS JOIN keywords or the
absence of a join condition would result in C being true D would be
true if one of the OUTER JOIN clauses was used instead of the JOIN
ON clause
4 þ A This statement demonstrates the correct usage of the JOIN USING
clause
ý B, C, and D B is incorrect because only nonqualified column names are
allowed in the brackets after the USING keyword C is incorrect because the
column in brackets after the USING keyword cannot be referenced with a
qualifier in the SELECT clause
5 þ B demonstrates the correct usage of the JOIN ON clause.
ý A, C, and D A is incorrect since the CROSS JOIN clause cannot contain
the ON keyword C is incorrect since the OUTER JOIN keywords must be
preceded by the LEFT, RIGHT, or FULL keyword
6 þ C The JOIN ON clause and the other join clauses may all be used for
joins between multiple tables The JOIN ON and JOIN USING clauses
are better suited for N-way table joins
ý A, B, and D A is false, since you may join as many tables as you wish
A Cartesian product is not created, since there are two join conditions and
three tables
7 þ B Three rows are returned For the row with a REGION_ID value of 2,
the REGION_NAME is Asia and half the length of the REGION_NAME is
also 2 Therefore this row is returned The same logic results in the rows with
REGION_ID values of 3 and 4 and REGION_NAME values of Europe and
Americas being returned
ý A, C, and D.
8 þ A The right outer join fetches the COUNTRY.COUNTRY_ID values that
do not exist in the LOCATIONS table in addition to performing an inner
join between the tables The WHERE clause then eliminates the inner join
results The rows remaining represent those countries for which there are no
locations
ý B, C, and D.
Trang 910 þ A The cross join associates every four rows from the REGIONS table
25 times with the rows from the COUNTRIES table, yielding a result set that contains 100 rows
ý B, C, and D.
Trang 10CHAPTER 13
Subqueries and Set Operators
Exam Objectives
In this chapter you will learn to
• 051.7.1 Define Subqueries
• 051.7.2 Describe the Types of Problems That the Subqueries Can Solve
• 051.7.3 List the Types of Subqueries
• 051.7.4 Write Single-Row and Multiple-Row Subqueries
• 051.8.1 Describe Set Operators
• 051.8.2 Use a Set Operator to Combine Multiple Queries into a Single Query
• 051.8.3 Control the Order of Rows Returned
515