Home » AI Databases » Natural Language SQL

How to Write SQL Queries Using Natural Language

The AI SQL assistant translates plain English into SQL queries that run against your MySQL or PostgreSQL database. You describe what data you want, the AI generates the correct SQL statement, executes it, and returns the results. This guide shows you how to phrase questions for the best results, from simple lookups to complex multi-table analysis.

Before You Start

Make sure your database is connected and the schema has been scanned. The AI needs your schema information to generate accurate queries. If you have not connected yet, start with the MySQL setup guide or PostgreSQL setup guide.

Step-by-Step: Your First Natural Language Query

Step 1: Open the query interface.
Navigate to the MySQL or PostgreSQL app in your admin panel and open the query section. This is where you type your questions and see results.
Step 2: Start with a simple question.
Type something straightforward like "show me the first 10 rows of the customers table" or "how many orders are in the database." The AI generates the SQL, runs it, and shows the results. You can also see the generated SQL to verify what ran.
Step 3: Add filters and conditions.
Narrow your results by adding conditions: "show me orders over $500 from this month" or "find customers in California who signed up last year." The AI adds WHERE clauses with the correct operators and date handling.
Step 4: Ask for summaries and aggregations.
Request totals, counts, and averages: "what is the total revenue by product category" or "how many new customers per month this year." The AI writes GROUP BY queries with the right aggregate functions.
Step 5: Combine tables for deeper insights.
Ask questions that span multiple tables: "show me customer names with their total spending" or "list products that have never been ordered." The AI uses foreign keys and relationships to build the correct JOIN statements automatically.

Example Questions and the SQL They Generate

Simple Lookup

Question: "Show me all active customers"
Generated: SELECT * FROM customers WHERE status = 'active'

Aggregation

Question: "What is the average order value by country"
Generated: SELECT country, AVG(total) as avg_order_value FROM orders GROUP BY country ORDER BY avg_order_value DESC

Date Filtering

Question: "How many orders were placed each day last week"
Generated: SELECT DATE(created_at) as order_date, COUNT(*) as order_count FROM orders WHERE created_at >= DATE_SUB(CURDATE(), INTERVAL 7 DAY) GROUP BY DATE(created_at) ORDER BY order_date

Multi-Table Join

Question: "Show me the top 5 customers by total spending with their email addresses"
Generated: SELECT c.name, c.email, SUM(o.total) as total_spent FROM customers c JOIN orders o ON c.id = o.customer_id GROUP BY c.id, c.name, c.email ORDER BY total_spent DESC LIMIT 5

Tips for Getting Better SQL Output

Read-only by default: Natural language queries default to SELECT statements. The AI will not generate INSERT, UPDATE, or DELETE statements unless you explicitly ask it to modify data, and only if your database user has write permissions.

Beyond Simple Queries

As you get comfortable, you can ask increasingly complex questions. The AI can generate subqueries, CTEs, window functions, and CASE statements. It handles questions like "rank customers by spending within each state" or "show me month-over-month growth percentages" by using the advanced SQL features of your database engine.

For finding trends and anomalies, see How to Use AI to Find Patterns in Your Database. For automating regular queries into reports, see How to Automate Database Reports With AI.

Write SQL queries without learning SQL. Connect your database and ask questions in plain English.

Get Started Free