JOIN Formatting Styles in SQL
A query with one JOIN reads fine no matter how you format it. A query with six JOINs does not. How you place the JOIN keyword, the table, and the ON condition decides whether the relationships between tables are visible at a glance or buried in a wall of text.
Compact: JOIN and ON on one line
SELECT o.order_id, c.first_name, p.product_name
FROM orders o
JOIN customers c ON c.customer_id = o.customer_id
JOIN order_items i ON i.order_id = o.order_id
JOIN products p ON p.product_id = i.product_id
WHERE o.order_date >= '2024-01-01'; Every join is one line, so the query stays short and the list of joined tables reads like an index. This works well while the ON conditions stay simple. Once a join has two or three conditions, the line grows past the screen edge and the advantage disappears.
Expanded: ON on its own line
SELECT o.order_id, c.first_name, p.product_name
FROM orders o
JOIN customers c
ON c.customer_id = o.customer_id
JOIN order_items i
ON i.order_id = o.order_id
AND i.status = 'shipped'
JOIN products p
ON p.product_id = i.product_id
WHERE o.order_date >= '2024-01-01'; Separating the condition from the table gives each part room. Additional conditions line up under the first, so a join with three predicates is as readable as one with a single predicate. The query gets longer vertically, which is the trade.
Aligned: keywords on a right-hand axis
SELECT o.order_id, c.first_name, p.product_name
FROM orders o
JOIN customers c ON c.customer_id = o.customer_id
JOIN order_items i ON i.order_id = o.order_id
JOIN products p ON p.product_id = i.product_id
WHERE o.order_date >= '2024-01-01'; Keywords end at a common column and table aliases align, which makes the structure scannable top to bottom. This is the same principle as River Style applied to joins. It looks precise, but every manual edit risks breaking the alignment — it only stays clean if a formatter maintains it.
Which one when
| Situation | Compact | Expanded | Aligned |
|---|---|---|---|
| Simple one-condition joins | ✓ | ||
| Multi-condition or outer joins | ✓ | ||
| Queries reviewed in diffs | ✓ | ||
| Team-wide standard, tool-enforced | ✓ | ||
| Ad-hoc queries in a console | ✓ |
A note on explicit join types
Whichever layout you choose, spell out the join type. JOIN and INNER JOIN are identical to the parser, but LEFT JOIN next to a bare JOIN makes a reader stop and check. Writing INNER JOIN everywhere costs six characters and removes the doubt. The same applies to LEFT OUTER JOIN over LEFT JOIN if your team leans verbose — consistency matters more than which form you pick.
Switch styles with one click
SQLinForm formats all three layouts and converts between them without manual editing. Try it in the online formatter.
For related layout decisions, see Leading vs Trailing Commas in SQL and Cascading Style vs River Style.