Way to Join only the first record

Dear Experts,

 How Do I in Oracle SQL make Left join between two tables which take only the first record in the right join?

For Example
Table 1:
A  100
B  200
C  300

A 900
A 300

The Result will be
A 100 900
B 200 null
C 300 null

Jamil MuammarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Further constraints?

Otherwise just use a sub-query as source for the left joined table, which return a single row.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
What determines the "first" row - maximum value maybe?
I agree you should set up a subquery as table source, which delivers only those rows you want to have joined. As soon as you are able to write up that query, the remainder is easy.
awking00Information Technology SpecialistCommented:
This can work for your example, but be careful since any inserts and/or deletes can radically change the results
select t1.col1 as t1_value, t2.col1 as t2_value from
(select col1, rownum rn from table1) t1
left join
(select col1, rownum rn from table2
 where rownum = 1) t2
on t1.rn = t2.rn;
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
awking00, that can't work, because the row numbers of each table have no connection to each other, and rownum is the row number of the select, not the table.
awking00Information Technology SpecialistCommented:
Not only can it work, it does work. However, it's only because the records were inserted in the order of Jamil's example data and not modified in any way.
insert into table1 values(100);
insert into table1 values(200);
insert into table1 values(300);
insert into table2 values(900);
insert into table2 values(300);
SQL> select * from table1;
SQL> select * from table2;
SQL> select col1, rownum rn from table1;
      COL1         RN
---------- ----------
       100          1
       200          2
       300          3
SQL> select col1, rownum rn from table2;
      COL1         RN
---------- ----------
       900          1
       300          2
SQL> select t1.col1 as t1_value, t2.col1 as t2_value from
  2  (select col1, rownum rn from table1) t1
  3  left join
  4  (select col1, rownum rn from table2
  5   where rownum = 1) t2
  6  on t1.rn = t2.rn;

---------- ----------
       100        900

Until we are given a precise and meaningful determination of what constitutes the first row (in both tables), the use of rownum is a readily available option. I could have also used row_number() over (partition by rowid) as rn and it would have produced the same results but, again with deletions and inserts would become skewed.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.