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

# Run SQL queries in an Athena notebook

> Create and save SQL queries in Olympus, retrieve asset IDs, and run query snippets from an Athena notebook to analyze the results.

export const AskAthena = ({prompt, send = false}) => {
  const href = `https://app.athenaintel.com/dashboard/spaces?copilotInput=${encodeURIComponent(prompt)}${send ? '&autoSubmit=true' : ''}`;
  return <a href={href} target="_blank" rel="noopener noreferrer" className="not-prose mb-4 inline-flex items-center gap-1.5 rounded-lg border border-zinc-950/10 dark:border-white/10 px-3 py-1.5 text-sm font-medium text-zinc-900 dark:text-zinc-100 hover:border-zinc-950/20 dark:hover:border-white/20 no-underline">
      Ask in Athena
      <span aria-hidden="true">→</span>
    </a>;
};

<Tab title="Query in Notebooks">
  Run saved SQL queries from a notebook to use their results in your analysis.

  ## Step-by-step Instructions

  <Steps>
    <Step title="Create a query snippet">
      Navigate to ***[Spaces](https://app.athenaintel.com/dashboard/spaces/)***. Drag and drop the database you wish to query onto your Space. In the **Chat** sidebar, enter the following prompt and press **Enter**.

      <CodeBlock filename="Prompt">
        Use query to answer: Which drivers have scored the most points so far in the 2024 season?
      </CodeBlock>

      <AskAthena prompt="Use query to answer: Which drivers have scored the most points so far in the 2024 season?" />

      <Frame>
        <img alt="Database table in Spaces beside a SQL query request" src="https://mintcdn.com/athenaintelligence-e46bc9d3/XOzR0DdNhB1vkjEH/images/query-step1.png?fit=max&auto=format&n=XOzR0DdNhB1vkjEH&q=85&s=6f948171344448416d43c39ab63aca68" width="3418" height="1958" data-path="images/query-step1.png" />
      </Frame>
    </Step>

    <Step title="Save the query">
      Save your query snippet by clicking on the save icon present on top-right of the query snippet.

      <Frame>
        <img alt="SQL snippet editor with the save control highlighted" src="https://mintcdn.com/athenaintelligence-e46bc9d3/XOzR0DdNhB1vkjEH/images/query-step2.png?fit=max&auto=format&n=XOzR0DdNhB1vkjEH&q=85&s=0c6aa98b632d7bc2cbc6c63ee8aa8571" width="3420" height="1960" data-path="images/query-step2.png" />
      </Frame>
    </Step>

    <Step title="Get IDs">
      In the right **Chat** sidebar, enter the following prompt and press **Enter**.

      <CodeBlock filename="Prompt">
        Retrieve the asset\_id for *@your-query-name* and *@your-database-name*.
      </CodeBlock>

      <AskAthena prompt="Retrieve the asset_id for @your-query-name and @your-database-name." />

      <Info>Replace `your-query-name` and  `your-database-name` with the names of your query and database on which the query is executed.</Info>

      <Frame>
        <img alt="Chat requesting the query and database asset IDs" src="https://mintcdn.com/athenaintelligence-e46bc9d3/XOzR0DdNhB1vkjEH/images/query-step3.png?fit=max&auto=format&n=XOzR0DdNhB1vkjEH&q=85&s=7f7bb8e8308aaa21adfb2a7d5eec9019" width="3420" height="1958" data-path="images/query-step3.png" />
      </Frame>
    </Step>
  </Steps>

  Queries in notebooks can be executed either by referencing a saved SQL snippet or by directly running SQL statements against your connected databases.

  <Tabs>
    <Tab title="Query a saved SQL snippet">
      <Step title="Create a Notebook">
        In the right **Chat** sidebar, enter the following prompt and press **Enter**.

        <CodeBlock filename="Prompt">
          Create a new Notebook.
        </CodeBlock>

        <AskAthena prompt="Create a new Notebook." send />

        <Frame>
          <img alt="New notebook open beside the saved SQL query" src="https://mintcdn.com/athenaintelligence-e46bc9d3/XOzR0DdNhB1vkjEH/images/query-step4.png?fit=max&auto=format&n=XOzR0DdNhB1vkjEH&q=85&s=63bdd54a818f93166d0789df26e66863" width="3420" height="1956" data-path="images/query-step4.png" />
        </Frame>
      </Step>

      <Step title="Execute your query from the saved query snippet">
        Copy and paste the following python snippet in a new cell of the Notebook that just got created. Run the cell.

        ```
        from athena.client import Athena
        # Setup the client
        client = Athena ()
        # Execute SQL snippet and load results into a DataFrame
        df = client.query.execute_snippet(snippet_asset_id="query-asset-id")
        #print the df
        print(df)
        ```

        <Info>Replace `"query-asset-id"` with the saved query's asset ID.</Info>

        <Frame>
          <img alt="Notebook cell executing a saved query snippet and displaying results" src="https://mintcdn.com/athenaintelligence-e46bc9d3/XOzR0DdNhB1vkjEH/images/query-step5.png?fit=max&auto=format&n=XOzR0DdNhB1vkjEH&q=85&s=029d0de8144cb141a1a26da6a650fa3f" width="3420" height="1960" data-path="images/query-step5.png" />
        </Frame>
      </Step>
    </Tab>

    <Tab title="Execute SQL against your DBs">
      <Steps>
        <Step title="Create a Notebook">
          In the right **Chat** sidebar, enter the following prompt and press **Enter**.

          <CodeBlock filename="Prompt">
            Create a new Notebook.
          </CodeBlock>

          <AskAthena prompt="Create a new Notebook." send />

          <Frame>
            <img alt="New notebook open beside the saved SQL query" src="https://mintcdn.com/athenaintelligence-e46bc9d3/XOzR0DdNhB1vkjEH/images/query-step4.png?fit=max&auto=format&n=XOzR0DdNhB1vkjEH&q=85&s=63bdd54a818f93166d0789df26e66863" width="3420" height="1956" data-path="images/query-step4.png" />
          </Frame>
        </Step>

        <Step title="Format and execute your query">
          Use the database SQL execution endpoint to run SQL directly against your connected database. Refer to the [Database SQL API documentation](https://docs.athenaintel.com/api-reference/databases/execute-sql) for details.
        </Step>
      </Steps>
    </Tab>
  </Tabs>
</Tab>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.