
sidebar.wechat

sidebar.feishu
sidebar.chooseYourWayToJoin

sidebar.scanToAddConsultant
Configuration is just the first step. How to use it efficiently is the key. This article will dive deep into the usage tips of AskTable MCP Server to help you become a data querying expert.
AskTable MCP Server provides two core tools, each with its own characteristics and applicable scenarios.
| Feature | Query Data (query) | Generate SQL (gen_sql) |
|---|---|---|
| Input | Natural language question | Natural language question |
| Output | Query results (data) | SQL statement (text) |
| Execution | Automatically executes query | Does not execute query |
| Applicable Scenario | Need to get answers directly | Need to view or modify SQL |
| Response Speed | Slower (needs execution) | Faster (only generates SQL) |
Use "Query Data" when you:
Use "Generate SQL" when you:
Simple Query:
User: "Query how many students there are in total"
AI calls: query("Query how many students there are in total")
Returns: {"status": "success", "data": "There are 200 students in total"}
Aggregation Query:
User: "How many students are in each class?"
AI calls: query("How many students are in each class?")
Returns:
{
"status": "success",
"data": "Class 1: 50 students, Class 2: 48 students, Class 3: 52 students, Class 4: 50 students"
}
Multi-table Join:
Question: "Query the list of students enrolled in 'Database Principles' course"
AI will automatically:
1. Identify that students, courses, and enrollments three tables need to be joined
2. Generate the correct JOIN statement
3. Execute query and return results
Condition Filtering:
Question: "Query students who are older than 20 and have excellent grades"
AI will automatically:
1. Understand the conditions "age > 20" and "excellent grades"
2. Generate WHERE clause
3. Return list of students meeting the conditions
Time Range:
Question: "Query the total order amount in the last 7 days"
AI will automatically:
1. Calculate the time range (today - 7 days)
2. Generate date filtering conditions
3. Calculate and return the total amount
Be Clear About Query Goals:
Specify Time Range:
Be Clear About Aggregation:
Specify Sorting:
Q: What if there are too many query results?
A: Specify limiting conditions in the question:
"Query the top 10 students"
"Query orders with sales greater than 10000"
Q: How to query specific fields?
A: Clearly specify the needed fields:
"Query student names, ages, and classes"
Q: How to handle null values?
A: Explain how to handle in the question:
"Query students who have email"
"Query students with non-null email"
Simple Query:
User: "Generate SQL to query all students"
AI calls: gen_sql("Generate SQL to query all students")
Returns:
{
"status": "success",
"data": "SELECT * FROM students"
}
Complex Query:
User: "Write SQL to count average scores for each class"
AI calls: gen_sql("Write SQL to count average scores for each class")
Returns:
{
"status": "success",
"data": "SELECT class_name, AVG(score) as avg_score FROM students GROUP BY class_name"
}
Subquery:
Question: "Generate SQL to query students with scores higher than the average"
Generated SQL:
SELECT * FROM students
WHERE score > (SELECT AVG(score) FROM students)
Window Function:
Question: "Generate SQL to query top 3 students in each class by score"
Generated SQL:
SELECT * FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY class_id ORDER BY score DESC) as rank
FROM students
) t WHERE rank <= 3
CTE (Common Table Expression):
Question: "Generate SQL to query users who have orders for 3 consecutive days"
Generated SQL:
WITH daily_orders AS (
SELECT user_id, DATE(created_at) as order_date
FROM orders
GROUP BY user_id, DATE(created_at)
)
SELECT user_id FROM daily_orders
GROUP BY user_id
HAVING COUNT(DISTINCT order_date) >= 3
View Generated SQL:
User: "Generate SQL to query all orders"
AI: Return SQL
User: "Can this SQL be optimized?"
AI: Analyze and provide optimization suggestions
Add Index Suggestions:
User: "This query is slow, what indexes should be added?"
AI: Analyze SQL and suggest indexes
In enterprise environments, users of different roles should only access data within their permission scope:
Scenario: Restrict sales to only view data in their region
Step 1: Create Role in AskTable
Log in to AskTable and create role "East China Sales":
orders.region = 'East China'role_abc123Step 2: Specify role_id When Querying
User: "Query order total"
AI calls: query(
question="Query order total",
role_id="role_abc123"
)
Actually executed SQL:
SELECT SUM(amount) FROM orders WHERE region = 'East China'
Scenario: Restrict employees to only view their own data
Step 1: Create Role in AskTable
Create role "Employee":
orders.employee_id = {{employee_id}}role_def456Step 2: Pass Variables When Querying
User: "Query my orders"
AI calls: query(
question="Query my orders",
role_id="role_def456",
role_variables={"employee_id": 12345}
)
Actually executed SQL:
SELECT * FROM orders WHERE employee_id = 12345
Principle of Least Privilege:
Dynamic Variables:
Multi-layer Permissions:
Background: Sales manager needs to analyze this month's sales situation
Conversation Example:
User: "Query this month's total sales"
AI: This month's total sales is ¥1,234,567
User: "How much did it grow compared to last month?"
AI: Last month's total sales was ¥987,654, this month grew by 25%
User: "Which product sold the best?"
AI: The highest sales product is "Product A" with ¥345,678 in sales
User: "Generate SQL to query sales for each product"
AI: SELECT product_name, SUM(amount) as total_sales
FROM orders
WHERE MONTH(created_at) = MONTH(CURRENT_DATE)
GROUP BY product_name
ORDER BY total_sales DESC
Background: Product manager needs to analyze user activity
Conversation Example:
User: "Query daily active users in the last 7 days"
AI: Daily active users in the last 7 days:
3/1: 1,234 users
3/2: 1,456 users
...
User: "Which users are the most active?"
AI: The most active users are:
User A: 15 logins
User B: 12 logins
...
User: "Generate SQL to query user retention rate"
AI: WITH first_login AS (
SELECT user_id, MIN(DATE(login_time)) as first_date
FROM user_logs
GROUP BY user_id
),
retention AS (
SELECT f.first_date,
COUNT(DISTINCT f.user_id) as new_users,
COUNT(DISTINCT CASE WHEN DATE(l.login_time) = DATE_ADD(f.first_date, INTERVAL 1 DAY) THEN l.user_id END) as day1_retained
FROM first_login f
LEFT JOIN user_logs l ON f.user_id = l.user_id
GROUP BY f.first_date
)
SELECT first_date,
new_users,
day1_retained,
ROUND(day1_retained * 100.0 / new_users, 2) as retention_rate
FROM retention
Background: Finance personnel need to generate monthly reports
Conversation Example:
User: "Query this month's income and expenses"
AI: This month's income: ¥2,345,678
This month's expenses: ¥1,234,567
Net profit: ¥1,111,111
User: "Statistics expenses by department"
AI: R&D department: ¥567,890
Sales department: ¥345,678
Admin department: ¥123,456
...
User: "Generate SQL to query monthly profit trend"
AI: SELECT DATE_FORMAT(date, '%Y-%m') as month,
SUM(CASE WHEN type = 'income' THEN amount ELSE 0 END) as income,
SUM(CASE WHEN type = 'expense' THEN amount ELSE 0 END) as expense,
SUM(CASE WHEN type = 'income' THEN amount ELSE -amount END) as profit
FROM transactions
GROUP BY DATE_FORMAT(date, '%Y-%m')
ORDER BY month
Bad prompts:
Good prompts:
Decompose complex problems:
Question: "Analyze sales situation and give suggestions"
Decompose into:
1. "Query this month's total sales"
2. "Compare growth with last month"
3. "Query products with declining sales"
4. "Analyze reasons and give suggestions"
Terms AI can understand:
Example:
"Query this month's user retention rate"
"Compare sales with the same period last year"
"Query the channel with the highest conversion rate"
Specify output format:
"Display sales for each product in table format"
"Generate a bar chart showing sales trends"
"Export to CSV format"
Problem: Query keeps returning no result
Solution:
Problem: Query results don't match expectations
Solution:
Problem: Prompt that no permission to access some data
Solution:
Problem: AI can't understand your query
Solution:
Mastering AskTable MCP Server usage tips can greatly improve data query efficiency:
Core Points: ✅ Understand the differences between the two tools and applicable scenarios ✅ Use clear and specific prompts ✅ Use permission control to protect data security ✅ Use step-by-step queries for complex problems
Advanced Tips: ✅ Learn common SQL patterns ✅ Understand business terminology and metrics ✅ Optimize query performance
Next Steps:
Related Reading:
Technical Exchange:
sidebar.noProgrammingNeeded
sidebar.startFreeTrial