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

# SQL Playground

> Run SQL against your app's database.

The SQL Playground is a direct query interface to your project's database. It runs in the same connection your app uses, so what you can do depends on the role you are signed in as.

To access this tool, select **SQL Playground** from the left-hand sidebar menu.

<img src="https://mintcdn.com/archie-e998dbf6/GdaYz5W-YpQoJXsQ/features/backend/sql-playground/sql-playground-overview.png?fit=max&auto=format&n=GdaYz5W-YpQoJXsQ&q=85&s=59d990a4fd1d15e17f36016078688421" alt="SQL Playground overview" width="851" height="325" data-path="features/backend/sql-playground/sql-playground-overview.png" />

## Query Editor

The main "Playground" area allows you to write standard SQL statements. You can perform simple `SELECT` queries or complex `JOIN` operations (e.g., linking `students` with `cities`).

### Execution Methods

There are multiple ways to execute your code, giving you flexibility depending on your workflow:

1. **Line Execution:** Click the **Blue Play Triangle (▶)** located in the gutter next to a specific line number to run just that statement.
2. **Context Menu (Right-Click):** Right-click anywhere in the editor to open a menu where you can select **Execute SQL Query**.
3. **Keyboard Shortcut:** Press **`Ctrl + Enter`** to execute the query immediately without using the mouse.

<img src="https://mintcdn.com/archie-e998dbf6/GdaYz5W-YpQoJXsQ/features/backend/sql-playground/sql-playground-execution-methods.png?fit=max&auto=format&n=GdaYz5W-YpQoJXsQ&q=85&s=bede09947ad1c434824469ea1a79029c" alt="SQL Playground execution methods: line execution, right-click, Ctrl+Enter" width="851" height="325" data-path="features/backend/sql-playground/sql-playground-execution-methods.png" />

4. **Operations Panel:** Click the large **Run (▶)** button located in the top-right corner. A "Select Operation" panel lists suggested or context-aware queries (e.g., "Select All From students").

<img src="https://mintcdn.com/archie-e998dbf6/GdaYz5W-YpQoJXsQ/features/backend/sql-playground/sql-playground-operations-panel.png?fit=max&auto=format&n=GdaYz5W-YpQoJXsQ&q=85&s=a15b9c09350fc15cb37c2e3e6081b95c" alt="SQL Playground Operations panel with Run button and suggested queries" width="851" height="325" data-path="features/backend/sql-playground/sql-playground-operations-panel.png" />

### Toolbar Actions

The toolbar at the top of the editor provides tools to manage your code:

<img src="https://mintcdn.com/archie-e998dbf6/GdaYz5W-YpQoJXsQ/features/backend/sql-playground/sql-playground-toolbar-actions.png?fit=max&auto=format&n=GdaYz5W-YpQoJXsQ&q=85&s=92966794fd995092d4bd7e514055e3c3" alt="SQL Playground toolbar with Format, History, Download, Copy, Clear actions" width="851" height="51" data-path="features/backend/sql-playground/sql-playground-toolbar-actions.png" />

* **Format SQL:** Use the **Magic Wand** icon to automatically format and indent your SQL code for better readability.
* **Query History:** Access a log of your previously executed queries.
* **Download SQL:** Download the current queries to your local machine.
* **Copy SQL:** Use the **Copy** icon to copy the editor's content to your clipboard.
* **Clear Editor:** Use the **Broom** icon to clear or delete all queries from the editor.

## Results Panel

The **Results** section at the bottom displays the output of your execution.

* **Data Grid:** The data is presented in a table format with columns corresponding to your query (e.g., `first_name`, `age`, `name_city`).
* **Row Count:** The total number of records returned is displayed in the top right (e.g., "9 rows found").
* **Column Visibility:** You can customize the view by clicking the column selector icon in the results header to toggle specific columns on or off.

<img src="https://mintcdn.com/archie-e998dbf6/GdaYz5W-YpQoJXsQ/features/backend/sql-playground/sql-playground-results-panel.png?fit=max&auto=format&n=GdaYz5W-YpQoJXsQ&q=85&s=ddfd9aa6c2714a7ce68ed5fca9cddce7" alt="SQL Playground Results panel with data grid and column visibility" width="1865" height="852" data-path="features/backend/sql-playground/sql-playground-results-panel.png" />

## DDL statements

Schema changes (CREATE, ALTER, DROP) are not executed in the playground — they belong in the [data model](/features/backend/data-model/overview) workflow which produces tracked migrations. {/* VERIFY: confirm whether DDL is rejected or allowed with a warning */}

## Per-environment data

The playground binds to the active environment. Switch environments to query staging or production data. Production access is typically restricted to admin roles.

## Best practices

* **Use transactions** for multi-statement writes — `BEGIN; ...; COMMIT;`
* **Limit destructive queries** — always test with a `WHERE` clause first
* **Use `EXPLAIN`** to inspect query plans for slow reads
* **Avoid manually inserting** rows that should go through the application layer (validation, hooks)

## FAQ

<AccordionGroup>
  <Accordion title="Are my queries logged?">
    Yes. Audit logs include the actor, query, and runtime.
  </Accordion>

  <Accordion title="Can I run reports on a schedule?">
    Use a custom function with a scheduled trigger and put the SQL there.
  </Accordion>
</AccordionGroup>
