When is a good situation to use a full outer join?

后端 未结 7 1554
耶瑟儿~
耶瑟儿~ 2020-12-23 02:07

I\'m always discouraged from using one, but is there a circumstance when it\'s the best approach?

相关标签:
7条回答
  • 2020-12-23 02:26

    The rare times i have used it has been around testing for NULLs on both sides of the join in case i think data is missing from the initial INNER JOIN used in the SQL i'm testing on.

    0 讨论(0)
  • 2020-12-23 02:34

    I've used full outer joins when attempting to find mismatched, orphaned data, from both of my tables and wanted all of my result set, not just matches.

    0 讨论(0)
  • 2020-12-23 02:35

    Just today I had to use Full Outer Join. It is handy in situations where you're comparing two tables. For example, the two tables I was comparing were from different systems so I wanted to get following information:

    1. Table A has any rows that are not in Table B
    2. Table B has any rows that are not in Table A
    3. Duplicates in either Table A or Table B
    4. For matching rows whether values are different (Example: The table A and Table B both have Acct# 12345, LoanID abc123, but Interest Rate or Loan Amount is different

    In addition, I created an additional field in SELECT statement that uses a CASE statement to 'comment' why I am flagging this row. Example: Interest Rate does not match / The Acct doesn't exist in System A, etc.

    Then saved it as a view. Now, I can use this view to either create a report and send it to users for data correction/entry or use it to pull specific population by 'comment' field I created using a CASE statement (example: all records with non-matching interest rates) in my stored procedure and automate correction, etc.

    If you want to see an example, let me know.

    0 讨论(0)
  • 2020-12-23 02:38

    I noticed that the wikipedia page provides an example.

    For example, this allows us to see each employee who is in a department and each department that has an employee, but also see each employee who is not part of a department and each department which doesn't have an employee.

    Note that I never encountered the need of a full outer join in practice...

    0 讨论(0)
  • 2020-12-23 02:41

    They're handy for finding orphaned data but I rarely use then in production code. I wouldn't be "always discouraged from using one" but I think in the real world they are less frequently the best solution compared to inners and left/right outers.

    0 讨论(0)
  • 2020-12-23 02:42

    In the rare times that I used Full Outer Join it was for data analysis and comparison purpose such as when comparing two customers tables from different databases to find out duplicates in each table or to compare the two tables structures, or to find out null values in one table compared to the other, or finding missing information in one tables compared to the other.

    0 讨论(0)
提交回复
热议问题