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

# Queries

> How the ontology is used to make AI-generated queries

TextQL uses an **Ontology** to generate AI-assisted database queries.
This page explores the two main types of queries—**Object Queries** and **Metric Queries**.

<Frame caption="Sample Ontology: Objects represent key concepts within a business' datawarehouse. Queries are used to extract data from the ontology.">
  <img className="block dark:hidden" src="https://mintcdn.com/textql/OiZBAuSgAbeJrWSJ/images/how-it-works/ontology/queries/ontology_example.svg?fit=max&auto=format&n=OiZBAuSgAbeJrWSJ&q=85&s=f3c80e4fdfcc92f0b431857b609ebd8e" alt="Example Ontology" style={{ height: 'auto', maxHeight: '600px' }} width="1731" height="2537" data-path="images/how-it-works/ontology/queries/ontology_example.svg" />

  <img className="hidden dark:block" src="https://mintcdn.com/textql/OiZBAuSgAbeJrWSJ/images/how-it-works/ontology/queries/ontology_example_dark.svg?fit=max&auto=format&n=OiZBAuSgAbeJrWSJ&q=85&s=98854b836fcb529dc1f268f8cbf68af4" alt="Example Ontology" style={{ height: 'auto', maxHeight: '600px' }} width="1731" height="2537" data-path="images/how-it-works/ontology/queries/ontology_example_dark.svg" />
</Frame>

## Object vs. Metric Queries

TextQL supports two main categories of queries:

