Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

284
Vistas
SQL: JOIN Syntax Variation

I am using PostgreSQL, but I think the question applies to other SQLs as well.

I was editing a JOIN clause, and I ended up with the following syntax.

tableA JOIN tableB JOIN tableC ON tableB.fk = tableC.pk ON tableA.pk = tableB.fk;

That is, I have 2 JOIN clauses followed by 2 ON clauses. The fields fk and pk are my primary keys, abbreviated for this example.

I tested before I noticed the syntax. What surprised me is that it worked.

The question is: is that a proper variation of the syntax, and how is it interpreted?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

What you have written is correct. You can also write it as :

tableA JOIN tableB ON tableA.pk = tableB.fk JOIN tableC ON tableB.fk = tableC.pk

Both the queries gives the same result though :)

over 4 years ago · Santiago Trujillo Denunciar

0

It is legal, though not common.

To interpret it, work from the inside out. First evaluate tableB JOIN tableC, and use the first ON expression. It helps, because this part makes sense all by itself:

tableB JOIN tableC ON tableB.fk = tableC.pk

The entire result set from that expression then becomes the 2nd-term/right-hand-side for the outer join expression, giving you this:

tableA JOIN (previous expression) ON tableA.pk = tableB.fk

One thing to remember is that if any records were filtered by the first/inner expression, they are not considered at all for the outer expression. It makes no difference in this example, but it can change things when OUTER joins are involved.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda