2 ms·
> Why indent the joins? > Are table2 and table3 less important than table1? It's often arbitrary which tables are joined and which one is from'd. But the joi
by combatentropy 10y ago
> Why indent the joins?
> Are table2 and table3 less important than table1?
It's often arbitrary which tables are joined and which one is from'd. But the joins are part of the from-clause. They all join together into one big from.
> Why place some predicates in the where-clause and
> others in the join-clauses? What's the thinking here?
> Why not put all predicates up in the join-clauses,
> nearer to the tables that they affect?
The join conditions are just to line up the rows of the different tables with each other, to avoid a cartesian product, to form one big table.
This giant table is then filtered through the where-clause, like a funnel. You can put the filters in the joins, and I have in the past, but putting them in the where-clause better reflects the picture in my head.
Tangentially, it would have been better if SQL had the select-clause after the from- and where-clauses:
from table1 t1
join table2 t2 on t1.col2 = t2.col1
join table3 t3 on
t1.col3 = t3.col1 and
t3.col2 = something_else
where
t1.col1 > 0 and
t2.col2 <> t1.col4
select
t1.col1,
t2.col2,
t3.col3
To understand the select-clause, I always first have to jump down to the from-clause anyway. This would also mirror the other statements: insert, update, and delete, which begin with the table names.
This better reflects the flow of data. First you decide the source of data (which tables). Then you filter down to which records (which rows, the where-clause). Finally you determine which fields to get (which columns, the select-clause).