join funcitons
DESCRIPTION
A short powerpoint overviewing Join FunctionsTRANSCRIPT
Overview of functions that join multiple table’s data
These priprotiary functions are designed specifically to be used with the Oracle system.
Joins where the specified columns are equal. Tables are joined automatically based on columns of the same name and datatype. Uses the Equal Sign (=)SELECT alias1.column, alias2.columnFROM Table1 alias1, Table2 alias2WHERE table1.column = table2.column
SELECT D_songs.artist, D_Songs.Title, D_Types.DESCRIPTIONFROM D_Songs, D_TypesWHERE D_Songs.type_code = D_Types.code
Equijoins
A type of equijoin that contains no WHERE clause. Joins everything to everything else by joining from two tables. SELECT *FROM table1, table2
SELECT*FROM d_play_list_items,d_track_listings
Cartesian Product
Join functions that do not use an equality operator. Often used if you want to find data in a range, by using the BETWEEN statement.Select Table1.column, table2.columnFROM Table1, Table2WHERE table1.column BETWEEN table2.column_01AND table2.column_02
SELECT d_packages.code, d_events.costFROM d_packages, d_eventsWHERE d_events.cost BETWEENd_packages.low_range ANDd_packages.high_range
Non Equijoins
Outer joins are used if you want all of the data from one table, even if there is not data (null value) in a corresponding table.
SELECT table1.column, table2.columnFROM table1, table2***WHERE table1.column(+) = table2.column;// Returns all values of table1.column***WHERE table1.column = table2.column(+);// Returns all values of table2.column***NEVER table1.column(+) = table2.column(+); *Note: you can only use one of these statements at a time
Outer Join
Joins used to join a table to itself by giving a single table two aliases.
SELECT alias1.column, alias2.columnFROM table1 alias1, table1 alias2WHERE alias1.column = alias2.column;
SELECT worker.last_name, worker.employee_id, manager.last_name, manager.employee_idFROM employees worker, employees managerWHERE worker.manager_id = manager.employee_id;
Self Joins
Joins a table based on a connection to lower, hierarchical indexes. Uses a JOIN BY PRIOR statement. Type of Self Join
SELECT LPAD(last_name, LENGTH(last_name)+(LEVEL*2)-2,'_') AS ORG_CHART, salary, department_idFROM employeesSTART WITH last_name='De Haan'CONNECT BY PRIOR employee_id=manager_id
Hierarchical Join
American National Standards Institute
A set of standard join types that can be used in ANY SQL environment.
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.The Equivalent of an EquijoinSELECT column1, column2, column3FROM table1 NATURAL JOIN table2
SELECT event_id, song_id, cd_numberFROM d_play_list_items NATURAL JOIN d_track_listings
Natural Joins
Produces a Cartesian Product by joining the two tables in all possible ways. The results set represents all possible combinations of columns from both tables. SELECT event_id, p.song_id, cd_numberFROM d_play_list_items p CROSS JOIN d_track_listingsWHERE event_id=105;
Cross Joins
Using ClauseThe USING clause specified specifies the columns that should be used for the equijoin.SELECT client_number, first_name, last_name, event_dateFROM d_clients JOIN d_eventsUSING (client_number);On ClauseUsed if columns have different names or non-equality comparison operatorsSELECT e.last_name as "EMP", m.last_name as "MGR"FROM employees e JOIN employees mON (e.manager_id = m.employee_id);
ON and USING Clauses
Combined three join statements to join three tables.USING---> JOIN ---> ON Statement
SELECT last_name, event_date, t.descriptionFROM d_clients c JOIN d_events eUSING (client_number)JOIN d_themes tON (e.theme_code = t.code);
Joining Three Tables
a join of two or more tables that return only matched rows is called an inner join.SELECT Person.LastName, Person.FirstName, Sales.OrderNoFROM PersonINNER JOIN SalesON Person.P_Id=Sales.P_IdORDER BY Person.LastName
Inner Joins
Works like a regular outer join to return values even if the corresponding table has no data (null values). Instead of (+)s the terms LEFT or RIGHT are used to determine which table is calledSELECT e.last_name, d.department_id, d.department_nameFROM employees eRIGHT OUTER JOIN departments dON (e.department_id = d.department_id);
Left and Right Outer Joins
Right Outer Join
Left Outer Join
Unlike any of the other outer join types, a full outer join will return data if there is null data in either of the two columnsSELECT e.last_name, d.department_id, d.department_nameFROM employees eFULL OUTER JOIN departments dON (e.department_id = d.department_id);.
Full Outer Joins
A comparative chart used to show equivalent functions
Oracle Where Clause ANSICartesian ******** CROSS JOINEquijoin WHERE = NATURAL JOIN
Using ON
Non Equijoin WHERE BETWEEN ONOuter Join WHERE + Outer Join
(LEFT, RIGHT, FULL)
Self Join WHERE + Alias SELF JOIN
ANSI versus Oracle Join Types