Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sections 3 – Joins & Hierarchical Queries

Similar presentations


Presentation on theme: "Sections 3 – Joins & Hierarchical Queries"— Presentation transcript:

1 Sections 3 – Joins & Hierarchical Queries
Database Programming Sections 3 – Joins & Hierarchical Queries 10/3/06 Updated 10/20/09 Used 10/18/11 DPLesson3_Fa_11.ppt DP_3_Fa11.txt

2 Overview Oracle Proprietary Joins (8i and prior):
Cartesian Product Equijoin Non-equijoin Outer join Self join SQL: 1999 Compliant Joins: Cross joins Natural joins Using clause Full or two sided outer joins Arbitrary join conditions for outer joins Natural Join = equijoin Cross join = Cartesian Product join Join On can be used to do a self join Explain that ANSI stands for American National Standards Institute. Founded in 1918, ANSI is a private, non-profit organization (501(c)3) that administers and coordinates the U.S. voluntary standardization and conformity assessment system. The Institute's mission is to enhance both the global competitiveness of U.S. business and the U.S. quality of life by promoting and facilitating voluntary consensus standards and conformity assessment systems, and safeguarding their integrity. Reference: Marge Hohly

3 Natural Join ANSI/ISO SQL: 1999 Join equivalent of an equijoin
Join on all common columns ie. columns with same name and data type SYNTAX: SELECT field1, field2 FROM table1 NATURAL JOIN table2 WHERE fieldn = value; A natural join is based on all columns in the two tables that have the same name and selects rows from the two tables that have equal values in all matched columns. You do not have to specify which column is common and used for the join. It must have the same column name and same data type. Note the join column does not have to appear in the output. Marge Hohly

4 Example Natural Join SELECT event_id, song_id, cd_number FROM d_play_list_items NATURAL JOIN d_track_listings WHERE event_id = 105; Ask students: which column or columns have the two tables been joined on? Answer: song_id, because it is the only column with the same name and datatype in both tables. Marge Hohly

5 Example Natural Join SELECT first_name, last_name, event_date, description FROM d_clients NATURAL JOIN d_events; These two tables were joined on the Client_number field which is the common field even though it was not listed in the join. SELECT employee_id, last_name, event_date, description FROM employees NATURAL JOIN departments; Marge Hohly

6 Example Use an equijoin between the DJs on Demand database tables, d_songs and d_types. Display the type code, description and title. Limit the rows returned to those type codes between 70 and 80. Answers: 5. SELECT type_code, description, title FROM d_songs, d_types WHERE d_songs.type_code = d_types.code AND type_code BETWEEN 70 and 80; Common field does not have the same name, where clause defines common field with qualified name. Marge Hohly

7 Cross Join ANSI/ISO SQL: 1999 syntax for achieving of a Cartesian product Syntax: SELECT * FROM employees CROSS JOIN departments; Last section Cartesian Product: SELECT * FROM employees, departments; The resulting row = #rows table1 * #rows table2 SELECT name, event_date, loc_type, rental_fee FROM d_events CROSS JOIN d_venues; In the example cross join, ask students to count the number of rows in both tables. The number of rows returned by the query will be the product of the number of rows in each table: 2 * 6 = 12. Marge Hohly

8 Join Using ANSI/ISO SQL: 1999 join condition used to join two tables on only one column Typically used where NATURAL JOIN cannot be used because there are other common columns with same name and different data types No table name or alias can be used on the referenced column anywhere in the USING statement Remind students of the restriction of using table names and aliases for the referenced columns in the USING clause. The columns referenced in the USING clause should not have a qualifier (table name or alias) anywhere in the SQL statement. Ask students to rewrite the JOIN USING example as an equijoin. Note that qualifiers can be used. Sometimes, two tables being joined using the natural join have more than one column in common. By employing the USING clause, the join can be on one common column that is specified. This is an ANSI standard join condition. The column reference in the JOIN USING command cannot be aliased anywhere within the SELECT statement. You can still use a WHERE condition to restrict rows returned after the USING clause and the ORDER BY clause. Let’s examine an example. Marge Hohly

