Results 1 to 5 of 5

Thread: Self Join

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2008
    Posts
    474

    Self Join

    Hey see the pic,how the query is returning 5 records,Can somebody explain me?
    Attached Images Attached Images  

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Self Join

    What you wrote is actually a cross join with a condition.
    So see it this way:
    Each record of Employee F will have a match in with each row of Employee S and vice versa. And then records in which the country of Employee F is not same as country of Employee S would be removed. What you would be left is these 5 matches.

    If you use the DISTINCT keyword, duplicate rows will be removed.
    I would also advice you to use inner joins instead of cross joins. Inner joins are much better performance wise.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Self Join

    (not tested code)
    SQL Code:
    1. Select F.EmpId, F.LastName, S.EmpId, S.LastName, F.Country
    2. FROM Employee F
    3. INNER JOIN Employee S ON F.Country = S.Country
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Self Join

    To get the unique values you can also try it like this.
    Code:
    Select F.EmpID,F.LastName,S.EmpID,S.LastName,F.Country
    FROM Employee F, Employee S
    WHERE F.Country = S.Country AND F.EmpID = S.EmpID AND F.LastName = S.LastName
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  5. #5
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Self Join

    What output did you expect or need?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width