Skip to content

An AI-powered analytics web application that allows users to query a Snowflake database using natural language. The system converts user questions into SQL queries using Gemini API, executes them in Snowflake, and displays results with visualization and CSV export.

License

Notifications You must be signed in to change notification settings

coding-for-it/Natural-Language-SQL-Query-Engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

27 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Project Overview

This project simplifies data analysis by removing the need to write SQL manually. Users can enter questions in plain English, and the application automatically:

  • Converts natural language to SQL using Gemini API
  • Executes queries on Snowflake database
  • Displays results in tabular and chart format
  • Allows users to download results as CSV

This makes data analysis accessible to non-technical users.


Technologies Used

  • Python
  • Streamlit (Web interface)
  • Snowflake (Cloud database)
  • Gemini API (AI SQL generation)
  • Plotly (Data visualization)
  • Pandas (Data handling)
  • Snowflake Python Connector

Features

  • Natural language to SQL conversion
  • Snowflake database integration
  • Query execution and result display
  • Automatic chart generation
  • CSV download option
  • Query history tracking

Project Structure

myproject/
β”‚
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ main.py                 # Streamlit web app
β”‚   β”œβ”€β”€ ai_sql_generator.py    # Converts question to SQL using Gemini
β”‚   β”œβ”€β”€ query_executor.py      # Executes SQL in Snowflake
β”‚   β”œβ”€β”€ snowflake_client.py    # Snowflake connection
β”‚   β”œβ”€β”€ visualization.py       # Creates charts
β”‚   β”œβ”€β”€ history_manager.py     # Saves query history
β”‚   β”œβ”€β”€ config.py              # Loads environment variables
β”‚
β”œβ”€β”€ data/
β”‚   └── sample_data.py
β”‚
β”œβ”€β”€ .env                       # API keys and credentials
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ run.py
└── README.md

System Workflow

User Input (Natural Language)
          β”‚
          β–Ό
Gemini API generates SQL
          β”‚
          β–Ό
Snowflake executes SQL
          β”‚
          β–Ό
Results returned to app
          β”‚
          β–Ό
Display table + visualization + CSV download

Database Schema Example

Table: SALES

Columns:

  • order_id
  • customer_name
  • product
  • category
  • region
  • quantity
  • price
  • order_date

Installation and Setup

1. Clone the repository

git clone <your-repo-url>
cd myproject

2. Create virtual environment

python -m venv venv
venv\Scripts\activate

3. Install dependencies

pip install -r requirements.txt

4. Configure environment variables

Create .env file:

GEMINI_API_KEY=your_api_key

SNOWFLAKE_USER=your_user
SNOWFLAKE_PASSWORD=your_password
SNOWFLAKE_ACCOUNT=your_account
SNOWFLAKE_WAREHOUSE=your_warehouse
SNOWFLAKE_ROLE=your_role

5. Run the application

streamlit run app/main.py

Example Usage

User Input:

Show total sales by region

Generated SQL:

SELECT region, SUM(price * quantity) AS total_sales
FROM sales
GROUP BY region;

Key Components Explanation

ai_sql_generator.py

Uses Gemini API to convert natural language to SQL.

query_executor.py

Executes SQL query on Snowflake and returns results.

snowflake_client.py

Creates connection to Snowflake database.

visualization.py

Generates charts using Plotly.

main.py

Handles user interface using Streamlit.


Problems Solved

Problem: Non-technical users cannot write SQL queries.

Solution: AI automatically generates SQL from natural language.

Problem: Manual data analysis is slow.

Solution: Automated query execution and visualization.


Future Improvements

  • Support multiple databases
  • Add authentication system
  • Improve SQL accuracy with schema awareness
  • Add dashboard saving feature

About

An AI-powered analytics web application that allows users to query a Snowflake database using natural language. The system converts user questions into SQL queries using Gemini API, executes them in Snowflake, and displays results with visualization and CSV export.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages