Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-1 CHAPTER 6: JOINS.

Similar presentations


Presentation on theme: "Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-1 CHAPTER 6: JOINS."— Presentation transcript:

1 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-1 CHAPTER 6: JOINS

2 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-2 A TYPICAL JOIN OPERATION The following figure displays the employee and department tables.The following figure displays the employee and department tables. The figure illustrates the concept of a JOIN operation by connecting columns within each table with a line.The figure illustrates the concept of a JOIN operation by connecting columns within each table with a line. One line connects the employee table's emp_dpt_number column with the department table's dpt_no column.One line connects the employee table's emp_dpt_number column with the department table's dpt_no column. A second line connects the employee table's emp_ssn column to the department table's dep_mgrssn column.A second line connects the employee table's emp_ssn column to the department table's dep_mgrssn column.

3 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-3 A TYPICAL JOIN OPERATION

4 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-4 JOINS We will begin our study of JOIN operations by focusing on the relationship between the employee and department tables represented by the common department number values.We will begin our study of JOIN operations by focusing on the relationship between the employee and department tables represented by the common department number values. Our first query lists employee names and department numbers. This query only retrieves data from the single employee table.Our first query lists employee names and department numbers. This query only retrieves data from the single employee table.

5 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-5 JOINS SELECT emp_last_name "Last Name", emp_first_name "First Name", emp_dpt_number "Department" emp_dpt_number "Department" FROM employee; Last Name First Name Department ------------- -------------- -------------- Bordoloi Bijoy 1 Joyner Suzanne 3 Zhu Waiman 7 more rows will be displayed...

6 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-6 JOINS A large organization can have dozens or even hundreds of departments. Thus, the numbers displayed in the department column shown above may not be very meaningful.A large organization can have dozens or even hundreds of departments. Thus, the numbers displayed in the department column shown above may not be very meaningful. Suppose you want the department names instead of the department numbers to be listed.Suppose you want the department names instead of the department numbers to be listed. The department names are mentioned in the “Department” table.The department names are mentioned in the “Department” table. Hence we need to join the “Employee” and the “Department” tables to get the required results.Hence we need to join the “Employee” and the “Department” tables to get the required results.

7 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-7 JOINS SELECT emp_last_name "Last Name", emp_first_name "First Name", emp_first_name "First Name", dpt_name "Department Name" dpt_name "Department Name" FROM employee, department WHERE employee.emp_dpt_number = department.dpt_no; Last Name First Name Department Name ------------- ------------- ---------------------- Bordoloi Bijoy Headquarters Joyner Suzanne Admin and Records Zhu Waiman Production more rows will be displayed...

8 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-8 JOINS The following table shows two tables simply named Table_1 and Table_2.The following table shows two tables simply named Table_1 and Table_2. Each table has a single column named Col_1. Each table also has three rows with simple alphabetic values stored in the Col_1 column.Each table has a single column named Col_1. Each table also has three rows with simple alphabetic values stored in the Col_1 column. Table_1 Table_2 Table_1 Table_2 COL_1 COL_1 a b c a b c

9 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-9JOINS SELECT * FROM table_1, table_2; COL_1 COL_1 -------- --------- a a b a c a a b b b c b a c b c cc

10 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-10JOINS The first row of the “table_1” table was joined with every row in the “table_2” table.The first row of the “table_1” table was joined with every row in the “table_2” table. A Cartesian product may not be useful and could be misleading.A Cartesian product may not be useful and could be misleading. Always include a WHERE clause in your JOIN statements.Always include a WHERE clause in your JOIN statements. SELECT * FROM table_1, table_2; WHERE table_1.col_1 = table_2.col_1; col_1 col_1 col_1 col_1 -------- -------- -------- -------- a a a a b b b b c c c c

11 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-11 JOIN OPERATION RULES JOINS and the SELECT Clause A JOIN query always begins with a SELECT clause.A JOIN query always begins with a SELECT clause. List the columns to be displayed in the result table after the SELECT keyword.List the columns to be displayed in the result table after the SELECT keyword. The result table column order reflects the order in which column names are listed in the SELECT clause.The result table column order reflects the order in which column names are listed in the SELECT clause. To modify the order in which column names are listed, simply rearrange the order of the column listing in the SELECT clause.To modify the order in which column names are listed, simply rearrange the order of the column listing in the SELECT clause.

12 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-12Example SELECT dpt_name "Department Name", emp_last_name "Last Name", emp_last_name "Last Name", emp_first_name "First Name" emp_first_name "First Name" FROM employee e, department d WHERE e.emp_dpt_number = d.dpt_no AND e.emp_dpt_number = 7; Department Name Last Name First Name ---------------------- -------------- --------------- Production Zhu Waiman Production Bock Douglas Production Joshi Dinesh Production Prescott Sherri

13 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-13 JOIN OPERATION RULES JOIN operations also support the specification of all columns by the use of a simple asterisk (*) in a SELECT clause.JOIN operations also support the specification of all columns by the use of a simple asterisk (*) in a SELECT clause. The result table for the query will contain all columns from both of the tables.The result table for the query will contain all columns from both of the tables. When the asterisk (*) is used, the column order of the result table is based on the order in which tables are listed in the FROM clause.When the asterisk (*) is used, the column order of the result table is based on the order in which tables are listed in the FROM clause.

14 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-14 JOINS and the FROM Clause Any SELECT statement that has two or more table names listed in a FROM clause is a JOIN query.Any SELECT statement that has two or more table names listed in a FROM clause is a JOIN query. By definition, a JOIN operation retrieves rows from two or more tables.By definition, a JOIN operation retrieves rows from two or more tables. The FROM clause is always used to list the tables from which columns are to be retrieved by a JOIN query.The FROM clause is always used to list the tables from which columns are to be retrieved by a JOIN query. The FROM clause listing has a limit of 16 table names.The FROM clause listing has a limit of 16 table names. The order of table name listings is irrelevant to the production of the result table with one exception – that is, if you use an asterisk (*) in the SELECT clause, then the column order in the result table reflects the order in which tables are listed in the FROM clause.The order of table name listings is irrelevant to the production of the result table with one exception – that is, if you use an asterisk (*) in the SELECT clause, then the column order in the result table reflects the order in which tables are listed in the FROM clause.

15 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-15 JOINS and the WHERE Clause The WHERE clause specifies the relationship between tables listed in the FROM clause.The WHERE clause specifies the relationship between tables listed in the FROM clause. It also restricts the rows displayed in the result table.It also restricts the rows displayed in the result table. The most commonly used JOIN operator is the "equal" (=) sign.The most commonly used JOIN operator is the "equal" (=) sign.

16 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-16 QUALIFYING COLUMN NAMES When column names are ambiguous (the column names used are from more than one table) you must qualify them.When column names are ambiguous (the column names used are from more than one table) you must qualify them. SELECT * FROM table_1, table_2 WHERE col_1 = col_1; This query is WRONG. Oracle Server would reject this query and generate the error message "ambiguous object.“This query is WRONG. Oracle Server would reject this query and generate the error message "ambiguous object.“ Error at line 3: Error at line 3: ORA - 00918 : column ambiguously defined ORA - 00918 : column ambiguously defined

17 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-17 QUALIFYING COLUMN NAMES This error message tells you that you have included a column name somewhere in the query that exists in more than one table listed in the FROM clause.This error message tells you that you have included a column name somewhere in the query that exists in more than one table listed in the FROM clause. Here the error is in the WHERE clause; however, it is also possible to make a similar error in the SELECT clause.Here the error is in the WHERE clause; however, it is also possible to make a similar error in the SELECT clause. The SELECT statement shown below fails to qualify the col_1 name in the SELECT clause, and Oracle again produces the ORA-00918 error message.The SELECT statement shown below fails to qualify the col_1 name in the SELECT clause, and Oracle again produces the ORA-00918 error message. SELECT col_1 FROM table_1, table_2 WHERE table_1.col_1 = table_2.col_1; ERROR at line 1: ORA-00918: column ambiguously defined

18 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-18 QUALIFYING COLUMN NAMES An ambiguous column name is qualified by using the DOT (.) connector to connect the table name and column name.An ambiguous column name is qualified by using the DOT (.) connector to connect the table name and column name. Sometimes it is easier to qualify column names by using table alias names.Sometimes it is easier to qualify column names by using table alias names. Often, a single letter is used as an identifier to reduce keystroke requirements.Often, a single letter is used as an identifier to reduce keystroke requirements. SELECT dpt_name "Department Name", emp_last_name "Last Name", emp_last_name "Last Name", emp_first_name "First Name" emp_first_name "First Name" FROM employee e, department d WHERE e.emp_dpt_number = d.dpt_no AND e.emp_dpt_number = 7;

