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

# SQL Editor

> Write and run SQL with syntax highlighting, multi-statement execution, find and replace, and a built-in formatter

# SQL Editor

Write and run SQL with tree-sitter syntax highlighting, schema-aware autocomplete, and multi-statement execution in a single editor pane.

<Frame caption="SQL Editor with syntax highlighting">
  <img className="block dark:hidden" src="https://mintcdn.com/ngquct-feat-ai-sql-walkthroughs/Bj1EoTflf_jG6N_3/images/sql-editor.png?fit=max&auto=format&n=Bj1EoTflf_jG6N_3&q=85&s=e8df77230878fa1ac1e336bd385cebfc" alt="SQL Editor" width="1560" height="960" data-path="images/sql-editor.png" />

  <img className="hidden dark:block" src="https://mintcdn.com/ngquct-feat-ai-sql-walkthroughs/Bj1EoTflf_jG6N_3/images/sql-editor-dark.png?fit=max&auto=format&n=Bj1EoTflf_jG6N_3&q=85&s=7dcaa8c37dedf94c5b9dad63dffecbff" alt="SQL Editor" width="1560" height="960" data-path="images/sql-editor-dark.png" />
</Frame>

## Writing Queries

Separate statements with semicolons. Place the cursor in any statement and press `Cmd+Enter` to run just that one.

```sql theme={null}
SELECT * FROM users LIMIT 10;
SELECT COUNT(*) FROM orders;
```

Select text and press `Cmd+Enter` to run only the selection. Multiple statements in a selection run sequentially:

* On databases with transaction support, the batch runs in a transaction: if a statement fails, execution stops and all changes roll back. Databases without transactions run each statement as-is, with no rollback.
* The error names the failing statement ("Statement 3/5 failed: ...").
* The last `SELECT` result appears in the data grid.
* Each statement is recorded individually in query history.

The editor supports multiple cursors for editing several places at once.

Autocomplete suggests tables, columns, and keywords as you type, and resolves aliases. See [Autocomplete](/features/autocomplete).

Use `:name` placeholders instead of hardcoding values; they bind via prepared statements. See [Query Parameters](/features/query-parameters).

## Per-Tab Database Picker

The editor toolbar shows a database picker (or schema picker, depending on the engine). Each SQL tab binds to its own database:

* Changing the picker affects only that tab. Switching the active database elsewhere keeps existing tabs on their bound database.
* The picker lists the databases visible in the sidebar, so it follows the sidebar database filter.
* Databases whose driver reconnects the session to switch show a lock instead of a menu; those tabs follow the connection's active database.

## Find and Replace

Press `Cmd+F` in a query tab to open the find panel. Switch the panel to Replace mode to replace the current match or all matches.

| Action        | Shortcut      |
| ------------- | ------------- |
| Find          | `Cmd+F`       |
| Find next     | `Cmd+G`       |
| Find previous | `Cmd+Shift+G` |

Match modes: contains, matches word, starts with, ends with, or regular expression, plus match case and wrap around toggles.

## Query Execution

| Action                | Shortcut           | Description                                                                                                                          |
| --------------------- | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------ |
| Execute query         | `Cmd+Enter`        | Runs query at cursor, or all selected statements                                                                                     |
| Execute without limit | `Cmd+Option+Enter` | Runs the query at cursor without the row cap                                                                                         |
| Cancel query          | `Cmd+.`            | Stops the running query                                                                                                              |
| Explain query         | `Cmd+Option+E`     | Shows the execution plan for the query at cursor as a tree or diagram (see [Explain Visualization](/features/explain-visualization)) |
| Format query          | `Cmd+Shift+L`      | Formats the current query                                                                                                            |

### Automatic Row Limit

SELECT and WITH queries without their own `LIMIT` or `FETCH FIRST` run with the configured row cap:

* The default cap is 10,000 rows. Pick 100 to 500,000 in **Settings > Data** (**Row cap**), or turn off **Truncate query results** to disable it.
* Your query is sent exactly as you wrote it. TablePro never changes the SQL, it stops reading once it reaches the cap.
* Your own `LIMIT`, `FETCH FIRST`, or `TOP` always wins: the query is not capped for that run.
* `EXPLAIN`, `SHOW`, writes, and DDL are never limited.

When the cap trims a result, the status bar shows **truncated** with a **Fetch All** link. To skip the cap for one run, press `Cmd+Option+Enter` or use the Execute button's menu or the Query menu.

### Results

Results appear in the data grid below the editor with row count and execution time. Large result sets are paginated.

