---
title: "Natural Language Queries - Documentation"
description: "Deep dive into how AI for Database converts plain English into SQL, including tips, examples, and advanced techniques."
canonical: "https://www.aifordatabase.com/docs/natural-language-queries/"
---

# Natural Language Queries

beginner 8 min read

## Natural Language Queries

The core capability of AI for Database is translating plain-English questions into database queries. This guide covers how it works, best practices, and dozens of real examples.

### How Translation Works

AI for Database reads your database schema (table names, column names, data types, foreign keys, and indexes) and sends this context along with your question to a large language model. The model generates a SQL query (or MongoDB aggregation pipeline) that answers your question.

The schema context is refreshed automatically when tables change. You can also trigger a manual refresh from **Settings > Connections > Refresh Schema**.

### Example Questions by Category

**Counting and Aggregation**

*   "How many orders were placed today?"
*   "What is the total revenue for Q1 2024?"
*   "What is the average response time for support tickets this week?"

**Filtering and Searching**

*   "Show me all customers in California with more than 5 orders"
*   "Find invoices that are overdue by more than 30 days"
*   "Which products have less than 10 units in stock?"

**Grouping and Ranking**

*   "Top 10 customers by lifetime value"
*   "Revenue by product category for each month in 2024"
*   "Number of new signups by country, ordered from highest to lowest"

**Comparisons and Trends**

*   "Compare this month's sales to last month's"
*   "Show the trend of daily active users over the past 90 days"
*   "Which region grew the fastest in revenue this quarter?"

**Joins and Relationships**

*   "List each customer's name and their most recent order date"
*   "Show me the product name and category for all items with low stock"
*   "How many tickets has each support agent closed this month?"

### Handling Ambiguity

When AI for Database encounters ambiguity, it makes a best guess and shows its reasoning. For example, if you ask "How many users?" and your database has both a `users` table and a `customers` table, the AI will pick the most likely one and explain its choice.

You can resolve ambiguity by being explicit: "How many rows in the customers table?" or by setting **table aliases** in Settings to map business terms to specific tables.

### Follow-Up Questions

After getting an initial result, you can refine with follow-up questions in the same conversation:

1.  1"Show me total sales by region" (initial query)
2.  2"Now filter to only regions with sales above $100K" (follow-up)
3.  3"Sort by sales descending" (another follow-up)

Each follow-up builds on the previous context, making it easy to explore your data iteratively.

### Viewing and Editing SQL

Click **View SQL** on any result to see the generated query. You can edit the SQL directly and re-run it. This is useful for fine-tuning results or learning SQL by example.

Edited queries can be saved and will preserve your modifications on future runs.

Source: https://www.aifordatabase.com/docs/natural-language-queries/
