Help › Write SQL

Format SQL

Lay out SQL with one command, choose keyword case, indentation and comma position, and know what the formatter leaves alone.

Querlyn 0.1.0 is not released yet. This help describes it ahead of release.

Format SQL lays out what you wrote: one clause per line, consistent indentation, keywords in one case. It changes only spaces, line breaks and the case of keywords. It never changes a name, a value or the meaning of a statement.

Format

Choose Edit › Format SQL (⇧⌘F on macOS or Ctrl+Shift+F on Windows and Linux), or Format in the bar above the editor.

  • With text selected, Querlyn formats every statement the selection touches, each as a whole.
  • With nothing selected, it formats the whole tab.

Formatting is one step for ⌘Z on macOS or Ctrl+Z on Windows and Linux, and the cursor stays on the word it was on.

For example, this:

select o.id, o.total, c.name from orders o join customers c on c.id = o.customer_id where o.total > 100 order by o.total desc;

becomes:

SELECT
    o.id,
    o.total,
    c.name
FROM orders o
JOIN customers c
    ON c.id = o.customer_id
WHERE o.total > 100
ORDER BY o.total DESC;

Choose the style

Open Settings (⌘, on macOS or Ctrl+, on Windows and Linux). Under Editor:

SettingChoicesStarts as
Format: keywordsUPPER, lower, As typedUPPER
Format: indentation2 spaces, 4 spaces, Tab4 spaces
Format: commasTrailing (at the end of a line), Leading (at the start of the next)Trailing

Lines are filled up to 100 characters; that width is not a setting.

The same choices as commands:

  • Command Palette › Preferences: Format SQL: Uppercase Keywords, Command Palette › Preferences: Format SQL: Lowercase Keywords, Command Palette › Preferences: Format SQL: Keep Keyword Case
  • Command Palette › Preferences: Format SQL: Indent with 2 Spaces, Command Palette › Preferences: Format SQL: Indent with 4 Spaces, Command Palette › Preferences: Format SQL: Indent with Tabs
  • Command Palette › Preferences: Format SQL: Trailing Commas, Command Palette › Preferences: Format SQL: Leading Commas

How statements are laid out

  • A select list with two or more items is written one item per line; a single item stays on the SELECT line. The same goes for the lists of SET, RETURNING and VALUES.
  • Each JOIN starts a line, with its ON on the next line, indented.
  • GROUP BY and ORDER BY lists stay on one line when they fit.
  • AND and OR conditions go one per line only when the condition does not fit on its line.
  • A subquery or a WITH query is indented, with its closing parenthesis on a line of its own.
  • A CASE with one WHEN stays on one line when it fits; otherwise each branch gets a line.
  • Between statements, at most one blank line is kept.

What the formatter leaves alone

  • Comments stay where they are, relative to the SQL around them.
  • Names. A name that happens to be a keyword, such as a column called user or date, keeps its case.
  • Function names keep the case you wrote. Data types such as INT and TEXT follow the keyword setting.
  • Procedural code is left exactly as written: BEGIN … END blocks, the bodies of functions, procedures and triggers, DO blocks and control flow. A message says so when part of what you formatted was left as written.
  • A statement Querlyn cannot format safely is left as it was. Before anything is changed, the result is compared with the original word by word; if they differ, nothing is applied and a message says "Couldn't format safely. The SQL was left unchanged."

SQL that is not finished or not valid can still be formatted.

Messages you may see

MessageMeaning
"The SQL is already formatted."Nothing needed to change.
"There is no SQL to format."The tab is empty.
"The selection contains no SQL statement to format."Select part of a statement.
"The SQL changed while it was being formatted. Format it again."You typed while a large tab was being formatted.

What is not offered

Formatting as you type, or when you save; a setting for the line width; and styles beyond the three settings above.

Help for Querlyn 0.1.0. All help topics