Left and right joins, PL-SQL Programming

Left and Right Joins

LEFT OUTER JOIN can be used when you want to retrieve the data from the main table (table1) even if there is no match in other tables (table_2, table_3....). While RIGHT OUTER JOIN is used to retrieve the data the from all other tables (table_2, table_3...) even if there is no match in the main table. As an example, in our classicalmodels sample database, when the company wants to establish a new office, the SQL script to insert a new office to the databse as follows:

INSERT INTO classicmodels.offices

(officeCode, city, phone, addressLine1, addressLine2, state, country, postalCode, territory)

VALUES  ('8', 'Boston', '+1 215 837 0825', '1550 dummy street', 'dummy address', 'MA', 'USA', '02107', 'NA')

At this time, the company hasn't hired any new employees yet, so the new office does not have any employees. If we want to know which employees belong to what offices and all the offices of the company, we can use RIGHT JOIN as follows:

SELECT firstname,



 FROM employees as e  RIGHT OUTER JOIN offices as o

 ON o.officeCode = e.officeCode

The Right join on offices will cause all records from the offices table (the table on the right in the join stmt) to display, even if there is not a corresponding record in the employees table. Notice the last record contains null values for the first and last name values pulled from employees table. This is because there are no records in the employees table with an office code of 8 (the boston office we just added to the office table.)


| firstname | lastname  | addressLine1             |


| Mary      | Patterson | 100 Market Street        |

| Diane     | Murphy    | 100 Market Street        |

| Jeff      | Firrelli  | 100 Market Street        |

| Anthony   | Bow       | 100 Market Street        |

| Leslie    | Jennings  | 100 Market Street        |

| Leslie    | Thompson  | 100 Market Street        |

| Julie     | Firrelli  | 1550 Court Place         |

| Steve     | Patterson | 1550 Court Place         |

| FoonYue  | Tseng     | 523 East 53rd Street     |

| George    | Vanauf    | 523 East 53rd Street     |

| Gerard    | Bondur    | 43 Rue JouffroyD'abbans |

| Loui      | Bondur    | 43 Rue JouffroyD'abbans |

| Gerard    | Hernandez | 43 Rue JouffroyD'abbans |

| Pamela    | Castillo  | 43 Rue JouffroyD'abbans |

| Martin    | Gerard    | 43 Rue JouffroyD'abbans |

| Mami      | Nishi     | 4-1 Kioicho              |

| Yoshimi   | Kato      | 4-1 Kioicho              |

| William   | Patterson | 5-11 Wentworth Avenue    |

| Andy      | Fixter    | 5-11 Wentworth Avenue    |

| Peter     | Marsh     | 5-11 Wentworth Avenu    |

| Tom       | King      | 5-11 Wentworth Avenue    |

| Larry     | Bott      | 25 Old Broad Street      |

| Barry     | Jones     | 25 Old Broad Street      |

| NULL      | NULL      | 1550 dummy street        |


24 rows in set (0.00 sec)

As you can see, the RIGHT JOIN get the all the data from second table (offices) and data from the first table even the condition does not match.

Posted Date: 2/16/2013 8:17:36 AM | Location : United States

Related Discussions:- Left and right joins, Assignment Help, Ask Question on Left and right joins, Get Answer, Expert's Help, Left and right joins Discussions

Write discussion on Left and right joins
Your posts are moderated
Related Questions
Managing Cursors The PL/SQL uses 2 types of cursors: implicit and explicit. The PL/SQL declares a cursor implicitly for all the SQL data manipulation statements, including th

Comparison Operators Usually, you use the comparison operators in the WHERE clause of a data manipulation statement to form the predicates, that compare one expression to anot

FETCH Statement The FETCH statement retrieve rows of data one at a time from the result set of the multi-row query. The data is stored in fields or variables which correspond t

I would like to have a custom MS Access database designed and coded that would help me schedule my customer's orders and that would help me track my employees production output and

I need to write one function and one procedure to query a Oracle 10.1 DB using PL SQL. I have the schema and exact queries...along with work Ive started and a template to put the a

Map and Order Methods: The values of the scalar datatype like CHAR or REAL have a predefined order that allows them to be compared. While, the instances of an object type has

IS NULL Operator The IS NULL operator returns the Boolean value TRUE whenever its operand is null or FALSE if it is not null. The comparisons including the nulls always yield NU

OUT Mode An OUT parameter returns values to the caller of a subprogram. Within the subprogram, an OUT parameter act like a variable. That means that you can use an OUT formal

Difference between 9i & 10G When Oracle releases any new databases then it are having some discrepancy with them. But 10G is having much difference than oracle 9i has. Oracle