Using inner join, PL-SQL Programming

Assignment Help:

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 are not returned. For example, see the following tables in our classicmodels sample database. We have employees table and offices table. Two tables are linked together through the keyed column officeCode. OfficeCode is the primary key of the offices table and is a foreign key in the employees table. To find out what employees work in which countries and states we can use INNER JOIN to join the offices and employees table. We return columns from both tables (note the fully qualified column names like "employees.firstname"), but only the rows where the officeCode in each of the tables match. Here is the SQL code:

SELECT employees.firstname,

employees.lastname,

offices.country,

offices.state

FROM employees INNER JOIN offices

  ON offices.officeCode = employees.officeCode

And we will get the data like this:

+-----------+-----------+-----------+------------+

| firstname | lastname  | country   | state      |

+-----------+-----------+-----------+------------+

| Diane     | Murphy    | USA       | CA         |

| Mary      | Patterson | USA       | CA         |

| Jeff      | Firrelli  | USA       | CA         |

| William   | Patterson | Australia | NULL       |

| Gerard    | Bondur    | France    | NULL       |

| Anthony   | Bow       | USA       | CA         |

| Leslie    | Jennings  | USA       | CA         |

| Leslie    | Thompson  | USA       | CA         |

| Julie     | Firrelli  | USA       | MA         |

| Steve     | Patterson | USA       | MA         |

| FoonYue  | Tseng     | USA       | NY         |

| George    | Vanauf    | USA       | NY         |

| Loui      | Bondur    | France    | NULL       |

| Gerard    | Hernandez | France    | NULL       |

| Pamela    | Castillo  | France    | NULL       |

| Larry     | Bott      | UK        | NULL       |

| Barry     | Jones     | UK        | NULL       |

| Andy      | Fixter    | Australia | NULL       |

| Peter     | Marsh     | Australia | NULL       |

| Tom       | King      | Australia | NULL       |

| Mami      | Nishi     | Japan     | Chiyoda-Ku |

| Yoshimi   | Kato      | Japan     | Chiyoda-Ku |

| Martin    | Gerard    | France    | NULL       |

+-----------+-----------+-----------+------------+

23 rows in set (0.02 sec)

We could make the above query return more specific results. What if we want to find only employees in USA? The following query adds an AND clause to narrow down the country:

SELECT e.firstname,

e.lastname,

state

FROM employees e INNER JOIN offices o

  ON o.officeCode = e.officeCode

WHERE  country = 'USA' ;

Here is the resulting data

+-----------+-----------+-------+

| firstname | lastname  | state |

+-----------+-----------+-------+

| Diane     | Murphy    | CA    |

| Mary      | Patterson | CA    |

| Jeff      | Firrelli  | CA    |

| Anthony   | Bow       | CA    |

| Leslie    | Jennings  | CA    |

| Leslie    | Thompson  | CA    |

| Julie     | Firrelli  | MA    |

| Steve     | Patterson | MA    |

| FoonYue  | Tseng     | NY    |

| George    | Vanauf    | NY    |

+-----------+-----------+-------+

10 rows in set (0.00 sec)


Related Discussions:- Using inner join

Autonomous versus nested transactions, Autonomous versus Nested Transaction...

Autonomous versus Nested Transactions Though an autonomous transaction is started by the other transaction, it is not a nested transaction for the reasons shown below: (i)

Query optimization, 1.( /5 marks) Suppose that a B+-tree index with the sea...

1.( /5 marks) Suppose that a B+-tree index with the search key (dept_name, building) is available on relation department. What would be the best way to handle the following selecti

Sql, Write a query to find academics that are authors and that have only ev...

Write a query to find academics that are authors and that have only ever coauthored papers with authors from institutes in the same state as their own. List their academic number,

Selecting objects in pl sql, Selecting Objects: Suppose that you have ...

Selecting Objects: Suppose that you have run the SQL*Plus script below that creates object type Person and object table persons, and that you have settled the table: CREATE

Using savepoint, Using SAVEPOINT The SAVEPOINT names and marks the pre...

Using SAVEPOINT The SAVEPOINT names and marks the present point in the processing of a transaction. Used with the ROLLBACK TO statement, the savepoints undo parts of a transac

Parameter & keyword description - functions, Parameter & Keyword Descriptio...

Parameter & Keyword Description: function_name: The user-defined function is identifying by that keyword. parameter_name: This identifies the formal parameter that

Packaging cursors, Packaging Cursors   You can split a cursor specific...

Packaging Cursors   You can split a cursor specification from its body for placement in a package. In that way, you can change the cursor body without changing the cursor spec

Declaring and initializing objects in pl/sql, Declaring and Initializing Ob...

Declaring and Initializing Objects: An object type is once defined and installed in the schema; you can use it to declare the objects in any PL/SQL, subprogram, block or packa

Sql operators, SQL Operators The PL/SQL uses all the SQL set, compariso...

SQL Operators The PL/SQL uses all the SQL set, comparison, and row operators in the SQL statements. This part briefly describes some of these operators.  1. Comparison Opera

Use tsql function sql server 2012, I want someone to write a TSQL function ...

I want someone to write a TSQL function that returns the name of the ODBC DSN. I will use the queries below, to get information about the connection, but none of these return th

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