How to Format SQL Window Functions Readably
By Sharon Ben-Moshe · June 25, 2026 · 3 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.
Window functions cram PARTITION BY, ORDER BY, and frames into one OVER clause. Here's how to format SQL window functions so the logic reads at a glance.
Quick answer
To format SQL window functions readably, break the OVER clause onto multiple lines when it has both PARTITION BY and ORDER BY, putting each on its own indented line. For repeated windows, define them once with a named WINDOW clause. Keep single-keyword OVER clauses like ROW_NUMBER() OVER (ORDER BY id) on one line.