Format SQL

Tidy up the layout of a statement or a whole script without changing what it does.

Format SQL rewrites the layout of your query so it's easier to read. It works on the current statement, on a selection, or on everything in the editor.

Format commands

CommandShortcutWhat it formats
Query ▸ Format SQL⌥⌘FThe selection, or the statement under the cursor when nothing is selected.
Query ▸ Format All⌥⇧⌘FThe whole editor.
Format button—Formats from the query tab's toolbar, without the keyboard.

The hint on the right of the query toolbar reminds you of the shortcut, for example ⌥⌘F format. If you change the shortcut in Settings ▸ Shortcuts, the hint follows.

What changes

The formatter only touches two things:

  • Whitespace — line breaks, indentation and spacing.
  • Keyword case — SQL keywords are written in a consistent case.

Table names, column names, string literals, numbers and comments are kept as they are.

A safe rewrite

After formatting, Mantidb reads the new text back and checks that it contains exactly the same tokens as the original. If anything would differ, the text is left unchanged. Formatting never changes what your query does.

Undo

Each format is a single undo step. Press ⌘Z (Edit ▸ Undo) to go back to the previous layout.

SQLite column names

In SQLite, a result column without an alias is named after the text of its expression. Formatting can change the case of that text — for example, count(*) becomes COUNT(*) — so the column name in the results changes too. Add an alias (count(*) AS total) if you rely on the column name.