> ## Documentation Index
> Fetch the complete documentation index at: https://cockroachlabs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# cockroach sqlfmt

export const InternalLink = ({version, path = "", children, ...props}) => {
  let detectedVersion = version || "stable";
  if (typeof window !== 'undefined' && !version) {
    const match = window.location.pathname.match(/\/docs\/([^/]+)/);
    if (match) {
      detectedVersion = match[1];
    }
  }
  const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
  return <a href={`/docs/${detectedVersion}/${normalizedPath}`} {...props}>
      {children}
    </a>;
};

The `cockroach sqlfmt`
<InternalLink path="cockroach-commands">command</InternalLink> changes the textual formatting of
one or more SQL queries. It recognizes all SQL extensions supported by
CockroachDB.

A [web interface to this feature](https://sqlfum.pt/) is also available.

## Synopsis

Use the query formatter interactively:

```shell theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
$ cockroach sqlfmt <flags>
<sql stmt>
CTRL+D
```

Reformat a SQL query given on the command line:

```shell theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
$ cockroach sqlfmt <flags> -e "<sql stmt"
```

Reformat a SQL query already stored in a file:

```shell theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
$ cat query.sql | cockroach sqlfmt <flags>
```

## Flags

The `sqlfmt` command supports the following flags.

| Flag                  | Description                                                               | Default value                            |
| --------------------- | ------------------------------------------------------------------------- | ---------------------------------------- |
| `--execute`<br />`-e` | Reformat the given SQL query, without reading from standard input.        | N/A                                      |
| `--print-width`       | Desired column width of the output.                                       | 80                                       |
| `--tab-width`         | Number of spaces occupied by a tab character on the final display device. | 4                                        |
| `--use-spaces`        | Always use space characters for formatting; avoid tab characters.         | Use tabs.                                |
| `--align`             | Use vertical alignment during formatting.                                 | Do not align vertically.                 |
| `--no-simplify`       | Avoid removing optional grouping parentheses during formatting.           | Remove unnecessary grouping parentheses. |

## Examples

### Reformat a query with constrained column width

Using the interactive query formatter, output with the default column width (80 columns):

1. Start the interactive query formatter:

   ```shell theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   $ cockroach sqlfmt
   ```

2. Press **Enter**.

3. Run the query:

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   > CREATE TABLE animals (id INT PRIMARY KEY DEFAULT unique_rowid(), name STRING);
   ```

4. Press **CTRL+D**.

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   CREATE TABLE animals (
           id INT PRIMARY KEY DEFAULT unique_rowid(),
           name STRING
   )
   ```

Using the command line, output with the column width set to `40`:

```shell theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
$ cockroach sqlfmt --print-width 40 -e "CREATE TABLE animals (id INT PRIMARY KEY DEFAULT unique_rowid(), name STRING);"
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
CREATE TABLE animals (
        id
                INT
                PRIMARY KEY
                DEFAULT unique_rowid(),
        name STRING
)
```

### Reformat a query with vertical alignment

Output with the default vertical alignment:

```shell theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
$ cockroach sqlfmt -e "SELECT winner, round(length / (60 * 5)) AS counter FROM players WHERE build = $1 AND (hero = $2 OR region = $3);"
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
SELECT
winner, round(length / (60 * 5)) AS counter
FROM
players
WHERE
build = $1 AND (hero = $2 OR region = $3)
```

Output with vertical alignment:

```shell theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
$ cockroach sqlfmt --align -e "SELECT winner, round(length / (60 * 5)) AS counter FROM players WHERE build = $1 AND (hero = $2 OR region = $3);"
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
SELECT winner, round(length / (60 * 5)) AS counter
  FROM players
 WHERE build = $1 AND (hero = $2 OR region = $3);
```

### Reformat a query with simplification of parentheses

Output with the default simplification of parentheses:

```shell theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
$ cockroach sqlfmt -e "SELECT (1 * 2) + 3, (1 + 2) * 3;"
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
SELECT 1 * 2 + 3, (1 + 2) * 3
```

Output with no simplification of parentheses:

```shell theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
$ cockroach sqlfmt --no-simplify -e "SELECT (1 * 2) + 3, (1 + 2) * 3;"
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
SELECT (1 * 2) + 3, (1 + 2) * 3
```

## See also

* [Sequel Fumpt](https://sqlfum.pt/)
* <InternalLink path="cockroach-demo">`cockroach demo`</InternalLink>
* <InternalLink path="cockroach-sql">`cockroach sql`</InternalLink>
* <InternalLink path="cockroach-commands">`cockroach` Commands Overview</InternalLink>
