Common Mistakes and Missed Optimization Opportunities in SQL
In regards to formatting sql, I used to do it the way shown, but a coworker formatted the columns in the select with the commas in front. This seemed strange to me until I tried it. I realized that this solved the problem of sometimes a query would be changed and the last item in the select list would be removed, but the last comma would not be removed. Or, a new item was added to the end of the select list, but they neglected to add in a comma at the end of the previous last item.<p>SELECT<p><pre><code>  col1

  ,col2

  ,COUNT(col3)
</code></pre>
FROM<p><pre><code>  t1

  JOIN t2 ON ta.pk = t2.fk
</code></pre>
WHERE<p><pre><code>  col1 = col2

  AND col3 > col4
</code></pre>
GROUP BY<p><pre><code>  col1

  ,col2
</code></pre>
HAVING<p><pre><code>  COUNT(col3) > 1</code></pre>