Input SQL
Formatted SQL

About SQL Formatting

Consistently formatted SQL is easier to read, review, and debug. While SQL engines ignore whitespace, well-structured queries help teams collaborate effectively and catch logic errors early.

Why Format SQL?

  • Readability - quickly understand query structure and intent
  • Maintainability - easier to modify and extend queries over time
  • Code review - consistent formatting makes diffs cleaner and reviews faster
  • Debugging - structured layout helps isolate problems in complex queries

Formatting Conventions

  • Uppercase keywords - SELECT, FROM, WHERE for visual distinction
  • Lowercase identifiers - table and column names in lowercase or snake_case
  • Indent subqueries - one level deeper than the containing clause
  • One column per line - in SELECT lists with many columns

Common SQL Clauses

Clause Purpose Example
SELECTColumns to returnSELECT name, email
FROMSource table(s)FROM users
WHERERow filter conditionsWHERE active = true
JOINCombine related tablesJOIN orders ON users.id = orders.user_id
GROUP BYGroup rows for aggregationGROUP BY department
HAVINGFilter groupsHAVING COUNT(*) > 5
ORDER BYSort resultsORDER BY created_at DESC
LIMITRestrict row countLIMIT 100