Solved

DB2 Join shortcuts - isn't there a special way you can join if the column name is the SAME in the 2 tables being joined

Posted on 2014-09-24
8
513 Views
Last Modified: 2014-09-25
I seem to recall reading there is a special way you can join if the column name is the SAME in the 2 tables being joined.  The perk to using this method is that when joining on this same column, the result set only gets the column one time.  So if you used this other syntax instead of "from t1 left join t2 on t2.mycol = t1.mycol" then "mycol" would only exist once in the joined result set.

Or maybe I just dreamed that?
0
Comment
Question by:Ray
  • 4
  • 2
  • 2
8 Comments
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
That's called a Natural Join, but I can't recall any popular DBMS supporting that. And it is always an Inner Join by definition.
0
 
LVL 10

Author Comment

by:Ray
Comment Utility
whats the specific syntax because it's not the typical ...
from t1 left join t2 on t2.mycol = t1.mycol
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
I don't get what you ask for.

Left Join is one kind of Outer Join, allowing one side (the right in this case) to be not matched and still return rows.

The Natural Join has theoretical syntax descriptions, but no implementation in DB2 and many more DBMS. It is "implemented" manually by enumerating the disjunct columns of at least one of the tables, so no change for you. And as said, Natural Join cannot be an Outer Join (left or right or full).
0
 
LVL 18

Accepted Solution

by:
daveslash earned 500 total points
Comment Utility
It works for me on DB2 for i :

select *              
  from deleteme 
  join deleteme2
 using (commonColumn)

Open in new window


HTH,
DaveSlash
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 10

Author Closing Comment

by:Ray
Comment Utility
"Using" is what I couldn't remember!  Thanks Dave :-)
0
 
LVL 10

Author Comment

by:Ray
Comment Utility
Thanks for the effort Qlemo.
0
 
LVL 18

Expert Comment

by:daveslash
Comment Utility
I realize this question is already answered, but here's another neat trick for shortening your queries that works beautifully on DB2 for i (v6.1) :

Instead of this:

SELECT someStuff
  FROM Table1 a
  left outer join Table2 b
    on b.column1 = a.column1
   and b.column2 = a.column2
  left outer join Table3 c 
    on c.column1 = b.column1
   and c.column2 = b.column2
   and c.column3 = b.column3
   and c.column4 = b.column4

Open in new window

You can do this:

SELECT someStuff
  FROM Table1 a
  left outer join Table2 b 
    on (b.column1,b.column2) = (a.column1,a.column2)
  left outer join Table3 c
    on  (c.column1,c.column2,c.column3,c.column4) =
        (b.column1,b.column2,b.column3,b.column4)

Open in new window


HTH,
DaveSlash
0
 
LVL 10

Author Comment

by:Ray
Comment Utility
VERY nice Dave!!!  I hope I remember this long enough to use it and turn it into a habit.  Thanks for the extra 'tip'!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I guess that all of us know that caching the data usually increase the performance, but I worried if all of us are aware about the risk that caching the data provides and how to minimize this.  That’s the reason why I decided to write this short art…
Read about achieving the basic levels of HRIS security in the workplace.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

9 Experts available now in Live!

Get 1:1 Help Now