SQL Tools

SQL tooling: formatting, keyword lookup and statement reference covering SELECT clause execution order, JOIN selection, aggregate versus window functions, and the WHERE/HAVING stage difference.

SQL is the standard language for interacting with databases, with many details and per-database dialect differences.

Approach Comparison

AspectWHEREHAVING
When it runsBefore GROUP BYAfter GROUP BY
Can use SELECT aliasesNoYes
Can aggregateNoYes
Operates onIndividual rowsGrouped result set
Index usageCan use an indexUsually cannot
Typical useFilter detail rowsFilter aggregates (count > 100)
Common mistakePutting aggregate conditions hereUsed as a WHERE substitute
NULL semanticsUse IS NULL to match emptiesSame

Edge Cases

  • Adding an aggregate condition to WHERE raises a non-aggregated column error, because it runs before grouping.
  • HAVING can use SELECT aliases while WHERE cannot — a direct consequence of execution order.
  • Filtering a right-side column in WHERE degrades a LEFT JOIN into an INNER JOIN; put such conditions in ON instead.
  • COUNT(*) counts rows while COUNT(column) skips NULLs, so results can differ substantially.

Common Pitfalls

  • Writing an aggregate filter in WHERE and only switching to HAVING after the error, when the condition was in the wrong stage all along.
  • Putting a left-side condition in the LEFT JOIN ON clause, which filters nothing and produces a cartesian product.
  • Assuming HAVING replaces WHERE, discarding detail rows before aggregation.
  • Using NOT IN against a subquery containing NULL, which returns nothing; use NOT EXISTS or filter the NULLs first.

Related Tools