<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: VINCENZO RAFAEL LLANOS NIÑO</title>
    <description>The latest articles on DEV Community by VINCENZO RAFAEL LLANOS NIÑO (@vincenzo_rafaelllanosni).</description>
    <link>https://dev.to/vincenzo_rafaelllanosni</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4024429%2F767cffec-bbf9-4409-8cc4-9d901350d590.jpg</url>
      <title>DEV Community: VINCENZO RAFAEL LLANOS NIÑO</title>
      <link>https://dev.to/vincenzo_rafaelllanosni</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vincenzo_rafaelllanosni"/>
    <language>en</language>
    <item>
      <title>From Natural Language to SQL with AI: Building an Intelligent SQL Query Generator Using Hugging Face and Streamlit</title>
      <dc:creator>VINCENZO RAFAEL LLANOS NIÑO</dc:creator>
      <pubDate>Fri, 10 Jul 2026 18:50:12 +0000</pubDate>
      <link>https://dev.to/vincenzo_rafaelllanosni/from-natural-language-to-sql-with-ai-building-an-intelligent-sql-query-generator-using-hugging-3cgj</link>
      <guid>https://dev.to/vincenzo_rafaelllanosni/from-natural-language-to-sql-with-ai-building-an-intelligent-sql-query-generator-using-hugging-3cgj</guid>
      <description>&lt;p&gt;Introduction&lt;/p&gt;

&lt;p&gt;Writing SQL queries is a fundamental skill for developers, data analysts, and database administrators. However, not everyone knows SQL syntax, and even experienced developers spend time writing repetitive queries.&lt;/p&gt;

&lt;p&gt;Recent advances in Generative AI and Large Language Models (LLMs) make it possible to convert plain English into SQL automatically. Instead of writing:&lt;/p&gt;

&lt;p&gt;SELECT name, salary&lt;br&gt;
FROM employees&lt;br&gt;
WHERE department = 'IT'&lt;br&gt;
ORDER BY salary DESC;&lt;/p&gt;

&lt;p&gt;A user can simply ask:&lt;/p&gt;

&lt;p&gt;"Show me all IT employees ordered by salary from highest to lowest."&lt;/p&gt;

&lt;p&gt;The AI translates the request into SQL.&lt;/p&gt;

&lt;p&gt;In this article, we'll build a Text-to-SQL application using:&lt;/p&gt;

&lt;p&gt;Python&lt;br&gt;
Streamlit&lt;br&gt;
Hugging Face Transformers&lt;br&gt;
SQLite&lt;br&gt;
SQLAlchemy&lt;/p&gt;

&lt;p&gt;We'll also discuss real-world applications, limitations, and best practices.&lt;/p&gt;

&lt;p&gt;Why Text-to-SQL Matters&lt;/p&gt;

&lt;p&gt;Organizations generate massive amounts of structured data stored in relational databases.&lt;/p&gt;

&lt;p&gt;Business users often need answers without knowing SQL.&lt;/p&gt;

&lt;p&gt;Examples include:&lt;/p&gt;

&lt;p&gt;Sales managers checking monthly revenue&lt;br&gt;
HR departments analyzing employee records&lt;br&gt;
Finance teams generating reports&lt;br&gt;
Customer support searching order history&lt;/p&gt;

&lt;p&gt;AI enables these users to retrieve information using natural language.&lt;/p&gt;

&lt;p&gt;Project Architecture&lt;br&gt;
User Question&lt;br&gt;
       │&lt;br&gt;
       ▼&lt;br&gt;
 Hugging Face Model&lt;br&gt;
       │&lt;br&gt;
       ▼&lt;br&gt;
Generated SQL Query&lt;br&gt;
       │&lt;br&gt;
       ▼&lt;br&gt;
SQLite Database&lt;br&gt;
       │&lt;br&gt;
       ▼&lt;br&gt;
Results&lt;br&gt;
       │&lt;br&gt;
       ▼&lt;br&gt;
Streamlit Dashboard&lt;/p&gt;

&lt;p&gt;The workflow is simple:&lt;/p&gt;