19 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-19 QUALIFYING COLUMN NAMES The use of the letters "e" and "d" is completely arbitrary; "t1" and "t2" or any other unique aliases could have been used.The use of the letters "e" and "d" is completely arbitrary; "t1" and "t2" or any other unique aliases could have been used. The important points to learn are:The important points to learn are: –The alias must follow a table name. –Use a space to separate a table name and its alias. –The alias must be unique within the SELECT statement. If the column names are not identical you are not required to qualify them, although you still might want to for documentation purposesIf the column names are not identical you are not required to qualify them, although you still might want to for documentation purposes

20 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-20 JOIN Operations Using Inequality Operators ( ) You may use any relational operator in a JOIN query. The next query uses an inequality operator, the greater than (>) relational operator.You may use any relational operator in a JOIN query. The next query uses an inequality operator, the greater than (>) relational operator. SELECT emp_first_name "First Name", dep_name "Dependent Name", dep_name "Dependent Name", dep_relationship "Relation" dep_relationship "Relation" FROM employee e, dependent d WHERE e.emp_ssn = d.dep_emp_ssn AND e.emp_first_name > d.dep_name; e.emp_first_name > d.dep_name; First Name Dependent Name Relation --------------- --------------------- ---------- Waiman Jo Ellen DAUGHTER Waiman Andrew SON Waiman Susan SPOUSE Suzanne Allen SPOUSE Douglas Deanna DAUGHTER

21 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-21 Joining More Than Two Tables While the examples given thus far have joined rows from two tables, you can specify up to 16 tables in a JOIN operation.While the examples given thus far have joined rows from two tables, you can specify up to 16 tables in a JOIN operation. The more tables that are included in a JOIN operation, the longer the query will take to process, especially when the tables are large with millions of rows per table.The more tables that are included in a JOIN operation, the longer the query will take to process, especially when the tables are large with millions of rows per table.

22 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-22 Joining More Than Two Tables The example shown in the following figure joins three tables to produce a result table based on two different relationships.The example shown in the following figure joins three tables to produce a result table based on two different relationships.

23 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-23 Joining More Than Two Tables The SELECT statement to join the tables depicted in the figure is shown here.The SELECT statement to join the tables depicted in the figure is shown here. SELECT emp_last_name "Last Name", emp_first_name "First Name", emp_first_name "First Name", 1.10*emp_salary "Raised Salary", p.pro_name "Project" 1.10*emp_salary "Raised Salary", p.pro_name "Project" FROM employee e, assignment a, project p WHERE e.emp_ssn = a.work_emp_ssn AND a.work_pro_number = p.pro_number AND a.work_pro_number = p.pro_number AND p.pro_name = 'Inventory'; p.pro_name = 'Inventory'; Last Name First Name Raised Salary Project --------------- --------------- ---------------- ----------- Zhu Waiman $47,300 Inventory Markis Marcia $27,500 Inventory Amin Hyder $27,500 Inventory

24 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-24 Joining Tables by Using Two Columns The diagram depicts the relationship at a university where students enroll in course sections.The diagram depicts the relationship at a university where students enroll in course sections.

25 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-25 Joining Tables by Using Two Columns The SELECT statement that accomplishes the JOIN based on two columns is shown below.The SELECT statement that accomplishes the JOIN based on two columns is shown below. This situation arises when the related tables have composite primary key columns.This situation arises when the related tables have composite primary key columns. SELECT s.course_title "Course Title", e.student_ssn "Student SSN" e.student_ssn "Student SSN" FROM enrollment e, section s WHERE e.course_number = s.course_number AND e.section_number = s.section_number; e.section_number = s.section_number;

26 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-26 OUTER JOIN Operations Oracle also supports what is called an outer- join. This means that a row will appear in the joined table even though there is no matching value in the table to be joined.Oracle also supports what is called an outer- join. This means that a row will appear in the joined table even though there is no matching value in the table to be joined. Suppose you want to know the names of the employees regardless of whether they have dependents or not. You can use the outer join as follows.Suppose you want to know the names of the employees regardless of whether they have dependents or not. You can use the outer join as follows.

27 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-27 OUTER JOIN Operations The plus sign in parentheses (+) tells Oracle to execute an OUTER JOIN operation.The plus sign in parentheses (+) tells Oracle to execute an OUTER JOIN operation. Further, it is the dependent table that is being outer-joined to the employee table because some employees will not have dependents.Further, it is the dependent table that is being outer-joined to the employee table because some employees will not have dependents. SELECT emp_last_name "Last Name", emp_first_name "First Name", dep_name "Dependent", dep_name "Dependent", dep_relationship "Relationship" dep_relationship "Relationship" FROM employee e, dependent d WHERE e.emp_ssn = d.dep_emp_ssn(+);

