Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

290
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!