-1

I was debate with my TL over natural join, he told natural join should not be used, so on what cases we are not going with natural join, equjoin and go with inner join.

Rich Benner
  • 7,873
  • 9
  • 33
  • 39
  • 3
    I'm assuming TL stands for Team Lead? He says don't use natural joins so don't use them. What's the problem? What, exactly, is your question? – Rich Benner Jul 08 '16 at 10:12
  • Possible duplicate of [Difficulties in understanding natural join](http://stackoverflow.com/questions/14546617/difficulties-in-understanding-natural-join) – underscore_d Jul 08 '16 at 10:17
  • Since I can't flag it more than once: This question is not about code (perhaps better for DBA, but even there it's also the following), too broad, too opinionated, and a duplicate: http://stackoverflow.com/questions/14546617/difficulties-in-understanding-natural-join / http://stackoverflow.com/questions/8696383/difference-between-natural-join-and-inner-join – underscore_d Jul 08 '16 at 10:18
  • @RichBenner, yes TL stand for Team Lead,my question is what problem occur when we use natural join. – Abhay Mishra Jul 08 '16 at 10:21
  • @AbhayMishra does it matter? You've been told not to use them. End of. – Rich Benner Jul 08 '16 at 10:22
  • 1
    @RichBenner That is like saying "well, we've always done it this way." Definitely, Abhay needs to respect his authority, but just not even care to learn SQL? No way. – clifton_h Jul 08 '16 at 11:45
  • 2
    @clifton_h yeah, I understand that. My issue is with a single sentence question showing no indication of research or an information on the discussion other than the fact that there was a debate. Also, the question doesn't make any sense to me either, It shouldn't be difficult to understand the question if meaningful responses are expected. – Rich Benner Jul 08 '16 at 11:55

1 Answers1

1

Please rephrase your question like What are the implications between a Natural Join and Inner JOIN? 'What are the limitations of these JOINS in SQL Server 2008?

SQL Server gets rid of Natural Joins, so there also is a scalable aspect to their usage. Without getting too specific, a NATURAL JOIN is essentially like an INNER JOIN except it

  • A) returns distinct columns (think INTERSECT/UNION except tables can have differing columns)
  • B) add implicitly an EQUI JOIN on all of the available columns.

Illustration: Note, this was designed in SQL Server 2012

  DECLARE @TableA TABLE (Col1 VARCHAR(10)
                       , Col2 VARCHAR(10) );
  DECLARE @TableB TABLE (Col1 VARCHAR(10)
                       , Col2 VARCHAR(10)
                       , Col3 VARCHAR(10) );
  INSERT INTO @TableA (Col1, Col2)
  VALUES ('C', 'D');
  INSERT INTO @TableB (Col1, Col2, Col3)
  VALUES ('C', 'D', 'E');

  SELECT *
  FROM  @TableA
  NATURAL JOIN (SELECT Col1, Col2, Col3
                FROM    @TableB) AS B

  -- returns
  Col1 | Col2 | Co3
  'C'    'D'    'E'

  SELECT *
  FROM  @TableA AS A
  INNER JOIN (SELECT Col1, Col2, Col3
              FROM  @Table B) AS B ON A.Col1 = B.Col1
                                  AND A.Col2 = B.Col2

  -- returns A = @TableA, B = @TableB
  A.Col1 | B.Col1 | A.Col2 | B.Col2 | B.Col2
    'C'     'C'      'D'      'D'      'E'

Do you see the difference? Rather significant, no? With the Inner you still could compare the two table's results, but a Natural JOIN is like a INTERCEPT, only it groups the columns together. You lose the relation in your result set.

Conclusion:

  • Everyone is entitled to their own opinion, but SQL will parse your query the same regardless.
  • Learning about how the joins work helps you understand what business use those joins can function...or at least work with other SQL languages.
  • TSQL removed the NATURAL JOIN in favor of UNION, UNION ALL, INTERSECT and EXCEPT.

If you can, ask your TL the 'whys' behind the business logic of using one or the other. Find out how he understands SQL Querying. You might either get something insightful or...well, be prepared to hear unconventional things...but at least you are learning more about SQL, your company, and how to ask questions.

A Win/Win, I say.

clifton_h
  • 1,298
  • 8
  • 10
  • ,thanks know I clear about NAUTRAL JOIN and INNER JOIN,will you please help to know why EQUJOIN and FULLJOIN are less used in sql server. – Abhay Mishra Jul 08 '16 at 12:46
  • "SQL Server gets rid of Natural Joins" - and yet, here you are, using `NATURAL JOIN` in SQL Server. Is there a version number missing from that statement? Or what did you really mean? – underscore_d Jul 08 '16 at 18:40
  • Also, "it groups the **columns** together. You lose the relation in your result set." Seems to me that it _enforces_ the relation. You just lose having both columns used to `join` returned to you, which obviously just have 2 copies of a guaranteed identical value anyway, so that's not actually a loss. Or, again, am I missing something? – underscore_d Jul 08 '16 at 18:43
  • @underscore_d it is tagged SQL 2008 and by losing relationally, if one used a CTE with this, you would not be able to distinguish the tables on similar columns (not that this is too much of a problem). – clifton_h Jul 08 '16 at 18:48
  • @underscore_d if you have a better way of describing how the data is grouped, ID like to hear it. – clifton_h Jul 08 '16 at 18:49