0

I'm looking a SQL code and I'm not sure what it does. This is the code:

SELECT *   
FROM dba.spro_lotesfrutagranel,   
     dba.variedades,   
     dba.productores  
WHERE ( dba.variedades.espe_codigo = dba.spro_lotesfrutagranel.lote_espcod )     and  
     ( dba.variedades.vari_codigo = dba.spro_lotesfrutagranel.vari_codigo )

Does this the same as a left join? or not?

Thanks!

CuchoAv
  • 165
  • 1
  • 9
  • 4
    That is a simple INNER JOIN just written in old legacy style. – juergen d Oct 20 '16 at 18:22
  • 2
    That is an obsolete notation. Promote the use of explict `JOIN` sintaxis, Aaron Bertrand wrote a nice article [Bad habits to kick : using old-style JOINs](https://sqlblog.org/2009/10/08/bad-habits-to-kick-using-old-style-joins) about it. – Juan Carlos Oropeza Oct 20 '16 at 18:23
  • 1
    Its more than just an INNER JOIN.. its an INNER JOIN between spro_lotesfrutagranel and variedades.. then that is CROSS JOINED to productores – JamieD77 Oct 20 '16 at 18:26
  • 1
    This gives a good explanation of what gofr1 said. http://stackoverflow.com/questions/1018822/inner-join-on-vs-where-clause – MikeB0317 Oct 20 '16 at 18:29
  • I don't know why this question is downvoted! OP just wants to know what this code does! And good explanation needed, that is all. – gofr1 Oct 20 '16 at 18:36

1 Answers1

2

Don't use old style join! Use table aliases! You query should be rewriten like:

SELECT *   
FROM dba.spro_lotesfrutagranel sl
INNER JOIN dba.variedades v
ON v.espe_codigo = sl.lote_espcod and  
     v.vari_codigo = sp.vari_codigo
INNER JOIN dba.productores p
ON p.somecolumn = ...

As for now your query is a Cartesian join with some where statement, all rows from spro_lotesfrutagranel and variedades are connected by two columns both and then with ALL rows from productores.

Note: I add two links with some explanation.

Community
  • 1
  • 1
gofr1
  • 15,741
  • 11
  • 42
  • 52
  • Thanks!! I'm trying to understand a code writted by someone else...! I'm not using old style!! – CuchoAv Oct 20 '16 at 18:36
  • It is just an advices! ;) with some explanation. Cartesian join with some where statement is similar to inner join. – gofr1 Oct 20 '16 at 18:38
  • I hope you got clarification you needed. I suggest you to rewrite this part of your query as mentioned in my answer, if you need all rows from third table use CROSS JOIN – gofr1 Oct 20 '16 at 18:55