9 Join Using SYNTAX: SELECT field1, field2, field3 FROM table1 JOIN table2 USING(column name); Example: SELECT e.employee_id, e.last_name, d.location_id FROM employees e JOIN departments d USING(department_id); OK to use Alias for table names and in fact it should be easier. With USING field name MUST be same in both tables, if the field names are different use ON. You may use a WHERE clause with JOIN USING also to restrict output. See next slide Rewrite JOIN USING example as an equijoin SELECT c.client_number, c.first_name, c.last_name, e.event_date FROM d_clients c, d_events e WHERE c.client_number = e.client_number; Marge Hohly

10 Example SELECT e.employee_id, e.last_name, d.location_id FROM employees e JOIN departments d USING(department_id); Look at the data in the employees and departments tables to evaluate the results. SELECT e.employee_id, e.last_name, d.location_id FROM employees e JOIN departments d USING(department_id); SELECT client_number, first_name, last_name, event_date FROM d_clients JOIN d_events USING(client_number); USING(client_number) WHERE last_name = ‘Peters’; Marge Hohly

11 JOIN ON ANSI/ISO: 1999 join clause that may be used to specify the condition or columns used: Syntax: SELECT field1, field2, field3 FROM table1 JOIN table2 ON(table1.fieldx=table2.fieldy); SELECT e.last_name emp, m.last_name mgr FROM employees e JOIN employees m ON(e.manager_id = m.employee_id); SELECT e.last_name as "EMP", w.last_name as "MGR“ FROM employees e JOIN employees w ON (e.manager_id = w.employee_id) WHERE e.last_name like 'H%'; Where clause can limit results. This is similar to a Self join The ON clause can be used to specify columns to join. The ON clause can also be used to specify arbitrary conditions such as joining columns that have different names. Also used to join non-equality comparison operators such as <, >, or BETWEEN. JOIN ON allows a greater variety of join conditions to be specified. SELECT c.client_number, c.first_name, c.last_name, e.event_date FROM  d_clients c, d_events e WHERE c.client_number = e.client_number; What if the columns have different names, or if the join condition uses non-equality operators? The ON clause is another ANSI standard syntax that allows for a greater variety of join conditions. JOIN ON is a join clause that may be used to specify the condition or columns used. It is very common in database design that two tables, where there is a primary key and foreign key relationship, do not have the same column name and this is the time when you need to use JOIN ON. Marge Hohly

12 Example of JOIN ON SELECT e.last_name, e.department_id, d.department_name FROM employees e JOIN departments d ON (e.department_id = d.department_id); Join On example SELECT e.last_name, e.department_id, d.department_name FROM employees e JOIN departments d ON (e.department_id = d.department_id); JOIN USING and JOIN ON can be used with three or more tables also. SELECT last_name, event_date, t.description FROM d_clients c JOIN d_events e USING (client_number) JOIN d_themes t ON (e.theme_code = t.code); Marge Hohly

13 Summary/Comparison Note Equijoin is the same as Natural Join (not Cross Join) Marge Hohly

14 A non-equijoin use ON for <> and between also.
Marge Hohly

15 Examples 2. Join DJs on Demand d_play_list_items, d_track_listings, and d_cds tables with the JOIN USING syntax. Include the song ID, CD number, title, and comments in the output. 3. Display the city, department name, location ID, and department ID for departments 10, 20, and 30 for the city of Seattle. 6. Display job title, employee first name, last name, and for all employees that are stock clerks. 9. (Use Join On) Query and display manager ID, department ID, department name, first name, and last name for all employees in departments 80, 90, 110, and 190. 2. SELECT song_id, cd_number, title, comments FROM   d_play_list_items JOIN d_track_listings  USING (song_id )JOIN d_cds USING (cd_number); 3. SELECT l.city, d.department_name, location_id, d.department_id FROM locations l JOIN departments d USING (location_id) WHERE city = 'Seattle' AND department_id IN (10, 20, 30); 6. SELECT job_title, first_name, last_name, FROM employees e JOIN jobs j USING (job_id) WHERE job_id = 'ST_CLERK'; 9. SELECT e.manager_id,d.department_id, d.department_name, e.first_name, e.last_name FROM  employees e JOIN departments d ON (e.department_id =d.department_id) WHERE d.department_id IN (80, 90, 110, 190); Marge Hohly

