Oracle Join SQL

Posted on 2014-08-12
Last Modified: 2014-08-22

I have a select query in Oracle like the followings. The query brings up the matching records have both TBL1.col2 and TBL2.col2 not equal to null. But if the records have null values in both TBL1.col2 and TBL2.col2, it won't bring up the records. Is there any work aroud? I need to show them as well.

Select *
From TBL1 left join TBL2
on TBL1.col1=TBL2.col1 and TBL2.col2=TBL2.col2

Question by:mrong
    LVL 76

    Accepted Solution

    Two ways I can think of:
    ... and nvl(TBL1.col2,'ziggy')=nvl(TBL2.col2,'ziggy')

    Just pick a word that should NEVER appear in the column.

    on TBL1.col1=TBL2.col1 and ( TBL1.col2=TBL2.col2 or (TBL1.col2 is null and TBL2.col2 is null))
    LVL 47

    Expert Comment

    afraid I don't get it: what is the purpose of "and TBL2.col2=TBL2.col2" at all

    is it a mistake? did you mean:

    Select *
    From TBL1 left join TBL2
    on TBL1.col1=TBL2.col1 and TBL1.col2=TBL2.col2 ?

    If so  I would suggest:

    Select *
    From TBL1 left join TBL2
    on TBL1.col1=TBL2.col1 and ( TBL1.col2=TBL2.col2 )  or ( TBL1.col2 is null and TBL2.col2 is null ))
    LVL 31

    Expert Comment

    Can you provide some sample data with results that represent what you're seeing and what you would like it to be?

    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
    PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
    This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
    This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now