# SQL editor
Source: https://sovaria.in/docs/sql-editor

Write and run SQL in the console: run, explain and dry-run modes, confirmations for changes, row limits and timeouts, saved, shared and private queries, templates, formatting and permissions.

The SQL editor runs SQL on your project's database as your team member account — with full access, like the service role key. Use it for exploring data, one-off fixes and trying schema changes before making them migrations.

## Run, Explain and Dry run

| Mode | Does | Use it for |
|---|---|---|
| **Run** | Executes the SQL. Statements that change data or schema, or several statements at once, ask you to confirm first. | Real changes and queries. |
| **Explain** | Shows the query plan without running the query. | Seeing whether an index is used. |
| **Dry run** | Checks and runs read-only SQL; statements that would change data or schema are not executed. | Trying queries safely on production. |

Press **⌘/Ctrl + Enter** to run. To test a schema change without keeping it, use a migration **Dry run**, which runs it in a transaction that is rolled back ([Migrations](https://sovaria.in/docs/migrations)).

## Row limits and timeouts

- A `SELECT` without `LIMIT` is limited automatically (the default row limit is in the toolbar); the result says when it was cut.
- However large a result, the editor shows at most 5,000 rows of it; for a read statement (`SELECT`, `WITH`, `VALUES`, `TABLE`) your database sends at most one row more than that.
- Long statements are cancelled by the statement timeout ([Pooling and timeouts](https://sovaria.in/docs/settings-pooling-and-timeouts)).
- Large results are better exported than displayed — select and download.

## Saved queries

| Folder | Who sees it |
|---|---|
| **Private** | Only you. |
| **Shared with team** | Everyone in the organization who can open the project. |
| **Favorites** | Your starred queries, private or shared. |
| **Templates** | Ready-made queries (create a table, enable RLS, find slow queries…) to start from. |

**Format** (⇧⌥F) tidies the SQL.

## The database tree and autocomplete

- The **Database** tree beside the editor lists your schemas, their tables and columns. Click a name to insert it; the table menu writes **Select first 100 rows**, **Count rows** or an **Insert template** with every column for you, and **Open in Table Editor** jumps to the [Table editor](https://sovaria.in/docs/table-editor).
- Tables outside `public` are written with their schema (`billing.invoices`), and names that need it are quoted (`"Order"`, `"select"`), so the SQL it writes always runs.
- Autocomplete suggests the tables and columns of the schema you are in.
- The tree and autocomplete come from one schema snapshot that the console keeps in your browser and checks with the server on each visit, so they open at once and refresh as soon as the schema changes. It is cleared when you sign out.
- Schemas Sovaria manages (`sovaria_internal`, `sovaria_trash`) aren't listed ([Postgres in Sovaria](https://sovaria.in/docs/postgres-basics#platform-schemas)).

## Schema changes from the SQL editor

A `create`, `alter` or `drop` you run here is applied at once and recorded in the project's migration history with the SQL you ran, so your team can see who changed what. It has no down SQL, so it can't be undone with one click.

| To change the schema with | You get |
|---|---|
| The SQL editor | Fast; recorded, but no one-click undo. |
| **Schema** pages: [Functions](https://sovaria.in/docs/database-functions), [Triggers](https://sovaria.in/docs/database-triggers), [Indexes](https://sovaria.in/docs/database-indexes); the [Table editor](https://sovaria.in/docs/table-editor) | A form that shows the SQL and its undo; **Check** before applying; undo from [Version history: see, undo and restore changes](https://sovaria.in/docs/version-history). |
| A migration file ([Migrations](https://sovaria.in/docs/migrations)) | Reviewed in git, applied the same way to staging and production, with your own down SQL. |

**Open in SQL editor** on a function, trigger or index opens its SQL in a new tab so you can adapt it. Nothing runs until you press **Run**.

## Who can run what

Read-only statements need `database.read`; anything that writes, changes the schema or has several statements needs `database.write` ([Organizations, members and roles](https://sovaria.in/docs/organizations)). Viewers can run read-only queries only.

> **Warning:** The SQL editor bypasses your access policies and, where policies include the bypass check, row-level security. Test what app users can see with [Simulate access before you ship](https://sovaria.in/docs/simulation) or a test session, not in the SQL editor.
