An AI-powered SQL learning tool built with Python, Streamlit, MySQL, and Google Gemini. It lets a user write a SQL query against a real database, runs it, and then uses Gemini AI to explain errors in plain English, suggest corrected SQL, review correct queries, and score query quality — like having a personal SQL tutor sitting next to you.
AI SQL Query Auditor was built to help beginners learn SQL the way a mentor would — not just by telling them what went wrong, but why, and how to fix it.
Instead of showing a raw, intimidating MySQL error message, the app automatically:
- Reads the live database schema
- Sends the schema, the user's business question, their SQL query, and any MySQL error to Gemini AI
- Returns a simple, beginner-friendly explanation, a corrected query, and a quality score
It started as a two-part hands-on project: first building the core query runner, then layering AI review on top.
- 🔌 Connects to a live MySQL database
- 📝 Lets users enter a business question + a SQL query through a web UI
▶️ Executes the query and displays results in a table⚠️ Catches and displays SQL errors without crashing- 🤖 Sends schema + question + query + error to Gemini AI for review
- 💡 AI explains errors in simple English
- 🛠️ AI provides the corrected SQL query with a line-by-line explanation
- ⭐ AI scores query quality out of 10 and flags beginner mistakes
- ✅ Reviews even correct queries and suggests simpler/better alternatives
| Layer | Technology |
|---|---|
| Frontend/UI | Streamlit |
| Backend Language | Python |
| Database | MySQL (via mysql-connector-python) |
| AI Model | Google Gemini (gemini-2.5-flash) via google-genai |
Student
│
▼
Streamlit Application
│
Writes SQL Query
│
Click "Run Query"
│
▼
Execute Query in MySQL
┌───────────┐
│ │
Success Error Found
│ │
└─────┬─────┘
▼
Read Database Schema
│
▼
Send to Gemini AI
• Database Schema
• Business Question
• Student SQL
• MySQL Error (if any)
│
▼
AI SQL Reviewer
• Explains the error
• Provides corrected SQL
• Explains corrections
• Gives a quality score
• Suggests improvements
AI_SQL_Auditor/
│
├── app.py # Streamlit UI + main application logic
├── config.py # MySQL & Gemini API configuration
├── requirements.txt # Python dependencies
│
├── utils/
│ ├── db.py # MySQL connection handler
│ ├── schema.py # Reads database schema automatically
│ └── gemini.py # Sends prompts to Gemini and returns response
│
└── prompts/
└── sql_prompt.txt # Prompt template used for AI SQL review
- Python 3.x (check Add Python to PATH during install)
- MySQL Community Server + MySQL Workbench
- A free Gemini API key
git clone https://github.com/Anuky-git/AI_SQL_Auditor.git
cd AI_SQL_Auditorpip install -r requirements.txtOpen MySQL Workbench and run:
CREATE DATABASE ai_sql_auditor;
USE ai_sql_auditor;Then create the Customers, Products, and Orders tables and insert sample data (see /database or setup script if included).
Update config.py with your MySQL credentials and Gemini API key:
MYSQL_HOST = "localhost"
MYSQL_USER = "root"
MYSQL_PASSWORD = "your_mysql_password"
MYSQL_DATABASE = "ai_sql_auditor"
GEMINI_API_KEY = "your_gemini_api_key"
⚠️ Never commit real API keys or passwords to GitHub. Use a.envfile orconfig.pyin.gitignorefor production.
streamlit run app.pyThe app will open automatically in your browser (usually at http://localhost:8501).
- Type a Business Question (e.g., "Show all customers")
- Write the corresponding SQL Query
- Click Run Query
- View the results table
- Scroll down to see the AI SQL Review — explanation, correction, and score
Business Question: Show all customers
SELECT * FROM Customers;✅ Query runs successfully → results table is shown → AI confirms the query is correct and gives a score.
Error Example:
SELECT * FROM Customer;❌ MySQL error (table doesn't exist) → AI explains the mistake in plain English, points out the correct table name is Customers, and shows the corrected query.
Add screenshots of the running app here so visitors can see it without running it themselves.



- Connecting Python to MySQL with
mysql-connector-python - Building an interactive UI with Streamlit
- Structuring a project into reusable modules (
db.py,schema.py,gemini.py) - Reading a database schema programmatically
- Prompt engineering for an AI code-review use case
- Integrating the Gemini API into a real application
- Handling and displaying errors gracefully in a user-friendly way
Contributions, issues, and feature requests are welcome. Feel free to fork this repo and submit a pull request.
This project is open source and available under the MIT License.