&lt;p&gt;User enters a question.&lt;br&gt;
AI generates SQL.&lt;br&gt;
SQL executes against SQLite.&lt;br&gt;
Results appear instantly.&lt;br&gt;
Technologies Used&lt;br&gt;
Technology  Purpose&lt;br&gt;
Python  Backend&lt;br&gt;
Streamlit   Web Interface&lt;br&gt;
Hugging Face    LLM for Text-to-SQL&lt;br&gt;
SQLAlchemy  Database connection&lt;br&gt;
SQLite  Sample database&lt;br&gt;
Installing Dependencies&lt;br&gt;
pip install streamlit&lt;br&gt;
pip install transformers&lt;br&gt;
pip install torch&lt;br&gt;
pip install sqlalchemy&lt;br&gt;
pip install pandas&lt;br&gt;
Creating a Sample Database&lt;br&gt;
from sqlalchemy import create_engine&lt;/p&gt;

&lt;p&gt;engine = create_engine("sqlite:///company.db")&lt;/p&gt;

&lt;p&gt;engine.execute("""&lt;br&gt;
CREATE TABLE employees(&lt;br&gt;
    id INTEGER PRIMARY KEY,&lt;br&gt;
    name TEXT,&lt;br&gt;
    department TEXT,&lt;br&gt;
    salary INTEGER&lt;br&gt;
)&lt;br&gt;
""")&lt;/p&gt;

&lt;p&gt;Insert sample data:&lt;/p&gt;

&lt;p&gt;engine.execute("""&lt;br&gt;
INSERT INTO employees(name, department, salary)&lt;br&gt;
VALUES&lt;br&gt;
('Alice','IT',7500),&lt;br&gt;
('Bob','Sales',5200),&lt;br&gt;
('Carol','IT',8900)&lt;br&gt;
""")&lt;br&gt;
Loading a Hugging Face Model&lt;/p&gt;

&lt;p&gt;One popular Text-to-SQL model is based on T5.&lt;/p&gt;

&lt;p&gt;from transformers import AutoTokenizer, AutoModelForSeq2SeqLM&lt;/p&gt;

&lt;p&gt;model_name = "tscholak/1wnr382e"&lt;/p&gt;

&lt;p&gt;tokenizer = AutoTokenizer.from_pretrained(model_name)&lt;br&gt;
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)&lt;br&gt;
Converting Natural Language into SQL&lt;br&gt;
question = "Show employees working in IT"&lt;/p&gt;

&lt;p&gt;inputs = tokenizer(question, return_tensors="pt")&lt;/p&gt;

&lt;p&gt;outputs = model.generate(**inputs)&lt;/p&gt;

&lt;p&gt;sql = tokenizer.decode(outputs[0], skip_special_tokens=True)&lt;/p&gt;

&lt;p&gt;print(sql)&lt;/p&gt;

&lt;p&gt;Possible output:&lt;/p&gt;

&lt;p&gt;SELECT *&lt;br&gt;
FROM employees&lt;br&gt;
WHERE department='IT';&lt;br&gt;
Executing the SQL&lt;br&gt;
import pandas as pd&lt;/p&gt;

&lt;p&gt;result = pd.read_sql(sql, engine)&lt;/p&gt;

&lt;p&gt;print(result)&lt;/p&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;id  name    department  salary&lt;br&gt;
1   Alice   IT  7500&lt;br&gt;
3   Carol   IT  8900&lt;br&gt;
Building the Streamlit Interface&lt;br&gt;
import streamlit as st&lt;/p&gt;

&lt;p&gt;question = st.text_input("Ask your database")&lt;/p&gt;

&lt;p&gt;if st.button("Generate SQL"):&lt;br&gt;
    sql = generate_sql(question)&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;st.code(sql, language="sql")

result = pd.read_sql(sql, engine)

st.dataframe(result)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now users only need to type questions such as:&lt;/p&gt;

&lt;p&gt;Show all employees&lt;br&gt;
List employees in Sales&lt;br&gt;
Average salary by department&lt;br&gt;
Highest paid employee&lt;br&gt;
Real-World Applications&lt;br&gt;
Business Intelligence&lt;/p&gt;

&lt;p&gt;Employees can generate reports without learning SQL.&lt;/p&gt;

&lt;p&gt;Healthcare&lt;/p&gt;

&lt;p&gt;Doctors can retrieve patient records using natural language.&lt;/p&gt;

&lt;p&gt;Banking&lt;/p&gt;

&lt;p&gt;Analysts can summarize transactions through conversational queries.&lt;/p&gt;

&lt;p&gt;E-commerce&lt;/p&gt;

&lt;p&gt;Managers can ask:&lt;/p&gt;

&lt;p&gt;"Which products sold the most last month?"&lt;/p&gt;

&lt;p&gt;instead of writing complex SQL.&lt;/p&gt;

&lt;p&gt;Challenges&lt;/p&gt;

&lt;p&gt;Although Text-to-SQL is impressive, it has limitations.&lt;/p&gt;

&lt;p&gt;Database Schema Understanding&lt;/p&gt;

&lt;p&gt;The AI performs much better when it understands the database schema.&lt;/p&gt;

&lt;p&gt;SQL Validation&lt;/p&gt;

&lt;p&gt;Generated SQL should always be validated before execution.&lt;/p&gt;

&lt;p&gt;Never execute AI-generated SQL directly in production.&lt;/p&gt;

&lt;p&gt;Security&lt;/p&gt;

&lt;p&gt;Restrict permissions to read-only whenever possible.&lt;/p&gt;

&lt;p&gt;Avoid allowing AI to execute:&lt;/p&gt;

&lt;p&gt;DELETE&lt;br&gt;
UPDATE&lt;br&gt;
DROP&lt;br&gt;
ALTER&lt;/p&gt;

&lt;p&gt;without human approval.&lt;/p&gt;

&lt;p&gt;Best Practices&lt;/p&gt;

&lt;p&gt;✅ Provide the database schema as context.&lt;/p&gt;

&lt;p&gt;✅ Validate SQL syntax.&lt;/p&gt;

&lt;p&gt;✅ Limit user permissions.&lt;/p&gt;

&lt;p&gt;✅ Log generated queries.&lt;/p&gt;

&lt;p&gt;✅ Review queries before execution.&lt;/p&gt;

&lt;p&gt;Public GitHub Example&lt;/p&gt;

&lt;p&gt;A complete open-source implementation can be found in projects like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/vanna-ai/vanna" rel="noopener noreferrer"&gt;https://github.com/vanna-ai/vanna&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/defog-ai/sqlcoder" rel="noopener noreferrer"&gt;https://github.com/defog-ai/sqlcoder&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/langchain-ai/langchain" rel="noopener noreferrer"&gt;https://github.com/langchain-ai/langchain&lt;/a&gt; (SQL agents)&lt;/p&gt;

&lt;p&gt;These repositories demonstrate production-ready approaches for natural language querying over SQL databases.&lt;/p&gt;

&lt;p&gt;Future Improvements&lt;/p&gt;

&lt;p&gt;Some ideas to extend this project include:&lt;/p&gt;

&lt;p&gt;PostgreSQL support&lt;br&gt;
MySQL support&lt;br&gt;
SQL Server support&lt;br&gt;
Query explanation&lt;br&gt;
Chart generation&lt;br&gt;
Conversational memory&lt;br&gt;
Retrieval-Augmented Generation (RAG)&lt;br&gt;
Integration with local LLMs using Ollama&lt;br&gt;
Conclusion&lt;/p&gt;

&lt;p&gt;Text-to-SQL is transforming how users interact with relational databases. By combining Hugging Face, Streamlit, and Python, developers can build applications that allow anyone to query databases using natural language.&lt;/p&gt;

&lt;p&gt;While these systems require careful validation and security controls, they significantly lower the barrier to accessing structured data and improve productivity for both technical and non-technical users.&lt;/p&gt;

&lt;p&gt;As open-source AI models continue to improve, conversational database interfaces will become an increasingly common feature in modern data applications.&lt;/p&gt;

&lt;p&gt;References&lt;br&gt;
Hugging Face Transformers: &lt;a href="https://huggingface.co/docs/transformers" rel="noopener noreferrer"&gt;https://huggingface.co/docs/transformers&lt;/a&gt;&lt;br&gt;
Streamlit Documentation: &lt;a href="https://streamlit.io" rel="noopener noreferrer"&gt;https://streamlit.io&lt;/a&gt;&lt;br&gt;
SQLAlchemy Documentation: &lt;a href="https://docs.sqlalchemy.org" rel="noopener noreferrer"&gt;https://docs.sqlalchemy.org&lt;/a&gt;&lt;br&gt;
Vanna AI: &lt;a href="https://github.com/vanna-ai/vanna" rel="noopener noreferrer"&gt;https://github.com/vanna-ai/vanna&lt;/a&gt;&lt;br&gt;
SQLCoder: &lt;a href="https://github.com/defog-ai/sqlcoder" rel="noopener noreferrer"&gt;https://github.com/defog-ai/sqlcoder&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>machinelearning</category>
      <category>python</category>
      <category>llm</category>
    </item>
  </channel>
</rss>