1. [Object Queries](/core/how-it-works/ontology/queries#object-queries)
2. [Metric Queries](/core/how-it-works/ontology/queries#metric-queries)

When you begin a chat or analysis, you must start with a query (even if “Ana” creates it automatically).
It’s crucial to recognize whether you need an **Object Query** or a **Metric Query** for your question.

### Object Queries

Object queries are designed to surface **all information about a specific object** in your business.
Every row of the resulting table will correspond to a unique instance of that object.

Examples of questions that fit Object Queries:

* **Which store has the most revenue?**\
  (You want a table where each row represents a unique store, possibly including a revenue metric.)

* **How many orders were shipped to Canada?**\
  (You want a table where each row represents a unique order. If you have location data, you might also see attributes like “country” attached to each row.)

**Key Characteristics:**

* The query returns rows corresponding to a **single object**—for example, “Store” or “Order.”
* Attributes from **related objects** can be joined in automatically.
  For example, if every store has a regional manager, then the name of the regional manager can be included when the store object is queried.
* Aggregated metrics from “downstream” objects can be included. (If each store has multiple orders, total profit from the “Order” object can be aggregated by store.)

Because object queries focus on one entity, they do **not** automatically break down metrics by every possible dimension. If you need a fully broken-down metric (e.g., total profit by quarter, region, etc.), you will want to use a **Metric Query**.

<Frame caption="Example: Querying the Location object returns a table where each row corresponds to a location, and columns refer to the attributes of that location.">
  <img src="https://mintcdn.com/textql/pa7tJ1trBrfXrbqq/images/how-it-works/ontology/queries/object.png?fit=max&auto=format&n=pa7tJ1trBrfXrbqq&q=85&s=b0bd1c7503d7b68bde96e962ef704233" alt="Example Object Query." width="913" height="540" data-path="images/how-it-works/ontology/queries/object.png" />
</Frame>

### Metric Queries

Metric queries are designed to surface **quantitative measures (metrics) broken down by dimensions**. The result table has each row corresponding to a specific slice of a metric along a dimension or category.

<details>
  <summary>Examples of questions that fit Metric Queries</summary>

  * **How much profit was made each quarter?**\
    (You want a table where each row is a quarter, and one column shows the total profit for that quarter.)

  * **What are the total sales by regional manager?**\
    (You want rows for each manager with a “total sales” metric. If managers belong to a different object than sales, the ontology will still handle the join behind the scenes.)
</details>

**Key Characteristics:**

* Direct focus on **numerical measurements**, such as revenue, profit, or quantity.
* Rows represent **slices** or **groups** of these metrics across defined dimensions (e.g., time, geography, product category).
* Ideal for **pivot-table-style** breakdowns (e.g., total sales per city per month).

<Frame caption="Example: Querying the Total Profit metric returns a table where each row corresponds to a particular aggregation of profit, and where columns describe the dimensions that the metric can be further aggregated on.">
  <img className="block dark:hidden" src="https://mintcdn.com/textql/OiZBAuSgAbeJrWSJ/images/how-it-works/ontology/queries/metric_query.svg?fit=max&auto=format&n=OiZBAuSgAbeJrWSJ&q=85&s=9e9018e3f4bc415ac11e18d645503db9" alt="Metric Query" style={{ height: 'auto', maxHeight: '500px' }} width="1676" height="1517" data-path="images/how-it-works/ontology/queries/metric_query.svg" />

  <img className="hidden dark:block" src="https://mintcdn.com/textql/OiZBAuSgAbeJrWSJ/images/how-it-works/ontology/queries/metric_query_dark.svg?fit=max&auto=format&n=OiZBAuSgAbeJrWSJ&q=85&s=c3253dc989fb94f3dc6d6b064c142523" alt="Metric Query" style={{ height: 'auto', maxHeight: '500px' }} width="1676" height="1517" data-path="images/how-it-works/ontology/queries/metric_query_dark.svg" />
</Frame>

## Technical Deep Dive

<Warning>
  The below section is rather intricate and technical, and deals mainly with how TextQL generates queries and not how to work with them. Feel free to skip it.
</Warning>

### Structure of an Ontology Query

In TextQL, each ontology query is composed of up to five major parts. Regardless of whether it’s an **Object Query** or a **Metric Query**, these elements guide how data is retrieved and displayed:

1. **Core Fact Object**
   * **Definition**: The primary object around which the query is centered.
   * **Purpose**: Determines the main data source and how other entities join.
   * **Example**: "Sale" as the core fact object in a sales analysis.

2. **Metrics**
   * **Definition**: The quantifiable measurements (e.g., sum, count, average) you want to analyze.
   * **Purpose**: Provide numerical insights, often aggregated.
   * **Example**: “Total Sales” or “Average Order Value.”

3. **Dimensions**
   * **Definition**: Attributes or fields that categorize your metrics.
   * **Purpose**: Break down metrics into slices (e.g., time, region, product category).
   * **Example**: “Store Location,” “Order Date,” or “Product Type.”

4. **Filters**
   * **Definition**: Criteria that include or exclude data from the query.
   * **Purpose**: Narrow down your dataset to the most relevant subset.
   * **Example**: Sales in the last quarter or orders above a certain dollar amount.

5. **Orders**
   * **Definition**: The sorting or ordering applied to the results.
   * **Purpose**: Make it easier to see trends, top performers, or chronological sequences.
   * **Example**: Sorting by descending “Total Sales” to identify best-selling products first.

### Object Queries

Object queries are fully implemented in TextQL through the **Object Explorer** interface. The Object Explorer allows you to:

* Select a core fact object (e.g., "Orders", "Customers")
* Join relevant attributes from related objects
* Include aggregated metrics from downstream objects
* Apply filters and sorting to your results
* View results in an interactive table

The Object Explorer intelligently identifies join paths and optimizes query performance across large datasets.

### Metric Queries

Metric queries are fully implemented in TextQL through the **Metric Explorer** interface. The Metric Explorer allows you to:

* Select one or more metrics from your ontology
* Choose dimensions to slice and group your data
* Apply filters to narrow down results
* View aggregated metrics broken down by dimensions
* Perform pivot-table-style analysis

The Metric Explorer handles complex aggregations and supports intelligent grouping across multiple objects.

***

## Next Steps

You should now have a basic understanding of how **object queries** differ from **metric queries** and the fundamental structure of any ontology query. In subsequent sections, we will dive deeper into the **technical details** and **best practices** for setting up your own business ontology in TextQL.