* **Result tabs**: every result gets a tab above the grid; multi-statement runs produce one tab per statement. Switch with `Cmd+Option+[` / `Cmd+Option+]`, close with `Cmd+Shift+W`. Hover a tab to see its query.
* **Pinning**: right-click a tab and choose **Pin Result**, or press `Cmd+Option+P`. The next query lands in a new tab instead of overwriting the pinned one. Pinned tabs cannot be closed or cleared until unpinned.
* **Panel**: toggle the results panel with `Cmd+Option+R` or the toolbar button; it auto-expands when a query executes. The toolbar trash button clears the query and results together; to keep the query, right-click the results and choose **Clear Results**.
* **Errors**: a red banner above the results shows the database error message with a **Fix with AI** button.
* **Non-SELECT statements** (INSERT, UPDATE, DELETE, DDL) show a success view with affected row count and execution time.

## SQL Formatting

Press `Cmd+Shift+L` to format the current query. You can also click **Format** in the toolbar or use **Query** > **Format Query**; the shortcut is rebindable in **Settings** > **Keyboard**. The token-based formatter:

* Breaks lines per clause (`SELECT`, `FROM`, `WHERE`, `JOIN`) and indents 2 spaces
* Uppercases keywords
* Preserves comments, string literals, and your cursor position
* Keeps dialect identifier quoting (MySQL backticks, PostgreSQL double quotes)
* Handles JOINs, subqueries, CASE expressions, CTEs (including recursive), window functions, and set operations

Procedural blocks (PL/pgSQL `DO`, stored procedures, T-SQL `BEGIN`/`END`) pass through with minimal changes.

**Before**:

```sql theme={null}
select u.id,u.name,count(o.id) as order_count from users u left join orders o on u.id=o.user_id where u.status='active' group by u.id,u.name having count(o.id)>5 order by order_count desc;
```

**After**:

```sql theme={null}
SELECT
  u.id,
  u.name,
  COUNT(o.id) AS order_count
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
WHERE u.status = 'active'
GROUP BY u.id, u.name
HAVING COUNT(o.id) > 5
ORDER BY order_count DESC;
```

## Vim Mode

Enable Vim keybindings in **Settings** > **Editor** > **Vim mode**. Normal, Insert, Visual, and Command-line modes are supported, with standard motions, operators, count prefixes, and text objects. `:w` executes the query and `:q` closes the tab. Jump to a line with `10G` or `10gg` (`:10` is not supported). Full key reference: [Vim Mode](/features/vim-mode).

## Editor Settings

**Settings > Editor** covers line numbers, current-line highlight, word wrap, tab width, auto-uppercase keywords as you type, query parameters, and Vim mode. Editor font family and size are set per theme in **Settings > Appearance**.

Editor windows remember their size, position, and font zoom between launches. See [Query Tabs](/features/tabs#windows-and-connections).

## AI Assistance

**Explain with AI** (`Cmd+L`) explains the query at cursor, **Optimize with AI** (`Cmd+Option+L`) suggests performance improvements, and **Fix with AI** in the error banner rewrites a failing query. Inline AI suggestions (ghost text) complete your SQL as you type: Tab accepts, Escape dismisses. See [AI Assistant](/features/ai-assistant) for setup.

## SQL Files

TablePro opens `.sql`, `.psql`, and `.pgsql` files:

* Double-click in Finder (or **Open With** > **TablePro**)
* **File** > **Open File...** (`Cmd+O`)
* Drag files onto the dock icon

Files open in a new tab. Without a connection they queue and open on connect. Opening the same file twice focuses the existing tab.

### Saving

* `Cmd+S` saves back to the source file. Untitled tabs trigger Save As.
* `Cmd+Shift+S` opens Save As.

The title bar shows the filename for file-backed tabs, with the standard macOS unsaved-changes dot on the close button. `Cmd+click` the title for the standard path menu, then pick a folder to open it in Finder. Closing a query tab keeps its SQL: reopen with `Cmd+Shift+T` or **File** > **Recently Closed**. See [Tabs](/features/tabs).

<Note>
  When a tab has both unsaved file changes and pending data grid edits, `Cmd+S` saves the grid changes first. Save the file after the grid save completes.
</Note>

### External Modifications

If a file changes on disk while open (a `git pull`, an edit in another editor), a yellow banner offers **Reload from Disk**, which replaces your tab edits with the new content. Saving over an externally changed file shows a side-by-side diff with three actions: **Keep My Changes**, **Reload from Disk**, or **Cancel**.

### Linked Folders

To watch a whole folder of `.sql` files, use [Linked SQL Folders](/features/favorites#linked-sql-folders) instead of opening files one by one.
