How to Write SQL Queries Using Natural Language
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
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.
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.
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.
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.
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
- Name your tables and columns when you know them. "Query the orders table" is more precise than "show me purchases."
- Specify what columns you want. "Show me name, email, and total" gives cleaner output than "show me everything."
- Use follow-up questions. After a query, say "now filter that to only California" or "add the customer email to those results." The AI keeps conversation context.
- Ask the AI to show the SQL. If you are learning SQL, ask "show me the SQL for that" and the AI will display and explain the generated query.
- Correct mistakes verbally. If the AI queries the wrong table, say "I meant the order_history table, not orders" and it will adjust.
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