16 Three-way Joins with the ON clause
A three-way join is the join of three tables Syntax: SELECT employee_id, city, department_name FROM employees e JOIN departments d ON (d.department_id = e.department_id) JOIN locations l ON (d.location_id=l.location_id); Employee_id from employees city from locations department_name from Departments Look at fields of tables SELECT last_name, event_date, t.description FROM d_clients c JOIN d_events e USING(client_number) JOIN d_themes t ON(e.theme_code=t.code; This example demonstrates one way to use the JOIN ON clause when joining three tables together. In this case the syntax lists two separate joins, with USING for one of them, and ON for the other. JOIN d_events USING (client_number) is the first join, a natural join. JOIN d_themes t ON (e.theme_code = t.code) is the second join, using the ON clause to specify the columns to join. This three-way join can also be accomplished by using JOIN ON for the first join. You would not want to use a NATURAL JOIN to join these tables together, because there is a sharing of more than two columns of information. Be sure to try this example in Application Express. Marge Hohly

17 Example SELECT employee_id, city, department_name FROM employees e JOIN departments d ON (d.department_id = e.department_id) JOIN locations l ON (d.location_id=l.location_id); Reviews the contents of the three tables. Note use of USING and ON, ON could be used for both joins also. SELECT last_name, event_date, t.description FROM d_clients c JOIN d_events e USING(client_number) JOIN d_themes t ON(e.theme_code=t.code; Same as previous slide. Marge Hohly

18 Revised example SELECT e.employee_id, l.city, d.department_name FROM employees e, departments d, locations l WHERE e.department_id = d.department_id AND d.location_id = l.location_id; The results are the same Marge Hohly

19 INNER JOINS An inner join returns only those rows that match the join condition SELECT e.last_name, e.department_id, d.department_name FROM employees e JOIN departments d ON (e.department_id = d.department_id); Run command. This is similar to an equi-join Anything by default that is not an outer join is an inner join. Section 3 Lesson 3 Marge Hohly

20 OUTER JOIN Outer joins return those rows that match the join condition and those that do not There are three ANSI/ISO SQL: 1999 outer joins: LEFT OUTER JOIN RIGHT OUTER JOIN FULL OUTER JOIN When a join returns the matched rows as well as unmatched rows, it is called an outer join. Outer join syntax uses the terms “left, full, and right.” So far, all the ANSI standard joins we have been examining are inner joins. Inner joins return only those rows that match the join condition. If we want to include rows that do not match our join conditions, we must use one of the three outer joins specified in the ANSI standard syntax. Marge Hohly

21 LEFT OUTER JOIN SELECT e.last_name, e.department_id, d.department_name FROM employees e LEFT OUTER JOIN departments d ON(e.department_id=d.department_id); SQL 99 equivalent: SELECT e.last_name, e.department_id, d.department_name FROM employees e, departments d WHERE e.department_id=d.department_id(+); This statement will return those employees who do not have a department_id Table on left (everything in left table) join to all matches in the right table Therefore you will find blanks fields for the right table This example demonstrates a LEFT OUTER JOIN. We see that in this example we are joining the employees table with the departments table. Now remember, one of the employees does not have a department id. In order to show that department, we use a LEFT OUTER JOIN listing the employees table first. In this case, every employee from the table will be shown in the results, even if they do not have a department id assigned. Marge Hohly

22 RIGHT OUTER JOIN SELECT field1, field FROM table1 a RIGHT OUTER JOIN table2 b ON (a.field=b.field); or USING(field name); SELECT e.last_name, e.department_id, d.department_name FROM employees e RIGHT OUTER JOIN departments d ON(e.department_id=d.department_id); This statement will return those departments who do not have any employees in them Everything in Right table to everything that matches in left table emp ---- Dept FROM tab1 JOIN tab2 on (field) JOIN tab3 on (field) SELECT e.last_name, e.department_id, d.department_name FROM employees e, departments d WHERE e.department_id(+)=d.department_id; This example demonstrates the RIGHT OUTER JOIN. The syntax is almost identical to the LEFT OUTER JOIN. This time, the query will return those departments who do not have any employees in them. Marge Hohly

23 FULL OUTER JOIN The FULL OUTER JOIN returns both matched and all unmatched rows Syntax: SELECT field1, field2, field3 FROM table1 a FULL OUTER JOIN table2 b ON a.field=b.field; SELECT e.last_name, e.department_id, d.department_name FROM employees e FULL OUTER JOIN departments d ON(e.department_id=d.department_id); There is no direct comparable Oracle specific join Everything from both tables, it will not leave out any record. Using a full outer join solves this problem. The results set of a full outer join includes all rows in both tables even if there is no match in the other table. The ANSI standard join can do a FULL OUTER JOIN, which is impossible to do with an Oracle proprietary join. The FULL OUTER JOIN returns both matched and all unmatched rows. The syntax is shown in this example. This join has the effect of using a plus on both sides of the equal sign to include everything in each table that does not have a direct match in the other table being used. Since the plus on both sides is not allowed in the Oracle proprietary join syntax, the FULL OUTER JOIN makes it possible to include all data, whether or not it matches with the other table. Make sure you try this in Application Express to see that every department is listed and every employee is listed. You should get a total of twenty one rows. Marge Hohly

24 Example Construct a join to display a list of Global Fast Foods customers whether or not they have placed an order as yet and all the customers who have placed orders. SELECT c.first_name, c.last_name, o.order_number,o.order_date, o.order_total FROM   f_customers c LEFT OUTER JOIN f_orders o ON   (c.id = o.cust_id); Result 2 records. Marge Hohly

25 Types of Joins Oracle Proprietary Joins (8i and prior):
Cartesian Product Equijoin Non-equijoin Outer join Self join SQL: 1999 Compliant Joins: Cross joins Natural joins Using clause Full or two sided outer joins Arbitrary join conditions for outer joins On clause Type of Joins There are two ways in which to make connections between tables in a database. One way is to use syntax or commands that are referred to as Oracle proprietary joins. The other way this to use the ANSI/ISO SQL 99 compliant standard joins. In this course, you will learn to use both Marge Hohly

26 Self-Join SELECT w.last_name || ' works for ' || m.last_name "Employees Managers" FROM employees w, employees m WHERE w.manager_id=m.employee_id Where w = workers and m = managers Self join is also called a ‘pig’s ear’ and is a join of a table to itself. This example above is similar to the one we discussed in data modeling Marge Hohly

27 Self-Join EMPLOYEES (worker) table EMPLOYEES (manager) table
Employee_id last_name manager_id employee_id 100 King 101 Kochar 102 De Haan 103 Hunold 104 Ernst 107 Lorentz 124 Mourgos To join the same table to itself you need to give two names or alias to the tables. Choose alias name that are related to the join There may be times when you need to join a table to itself. This is a possibility whenever there is a recursive relationship that’s brought back from your ER diagram. To find the name of each employee’s manager, you need to join the employees table to itself or perform what is called a self join. For example, to find the name of Lorentz’s manager you need to first find Lorentz in the employees table by looking at the last name column. You find the manager number for Lorentz by looking at the manager id column. Lorentz’s manager number is 103. Next, you find the name of the manager with the employee id 103. Looking at the last name column, Hunold’s employee id is 103. So, Hunold is Lorentz’s manager. In this process you look at the table twice. The first time you look in the table to find Lorentz in the last name column and 103 in the manager id column. The second time you look in the employee id column to find 103 and the last name column to find Hunold. The query works because every manager is also an employee. EMPLOYEES (worker) table EMPLOYEES (manager) table Manager_id in worker table = employee_id in Manager table Marge Hohly

28 Example SELECT chair.last_name || ‘ is the section chair of ‘ || player.last_name FROM band_members chair, band_members player WHERE player.chair_id = chair.member_id; This example joins the band_members table to itself to see who is the chair of that player’s section. To simulate two tables in the FROM clause, there are two aliases, namely ‘chair’ and ‘player,’ for the same table, the ‘band_members’ table. In this example, the WHERE clause contains the join that means, where a player’s chair number matches the member_id of the chair. Marge Hohly

29 Hierarchical Queries Similar to the self-join
Similar to an Organization Chart Shows company, department , family structure etc. Marge Hohly

30 Using Hierarchical Queries
Retrieve data based on natural hierarchical relationships between rows If data within a single table called tree walking Closely related to self-joins are hierarchical queries. A column in a database may contain data that is hierarchical in nature. The basis for this data is often a recursive relationship such as the one you’ve seen in the employees table. We can build an Organization Chart showing the structure of a company or a department. Imagine a family tree with the eldest members of the family found close to the base or trunk of the tree and the youngest members representing branches of the tree. Branches can have their own branches, and so on. Examine the EMPLOYEES table to see who works for whom. Like going down a tree. Marge Hohly

31 Hierarchical queries EMPLOYEES as an organization chart
Kochar De Haan Mourgos Hunold Rajs Ernst King Ask students how to modify the join statement to include only orders from December 1, 2002, to December 31, 2004, inclusive. Answer: WHERE o.order_date BETWEEN '01-JAN-02' AND '31-DEC-04'; Discuss with students, “Why is it important, from a business perspective, to be able to join data from multiple sources in so many different ways?” In the real world, you often count on outer joins for checking your data. A business rule may state, “You must enter all new employees and then assign them to a department.” What if the system allowed employees to be entered without departments, even though the business rule requires them to have departments? Well, first, the system would not be perfect. Someone didn’t design it properly with the business needs in mind. However, as the programmer, you may be asked to find those employees without departments and produce a report so that a manager can assign them to a department and correct the data! Marge Hohly

32 Hierarchical queries keywords
START WITH CONNECT BY PRIOR LEVEL To create hierarchical queries, we use three key phrases: START WITH, CONNECT BY PRIOR, and LEVEL. START WITH tells the query at which level to begin the analysis or the root of the tree. CONNECT BY PRIOR defines the relationship basis of the hierarchy. This is often a primary to foreign key relationship. LEVEL identifies the distance from the START WITH or root level of the hierarchy. Let’s examine an example. Marge Hohly

33 Hierarchical queries keyword example
SELECT employee_id, last_name, job_id, manager_id FROM employees START WITH employee_id = 100 CONNECT BY PRIOR employee_id = manager_id; Run command and see the above table is produced. Similar to a self-join result. Marge Hohly

34 Another example SELECT last_name || ' reports to ' || PRIOR last_name "Walk Top Down“ FROM employees START WITH last_name = 'King‘ CONNECT BY PRIOR employee_id = manager_id; Run the above command and see the resulting table. Marge Hohly

35 Results of query Marge Hohly Walk Top Down King reports to
Kochhar reports to King Whalen reports to Kochhar Higgins reports to Kochhar Gietz reports to Higgins De Haan reports to King Hunold reports to De Haan Ernst reports to Hunold Lorentz reports to Hunold Marge Hohly

36 Let’s start by looking at the code in this example.
We first see that the keyword LEVEL is used as a column name. PRIOR is also part of the SELECT clause. ‘Walk Top Down’ is the column alias. The FROM clause identifies the employees table. The START WITH clause identifies the first level of the hierarchy. In this case, it identifies “King” by name. We could also have used his employee id. CONNECT BY PRIOR identifies the relationship. It is similar to the join condition of a regular self join SELECT statement. ()Now let’s look at the output. Since START WITH identified “King” as our starting level, “King” is level one. Notice “King” has no prior last name, since he is at the top level. King’s immediate subordinates show him as ‘prior.’ Finally, see that for each row, the PRIOR name is exactly one level above the employee name. We could have included an ORDER BY LEVEL phrase to use the LEVEL column to sort the output differently. Marge Hohly

37 Hierarchical query report
SELECT LPAD(last_name, LENGTH(last_name)+(LEVEL*2)-2,'_') AS ORG_CHART FROM employees START WITH last_name = 'King‘ CONNECT BY PRIOR employee_id = manager_id; View the results and note each row is indented by two underscores per level. Marge Hohly

38 Hierarchical queries pruning
Delete branches of tree with WHERE clause or CONNECT BY PRIOR clause WHERE - only row named is excluded CONNECT BY PRIOR – entire branch is excluded View example on next slide Marge Hohly

39 Pruning Example SELECT last_name FROM employees WHERE last_name <> 'Higgins‘ START WITH last_name = 'Kochhar‘ CONNECT BY PRIOR employee_id = manager_id; Only the branch under King is returned Compare results with results of slide #37 Marge Hohly

40 Pruning Example SELECT last_name FROM employees START WITH last_name = 'Kochhar‘ CONNECT BY PRIOR employee_id = manager_id AND last_name <> 'Higgins'; Note your results are only Kochhar and Whalen Marge Hohly


Download ppt "Sections 3 – Joins & Hierarchical Queries"

Similar presentations


Ads by Google