28 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-28 SELECT emp_last_name "Last Name", emp_first_name "First Name", emp_first_name "First Name", dep_name "Dependent", dep_name "Dependent", dep_relationship "Relationship" dep_relationship "Relationship" FROM employee e, dependent d WHERE e.emp_ssn = d.dep_emp_ssn(+); Last Name First Name Dependent Relationship --------------- --------------- -------------- ------------ Bordoloi Bijoy Joyner Suzanne Allen SPOUSE Zhu Waiman Andrew SON Zhu Waiman Jo Ellen DAUGHTER Zhu Waiman Susan SPOUSE Markis Marcia Amin Hyder Bock Douglas Deanna DAUGHTER Bock Douglas Jeffery SON Bock Douglas Mary Ellen SPOUSE Joshi Dinesh Prescott Sherri 12 rows selected.

29 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-29 OUTER JOINS and NULL values Management might desire a listing of employees with no dependents in order to satisfy some governmental reporting requirement.Management might desire a listing of employees with no dependents in order to satisfy some governmental reporting requirement. We can take advantage of the fact that the dep_name column will be NULL for employees with no dependents, and simply add a criteria to the WHERE clause to include employees where the dep_name column is NULL.We can take advantage of the fact that the dep_name column will be NULL for employees with no dependents, and simply add a criteria to the WHERE clause to include employees where the dep_name column is NULL.

30 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-30 OUTER JOINS and NULL values SELECT emp_last_name "Last Name", emp_first_name "First Name" FROM employee e, dependent d WHERE e.emp_ssn = d.dep_emp_ssn(+) AND d.dep_name IS NULL; d.dep_name IS NULL; Last Name First Name --------------- --------------- Bordoloi Bijoy Markis Marcia Amin Hyder Joshi Dinesh Prescott Sherri

31 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-31 LEFT and RIGHT OUTER JOIN Operations If the employee table is on the left and it is outer-joined with the dependent table depicted on the right, then this is sometimes referred to as a RIGHT OUTER JOIN.If the employee table is on the left and it is outer-joined with the dependent table depicted on the right, then this is sometimes referred to as a RIGHT OUTER JOIN. Now if you think about it, the entity- relationship diagram could just as easily have been drawn with the dependent table on the left and the employee table on the right. The same JOIN operation would now be termed a LEFT OUTER JOIN.Now if you think about it, the entity- relationship diagram could just as easily have been drawn with the dependent table on the left and the employee table on the right. The same JOIN operation would now be termed a LEFT OUTER JOIN.

32 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-32 LEFT JOIN SELECT emp_last_name "Last Name", emp_first_name "First Name", dep_name "Dependent", dep_relationship "Relationship" dep_name "Dependent", dep_relationship "Relationship" FROM employee e, dependent d WHERE d.dep_emp_ssn(+) = e.emp_ssn; Last Name First Name Dependent Relationship --------------- --------------- -------------- ------------ Bordoloi Bijoy Joyner Suzanne Allen SPOUSE Zhu Waiman Andrew SON Zhu Waiman Jo Ellen DAUGHTER Zhu Waiman Susan SPOUSE Markis Marcia Amin Hyder Bock Douglas Deanna DAUGHTER Bock Douglas Jeffery SON Bock Douglas Mary Ellen SPOUSE Joshi Dinesh Prescott Sherri 12 rows selected.

33 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-33 SELF-JOIN Operations A SELF JOIN operation is used to produce a result table when the relationship of interest exists among rows that are stored within a single table.A SELF JOIN operation is used to produce a result table when the relationship of interest exists among rows that are stored within a single table.

34 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-34 SELF-JOIN Operations SELECT e1.emp_last_name || ', ' || e1.emp_first_name "Supervisor", e2.emp_last_name || ', ' || e2.emp_first_name "Employee" e2.emp_last_name || ', ' || e2.emp_first_name "Employee" FROM employee e1, employee e2 WHERE e1.emp_ssn = e2.emp_superssn; Supervisor Employee ---------------------------- ------------------- Bordoloi, Bijoy Joyner, Suzanne Bordoloi, Bijoy Zhu, Waiman Joyner, Suzanne Markis, Marcia Joyner, Suzanne Amin, Hyder Zhu, Waiman Bock, Douglas Zhu, Waiman Joshi, Dinesh Zhu, Waiman Prescott, Sherri


Download ppt "Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-1 CHAPTER 6: JOINS."

Similar presentations


Ads by Google