SQL Indentation: 2 Spaces, 4 Spaces, or Tabs? Formatting Standards
By Sharon Ben-Moshe · October 12, 2026 · 1 min read
Sharon Ben-Moshe is the founder of sqlfmt, a browser-based SQL formatter and validator with misspelled-keyword suggestions across PostgreSQL, MySQL, SQLite, SQL Server, and BigQuery.
Resolve the indentation debate in SQL. Compare 2 spaces, 4 spaces, and tabs. Learn clause alignment styles and subquery indentation depth.
Quick answer
The industry standard for modern SQL codebases is 2 spaces per indentation level. While 4 spaces and tabs remain common in legacy environments, 2-space indentation prevents wide horizontal scrolling in nested CTEs, window functions, and subqueries while rendering consistently across git diffs, terminals, and code review tools.
FAQs
Why do modern SQL style guides prefer 2 spaces over 4 spaces?
SQL queries frequently nest multiple clauses (WITH, SELECT, FROM, JOIN, ON, WHERE, GROUP BY, HAVING, ORDER BY). With 4 spaces, deeply nested subqueries and CASE statements quickly exceed 100 characters per line, forcing horizontal scrolling.
Why are tabs problematic in SQL scripts?
Different tools and editors interpret tabs differently (rendering them as 2, 4, or 8 spaces). In Git pull requests and terminal diffs, tab misalignment disrupts clause visual hierarchy.
What is 'river alignment' in SQL?
River alignment right-aligns major SQL keywords (SELECT, FROM, WHERE) so their trailing characters form a vertical line. While visually stylized, it is difficult to maintain without specialized tooling and creates noisy git diffs when adding keywords.
Should subqueries inside WHERE clauses be indented?
Yes. The enclosing parentheses of a subquery should be on their own line or indented, with the inner query indented one level deeper (2 spaces) to visually isolate scope.
Keep reading
Building an Enterprise SQL Style Guide: Formatting Standards, Linting, and Team Conventions
By Sharon Ben-Moshe · Oct 13, 2026
The complete blueprint for an enterprise SQL style guide. Standardize formatting, keyword casing, indentation, naming conventions, and CI linting.
Read postThe Master Guide to SQL Common Table Expressions (CTEs): Syntax, Optimization Fences, and Recursion
By Sharon Ben-Moshe · Oct 13, 2026
The definitive guide to SQL Common Table Expressions (CTEs). Master chaining, recursive hierarchies, data-modifying CTEs, and optimization fences.
Read postTry it now
Paste your SQL into sqlfmt — format, validate, and catch typos free in your browser.