SQLite Forum

Subqueries and "no such column" error on references to other tables
Login
1. Yes the first query will always work,
 but
2. That is not the problem with query 2 - The SELECT is very happy, it's the JOIN that is the problem because you are joining to an as-yet non existing table/view (which the join creates) but then trying to refer to chat.id in that second join-created-view, however "chat" never appears in THAT join's SELECT .. FROM, and you cannot magically grab it from some other JOIN you did earlier. 
The JOINS are not the same and won't be in the same For-Loop at the same row/position at the same moment - that only works for correlated sub-queries, which your JOIN is not.

Perhaps read up on Correlated sub-queries if what I say did not make immediate sense, once you understand the limitations of those, it should become more clear why your JOIN did not behave as expected, and how you could make that sort of thing work.

Good luck!