Writing more legible SQL
Here is how I write SQL:<p><pre><code>    select
        t1.col1,
        t2.col2,
        t3.col3
    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 &gt; 0 and
        t2.col2 &lt;&gt; t1.col4
    order by col2
    limit 100
</code></pre>
So:<p>1. SQL capitalization is not sacred. I lowercase everything.<p>2. I just indent subclauses, with four spaces, like I indent other languages. I don&#x27;t go out of my way to line up things vertically.<p>3. Conjunctions like &quot;and&quot; are put at the end of a line, like a comma, not at the beginning. I think it lines things up better vertically.<p>4. I try to keep lines short, but I also try to keep it from getting needlessly long vertically. So if the statement is short and simple, I might fold some things back onto one line, like:<p><pre><code>    select col1, col2, col3
    from table1
    where col1 = something_or_other

</code></pre>
or even:<p><pre><code>   select * from table1 where col1 = something</code></pre>