Overview of functions that join multiple table’s data
These priprotiary functions are designed specifically to be used with the Oracle system.
Equijoins 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.column FROM Table1 alias1, Table2 alias2 WHERE table1.column = table2.column
SELECT D_songs.artist, D_Songs.Title, D_Types.DESCRIPTION FROM D_Songs, D_Types WHERE D_Songs.type_code = D_Types.code
Cartesian Product 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
Non Equijoins 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.column FROM Table1, Table2 WHERE table1.column BETWEEN table2.column_01 AND table2.column_02 SELECT d_packages.code, d_events.cost FROM d_packages, d_events WHERE d_events.cost BETWEEN d_packages.low_range AND d_packages.high_range
Outer Join 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.column FROM 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
Self Joins Joins used to join a table to itself by giving a single table two aliases. SELECT alias1.column, alias2.column FROM table1 alias1, table1 alias2 WHERE alias1.column = alias2.column; SELECT worker.last_name, worker.employee_id, manager.last_name, manager.employee_id FROM employees worker, employees manager WHERE worker.manager_id = manager.employee_id;
Hierarchical Join 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_id FROM employees START WITH last_name='De Haan' CONNECT BY PRIOR employee_id=manager_id
American National Standards Institute A set of standard join types that can be used in ANY SQL environment.
Natural Joins 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 Equijoin SELECT column1, column2, column3 FROM table1 NATURAL JOIN table2
SELECT event_id, song_id, cd_number FROM d_play_list_items NATURAL JOIN d_track_listings
Cross 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_number FROM d_play_list_items p CROSS JOIN d_track_listings WHERE event_id=105;
ON and USING Clauses Using Clause The USING clause specified specifies the columns that should be used for the equijoin. SELECT client_number, first_name, last_name, event_date FROM d_clients JOIN d_events USING (client_number);
On Clause Used if columns have different names or non-equality comparison operators SELECT e.last_name as "EMP", m.last_name as "MGR" FROM employees e JOIN employees m ON (e.manager_id = m.employee_id);
Joining Three Tables Combined three join statements to join three tables. USING---> JOIN ---> ON Statement 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);
Inner Joins a join of two or more tables that return only matched rows is called an inner join. SELECT Person.LastName, Person.FirstName, Sales.OrderNo FROM Person INNER JOIN Sales ON Person.P_Id=Sales.P_Id ORDER BY Person.LastName
Left and Right Outer 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 called
Right Outer Join
SELECT e.last_name, d.department_id, d.department_name FROM employees e RIGHT OUTER JOIN departments d ON (e.department_id = d.department_id);
Left Outer Join
Full Outer Joins 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 columns SELECT e.last_name, d.department_id, d.department_name FROM employees e FULL OUTER JOIN departments d ON (e.department_id = d.department_id);.
A comparative chart used to show equivalent functions
ANSI versus Oracle Join Types Oracle
Where Clause
ANSI
Cartesian
********
CROSS JOIN
Equijoin
WHERE =
Non Equijoin
WHERE BETWEEN
NATURAL JOIN Using ON ON
Outer Join
WHERE +
Self Join
WHERE + Alias
Outer Join (LEFT, RIGHT, FULL) SELF JOIN