> ## 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.

# Create a Metric using Custom SQL

> This guide provides a step-by-step approach to utilizing custom SQL for creating metrics, which can enhance your data analysis capabilities.

<Steps>
  <Step title="Access Custom SQL">
    On the **Create Metric** page, select the **"Custom Query"** option.

    <Frame>
      <img src="https://mintcdn.com/databrainlabs-bef6850a/3OVuv1yincv0D6pa/images/guides/custom-query.gif?s=40ce195b976edeaeda09b245e9d9a260" alt="Selecting the Custom Query option from the metric creation screen" width="1629" height="920" data-path="images/guides/custom-query.gif" />
    </Frame>
  </Step>

  <Step title="Query Execution">
    In the **Query Editor**, enter your custom SQL query.

    Use the **"Format Query with AI"** option to align with Databrain's best practices.

    Click **"Run Query"** to execute.

    <Frame>
      <img src="https://mintcdn.com/databrainlabs-bef6850a/oJW5seGO2mMUK4Ch/images/guides/run-query.gif?s=5a8816cbe278ad040a4d888b581b53a9" alt="Executing a custom SQL query in the Databrain Query Editor" width="1632" height="918" data-path="images/guides/run-query.gif" />
    </Frame>
  </Step>

  <Step title="Preview the Results">
    Once the query runs, review the **Query Result** section to view your raw SQL output in a table.

    You can download the results as CSV if needed.

    <Frame>
      <img src="https://mintcdn.com/databrainlabs-bef6850a/MbLmlD85jcjEoVGY/images/guides/edit-custom-query.gif?s=d6621266fcf5aedd5f0a3260d04daf48" alt="Previewing and editing Custom SQL query results in Databrain" width="1631" height="919" data-path="images/guides/edit-custom-query.gif" />
    </Frame>
  </Step>

  <Step title="Save your Query Result">
    Click **Use Results >** to proceed. You can save it in the following ways:

    <br />

    **1. Table Chart**

    Click **"Use as Table Chart"**, configure additional settings, and save it to your dashboard.

    <Frame>
      <img src="https://mintcdn.com/databrainlabs-bef6850a/hRxI-z391a3Ay8M5/images/guides/table-chart.png?fit=max&auto=format&n=hRxI-z391a3Ay8M5&q=85&s=70c26567d500b10257793db817ae37a7" alt="Saving SQL query output as a table chart" width="2934" height="1650" data-path="images/guides/table-chart.png" />
    </Frame>

    <br />

    **2. Table Chart with Dynamic Table Property**

    This automatically adjusts to the latest structure of your SQL query result — ideal for evolving datasets using `SELECT *`.

    <br />

    **3. Publish as Custom Dataset**

    Provide a dataset name, choose a schema, and click **Save**.

    <Frame>
      <img src="https://mintcdn.com/databrainlabs-bef6850a/3OVuv1yincv0D6pa/images/guides/custom-dataset.png?fit=max&auto=format&n=3OVuv1yincv0D6pa&q=85&s=ed82ee8c7ac848b7f0185d307c131e70" alt="Publishing SQL query results as a custom dataset" width="2940" height="1656" data-path="images/guides/custom-dataset.png" />
    </Frame>

    <br />

    **4. New Chart**

    Select your preferred chart type, configure it, and save.

    <Frame>
      <img src="https://mintcdn.com/databrainlabs-bef6850a/oJW5seGO2mMUK4Ch/images/guides/new-chart.png?fit=max&auto=format&n=oJW5seGO2mMUK4Ch&q=85&s=0f7b2db11ee8b6d7f25c0df26a0deb57" alt="Creating a new chart from SQL query results" width="2940" height="1658" data-path="images/guides/new-chart.png" />
    </Frame>
  </Step>

  <Step title="Edit Custom Query">
    At any point, you can switch between the **Chart Builder** and the **Query Editor**.

    Make changes to your SQL and immediately view updated results in both areas.

    <Frame>
      <img src="https://mintcdn.com/databrainlabs-bef6850a/MbLmlD85jcjEoVGY/images/guides/edit-custom-query.gif?s=d6621266fcf5aedd5f0a3260d04daf48" alt="Editing custom SQL query and instantly updating metric visualization" width="1631" height="919" data-path="images/guides/edit-custom-query.gif" />
    </Frame>
  </Step>
</Steps>

<Check>
  You’ve successfully created a metric using Custom SQL in Databrain!\
  Your query-driven metric is now active, giving you full control over data transformation, advanced logic, and custom visualizations directly from your SQL output.
</Check>
