SQL Formatter — SELECT


← SQL Formatter overview

Contents


SELECT column list

Each column expression is placed on its own line, indented one level from SELECT. If the entire column list is short enough (controlled by SELECT column list and its max length), all columns are kept on the same line as SELECT.

Short list on one line:

SELECT id, name, salary FROM employees;

Expanded — one column per line:

SELECT
  employee_id,
  last_name,
  salary
FROM employees
WHERE department_id = 10;

Column alias alignment

When Align column aliases is enabled, the formatter right-pads each column expression with spaces so that all AS aliases line up in a single column. Lines inside subqueries embedded in the column list do not affect alias alignment of the outer query.

Example — expanded with alias alignment:

SELECT --+hint
  (SELECT 1 FROM dual) AS "1",     -- dummy subquery
  employee_id          AS emp_id,  -- employee
  first_name           AS name,    -- name
  department_id        AS dept     -- dept
FROM employees                     -- emp table

In this example:


Brick list mode

Normal expanded layout places each column on its own line. Brick list mode is a compact alternative: instead of one column per line, items are packed into rows with a configurable maximum line length and a maximum number of items per row, producing a tight rectangular block.

Brick list mode applies to SELECT column lists when all of the following are true:

Example — 8 columns with max 60 chars and max 3 items per row:

SELECT
  col1, col2, col3,
  col4, col5, col6,
  col7, col8
FROM t;

Each row starts at the column position of the first item on the first row (aligned under the character after SELECT ). Items within a row are separated by a single comma and padded with spaces so they line up in neat columns — hence the name “brick”.

Note: Brick list mode and alias alignment are mutually exclusive for the same column list. When brick layout is active, alias alignment is not applied to that list.


FROM clause

Each table or join expression is placed on its own line. If the table list is short (controlled by FROM table list and its max length), it is kept on the same line as FROM.

JOIN keywords (INNER JOIN, LEFT JOIN, etc.) always start on a new line at the clause indentation level. The ON and USING keywords start on a new indented line below the join.

Example:

SELECT *
FROM employees e
JOIN departments d
  ON e.department_id = d.department_id
LEFT JOIN locations l
  ON d.location_id = l.location_id

WHERE / AND / OR

WHERE starts on its own line. Each AND and OR condition starts on a new indented line.

AND inside a BETWEEN … AND … expression is never treated as a new-line trigger. The formatter tracks nesting depth so the correct AND is identified.

Example:

WHERE salary BETWEEN 1000 AND 5000
  AND department_id = 10
  OR job_id = 'MGR'

CASE expressions

Multi-line CASE expressions are indented as follows:

Example:

CASE
  WHEN salary > 10000
    THEN 'High'
  WHEN salary > 5000
    THEN 'Medium'
  ELSE 'Low'
END

The CASE WHEN max length setting controls whether a short WHEN … THEN … pair is kept on one line. Set to 0 (the default) to always expand CASE vertically.

If a CASE is wrapped in parentheses and the entire block fits, it is kept on a single line:

SELECT
  (CASE WHEN active = 1 THEN 'Yes' ELSE 'No' END) status
FROM employees;

GROUP BY and ORDER BY

GROUP BY and ORDER BY each start on their own line. If the list is short (controlled by GROUP BY list / ORDER BY list and their max length settings), it is kept on the same line as the keyword.

Short list on one line:

GROUP BY department_id, job_id
ORDER BY salary DESC

Expanded:

GROUP BY department_id,
  job_id,
  hire_date
ORDER BY salary DESC,
  last_name

Set operators (UNION / INTERSECT / MINUS)

UNION, UNION ALL, INTERSECT, and MINUS each start on their own line at the root indentation level. The following SELECT keyword appears on the same line as the operator.

When Blank line before set ops is enabled, an extra blank line is inserted before each operator to visually separate the query branches in long compound queries.

Example:

SELECT employee_id, last_name, department_id
FROM employees
WHERE department_id = 10

UNION ALL SELECT employee_id, last_name, department_id
FROM contractors
WHEREdepartment_id = 10;

Linked brick layout across set-operation branches

When brick list mode is active and a SELECT column list in a compound query wraps across multiple rows, the Linked UNION / INTERSECT / MINUS setting (default: on) causes every subsequent branch to wrap its column list at exactly the same item positions as the first branch.

This means the columns of all branches line up vertically across the entire compound query, making it much easier to compare corresponding values row by row.

Example — linked brick, 3 items per row:

SELECT employee_id, last_name, department_id,
  job_id, hire_date, salary

UNION ALL SELECT employee_id, last_name, department_id,
  job_id, hire_date, salary
FROM contractors;

Without linking, each branch would calculate its own wrap positions independently, and the columns would not necessarily align.

Note: Linking only applies when the number of columns in all branches is identical. If a branch has a different column count, it falls back to independent brick layout.


Subqueries and parenthesised blocks

When an opening parenthesis ( is encountered, the formatter measures the entire block up to the matching ):

Alias alignment note: Lines inside a subquery in the SELECT list are excluded from the outer query’s alias alignment pass, so they do not push aliases unnecessarily far to the right. Lines containing multi-line expressions such as DECODE(…) or NVL(…) are included in alignment as normal.


← SQL Formatter overview   |   INSERT / UPDATE / DELETE / MERGE →