Left and right joins, PL-SQL Programming

Assignment Help:

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,

lastname,

addressLine1

 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.


Related Discussions:- Left and right joins

Control structure, Control Structures The Control structures are the mo...

Control Structures The Control structures are the most important PL/SQL extension to the SQL. Not only does PL/SQL let you manipulate Oracle data, it lets you process the data

Example of unwrap operator - sql, Example of UNWRAP Operator - SQL Exa...

Example of UNWRAP Operator - SQL Example here shows how unwrapping can be done in longhand in SQL. Example: Unwrapping in SQL Letting CONTACT_INFO_WRAPPED denote the res

Use the pls_integer datatype - performance of application, Use the PLS_INTE...

Use the PLS_INTEGER Datatype When you require to declare an integer variable, use the datatype PLS_INTEGER that is the most efficient numeric type. That is as the PLS_INTEGER

Use the returning clause -improve performance of application, Use the RETUR...

Use the RETURNING Clause Frequently, the application requires information about the row affected by a SQL operation, for illustration, to produce a report or take a subsequent

Using inner join, Using INNER JOIN INNER JOIN is used to retrieve the ...

Using INNER JOIN INNER JOIN is used to retrieve the data from all tables listed based on a condition of equality listed after keyword ON. If the condition is not meet, rows ar

Calculate days between ordering and shipping, An analyst in the quality ass...

An analyst in the quality assurance office reviews the time lapse between receiving an order and shipping an order. Any orders that have not been shipped within a day of the order

Updating objects in pl sql, Updating Objects: To change the attributes...

Updating Objects: To change the attributes of objects in an object table, you can use the UPDATE statement, as the illustration below shows: BEGIN UPDATE persons p SET p

Row operators - sql operators, Row Operators The Row operators return ...

Row Operators The Row operators return or reference the particular rows. ALL retains the duplicate rows in the result of a query or in an aggregate expression. The DISTINCT el

Is null operator-comparison operators, IS NULL Operator The IS NULL oper...

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

Exceptions are raised - Using Raise statement, How Exceptions Are Raised ...

How Exceptions Are Raised By the run-time system, the internal exceptions are raised implicitly as are user-defined exceptions that you have related with an Oracle error number

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd