# 4. Writing SQL

*Tabellum for mobile, chapter 4. On the web: https://tabellum.dudko.dev/manual/mobile/writing-sql*

## 4.1 The query tab

Tap **+** above the tabs (or **New query tab** in the palette) for an editor on the connection chosen at its top. Type SQL and tap **Run**: Tabellum runs the selection if there is one, otherwise the statement under the cursor. The buttons next to **Run** format the SQL, save the tab as a file in `queries/`, and show the query plan.

While a query runs, **Run** becomes a red **Stop** button that counts the seconds.

## 4.2 Completion

The editor completes as you type, and it knows both the database’s dialect and your schema: tables, columns of the tables the statement names (with their aliases), functions with their arguments, and whole join conditions along foreign keys. After `ON`, the joins the foreign keys allow come first. Tap a suggestion to insert it.

## 4.3 Parameters

A query can have named parameters — `:since`, `$1` or `@name`, as the database writes them. Before it runs, Tabellum asks for their values and passes them to the database separately, so they are never pasted into the SQL.

![Values for a querys parameters](https://tabellum.dudko.dev/manual/mobile/img/params.jpg)

*Values for a query’s parameters*

## 4.4 The query plan

The plan button asks the database how it would run the statement and shows the answer as a tree. Each step has a bar for its share of the cost, the costliest steps are highlighted, and the database’s own output is a tap away. Tap it again to go back to the rows without running anything.

![A plan: each step with its rows and share of the time](https://tabellum.dudko.dev/manual/mobile/img/plan.jpg)

*A plan: each step with its rows and share of the time*

## 4.5 The command palette

The search button in the title bar opens the palette: type a few letters of a connection, table, saved query or command and tap the result.

![The command palette](https://tabellum.dudko.dev/manual/mobile/img/palette.jpg)

*The command palette*

## 4.6 The approval dialog

Tabellum reads every statement before it runs. Anything it cannot prove to be a read — `UPDATE`, `DELETE`, `INSERT`, `CREATE`, `ALTER`, `DROP` — stops at an approval dialog that shows the statements, their kind (1) and, where the database can tell, how many rows they would change.

![A write waits for your tap](https://tabellum.dudko.dev/manual/mobile/img/gate.jpg)

*A write waits for your tap*

Marked on the screenshot: (1) What kind of statement it is; (2) Run it, all or nothing.

**Run in transaction** (2) runs them all or none. Before a statement that changes the schema, the dialog offers to back up first. This dialog is the same whether you typed the SQL, edited cells, applied a design or the AI agent proposed it.
