# Querying Structured Data (SQL)

> Build an agentic RAG system that can query structured data (SQL).

<a target="_blank" href="https://colab.research.google.com/github/cohere-ai/cohere-developer-experience/blob/main/notebooks/guides/agentic-rag/agentic_rag_pt6_structured_data_SQL.ipynb">
  Open in Colab
</a>

In the previous tutorial, we explored how agentic RAG can handle complex queries on structured data in the form of tables using pandas. Now, we'll see how we can do the same for SQL databases.

Consider a scenario similar to the previous tutorial where we have evaluation results for an LLM application. However, instead of a CSV file, this data is now stored in a SQLite database. Users might still ask questions like "What's the average score for a specific use case?" or "Which configuration has the lowest latency?", but now we'll answer these using SQL queries instead of pandas operations.

In this tutorial, we'll cover:

* Setting up a SQLite database
* Creating a function to execute SQL queries
* Building an agent for querying SQL databases
* Running the agent with various types of queries

By implementing these techniques, we'll expand our agentic RAG system to handle structured data in SQL databases, complementing our previous work with tabular data in pandas.

Let's get started by setting up our environment and creating our SQLite database.

## Setup

To get started, first we need to install the `cohere` library and create a Cohere client.

```python PYTHON
! pip install cohere pandas -qq
```

```python PYTHON
import json
import os
import cohere
import sqlite3
import pandas as pd

co = cohere.ClientV2(
    "COHERE_API_KEY"
)  # Get your free API key: https://dashboard.cohere.com/api-keys
```

## Creating a SQLite database

Next, we'll create a SQLite database to store our evaluation results. SQLite is a lightweight, serverless database engine that's perfect for small to medium-sized applications. Here's what we're going to do:

1. Create a new SQLite database file named `evaluation_results.db`.
2. Create a table called `evaluation_results` with columns for `usecase`, `run`, `score`, `temperature`, `tokens`, and `latency`.
3. Insert sample data into the table to simulate our evaluation results.

<Note>
   Important: the data can be 

  [found here](https://github.com/cohere-ai/cohere-developer-experience/blob/main/notebooks/guides/agentic-rag/evaluation_results.db)

  . Make sure to have the file in the same directory as this notebook for the imports to work correctly. 
</Note>

```python PYTHON