Labels

2017 absence absence management Account accounting AIM aliases AME and API application application utilities lookups approval assignments ATO Australia Bank bi publisher budget business business groups CAGR candidates cartisian product case CEMLI Center Stage channels Classification competency concurrent Configuration configuration profile constants contextualization conversion correction cost costing coverage area customization data database date DateTracked deductions define design develop DFF diagnostics document earnings ebs EIT Element employee enhancements erp excel expression extension failure Fastformula FBT Flexfield FND fndload foreign key forms Formula fringe benefit FRM-40654 from FTE Functions fund fusion GL global transfer grade help hierarchy HR HRMS human resource management system implementation income information interfaces internet interview job join key flexfield KFF KPI language learning leave legal employer legislation links lists localization location management New Year obia obiee OLF onboarding oracle oracle applications oracle descriptive flex field oracle descriptive flexfield oracle ebs oracle erp oracle fusion HCM oracle hrms oracle hrms interview questions oracle hrms modules oracle hrms modules list oracle hrms organization oracle hrms table oracle hrms tables oracle hrms tutorial oracle irecruitment oracle legal entities oracle lookups oracle organization hierarchy oracle payroll oracle payroll interview questions oracle payroll tables oracle self service order by Organization organization type otbi package package body package specification patch payg Payment payroll people group perform person personalisation phase pl/sql position primary key process profile programs project qualifier Query question questions Recruiting Center Recruitment regex regular expression reporting tool reports requests requirement requisition resume retropay RICE salary schema security profile select SIT smartorg sql statutory stores STP Super Superannuation system systems Table Taleo taleo 15B Taleo Recruitment tax termination test testing trunc update user group user management user type value set variables View Views Web ADI webadi where work relationship

Tuesday, 16 December 2014

Table Joins in SQL - Part I



In Oracle as data is not stored in a single table, we usually have to draw data from multiple tables together in a meaningful way. To show this data from multiple tables in one query, Oracle allows us to perform table joins. 


Here are the two rules you need to remember for table joins. Data from two (or more) tables can be joined, if the same column/s (under the same or a different name) appears in both tables, and the column/s is the primary key (or part of that key) in one of the tables. Having a common column in two tables implies a relationship between the two tables. The nature of that relationship is determined by which table uses the column/s as a primary key. This begs the question, what is a primary key? A primary key is a column in a table used for identifying the uniqueness of each row in a table. The table in which the column appears as a primary key is referred to as the parent table in this relationship (sometimes also called the master table), whereas the column that references the other table in the relationship is often called the child table (sometimes also called the detail table). The common column appearing in the child table is referred to as a foreign key.




To illustrate this, let us look two of the most frequently used tables in Oracle HRMS viz – PER_ALL_PEOPLE_F and PER_ALL_ASSIGNMENTS_F. As highlighted the table per_all_people_f has the combination of person_id, effective_start_date and effective_end_date as its primary key. Table per_all_assignments_f has assignment_id, effective_start_date and effective_end_date as its primary key. Here the common column person_id is foreign key in the child table per_all_assignments_f.

JOIN SYNTAX

Let's now look at a simple join statement using the Oracle traditional syntax, where we join the contents of the people and assignment tables together to obtain a listing of all employees, along with their assignment details
select *
from    per_all_people_f papf,
          per_all_assignments_f paaf
where paaf.person_id = papf.person_id

Note the many important components in this table join. Listing two tables in the ‘from’ clause clearly indicates that a table join is taking place. Note also that each table name is followed by a word: papf for people table and paaf for assignment table. This demonstrates an interesting concept—just as columns can have aliases, so too can tables. The aliases serve an important purpose—they prevent Oracle from getting confused about which table to use when listing the data in the common columns. Remember, both people and assignment tables have several columns with same name like – effective_start_date, effective_end_date, person_id, etc. You can also avoid ambiguity in table joins by prefixing references to the columns with the table names, but this often requires extra coding. You can also give the column two different names, but then you might forget that the relationship exists between the two tables. It's just better to use aliases! 


No comments:

Post a Comment