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 (⇧⌘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:
| Setting | Choices | Starts as |
|---|---|---|
| Format: keywords | UPPER, lower, As typed | UPPER |
| Format: indentation | 2 spaces, 4 spaces, Tab | 4 spaces |
| Format: commas | Trailing (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:
- , ,
- , ,
- ,
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
SELECTline. The same goes for the lists ofSET,RETURNINGandVALUES. - Each
JOINstarts a line, with itsONon the next line, indented. GROUP BYandORDER BYlists stay on one line when they fit.ANDandORconditions go one per line only when the condition does not fit on its line.- A subquery or a
WITHquery is indented, with its closing parenthesis on a line of its own. - A
CASEwith oneWHENstays 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
userordate, keeps its case. - Function names keep the case you wrote. Data types such as
INTandTEXTfollow the keyword setting. - Procedural code is left exactly as written:
BEGIN … ENDblocks, the bodies of functions, procedures and triggers,DOblocks 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
| Message | Meaning |
|---|---|
| "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