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

# Custom SQL Console

### 1. **Access the Custom SQL**

On the **Create Metric** page, select the **"Custom Query"** option.

<Frame>
  <img src="https://mintcdn.com/databrainlabs-bef6850a/q5ifRIFfOuoabaky/CustomSQLConsole.png?fit=max&auto=format&n=q5ifRIFfOuoabaky&q=85&s=4a20deb4e4010fce4d34883ec8eb91c5" alt="Access the Custom SQL" width="2936" height="1660" data-path="CustomSQLConsole.png" />
</Frame>

### 2. **Writing SQL Scripts**

* Enter your SQL code and click **"Run Query"** to generate the chart.
* (Optional) Use the **"Format Query with AI"** option to ensure proper formatting of your query.
* **Query Result** displays the data returned for your SQL query. This data can be downloaded as a CSV file if required.

<Frame>
  <img src="https://mintcdn.com/databrainlabs-bef6850a/ZpDsEVsiugIJCc1l/WritingSQLScripts.png?fit=max&auto=format&n=ZpDsEVsiugIJCc1l&q=85&s=97a77c652f35d26b89d17d24d9323503" alt="Writing SQL scripts" width="2936" height="1652" data-path="WritingSQLScripts.png" />
</Frame>

### 3. **Usage Example**

Below is an example SQL code to visualize the value and quantity of all orders based on the products ordered.

```sql theme={"dark"}
SELECT
  AVG("main_procurement_mgmnt_data"."order_value") AS "Avg Order Value",
  AVG("main_procurement_mgmnt_data"."order_quantity") AS "Avg Order Quantity",
  "main_procurement_mgmnt_data"."product" AS "Product"
FROM
  "main"."procurement_mgmnt_data" AS "main_procurement_mgmnt_data"
GROUP BY
  "Product"
LIMIT
  100
```

### 4. **Output of the above SQL Code**

<Frame>
  <img src="https://mintcdn.com/databrainlabs-bef6850a/q5ifRIFfOuoabaky/OutputofSQLcode.png?fit=max&auto=format&n=q5ifRIFfOuoabaky&q=85&s=b5f6a6e653c0e06029d871923a597ffb" alt="Output of above SQL" width="1386" height="700" data-path="OutputofSQLcode.png" />
</Frame>
