How to use AI to write SQL queries
gpt-4o to convert natural language descriptions into SQL queries by sending prompts to the chat.completions.create endpoint. Provide clear instructions and database schema context in the prompt to get accurate SQL code.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0
Setup
Install the OpenAI Python SDK and set your API key as an environment variable to authenticate requests.
pip install openai>=1.0 Step by step
This example shows how to send a natural language request to generate a SQL query using the gpt-4o model. It includes the database schema in the prompt for context.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
prompt = (
"You are an expert SQL assistant. Given the following database schema:\n"
"Tables:\n"
"- users(id INT, name TEXT, email TEXT)\n"
"- orders(id INT, user_id INT, amount FLOAT, order_date DATE)\n"
"Write a SQL query to find the total order amount per user in 2025."
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": prompt}]
)
sql_query = response.choices[0].message.content
print("Generated SQL query:\n", sql_query) Generated SQL query: SELECT u.name, SUM(o.amount) AS total_amount FROM users u JOIN orders o ON u.id = o.user_id WHERE o.order_date BETWEEN '2025-01-01' AND '2025-12-31' GROUP BY u.name;
Common variations
You can use asynchronous calls, switch to other models like claude-3-5-sonnet-20241022, or add streaming to get partial results as they generate. Adjust the prompt to include more schema details or constraints for complex queries.
import asyncio
import os
from openai import OpenAI
async def generate_sql_async():
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
prompt = (
"Given tables users(id, name) and orders(user_id, amount), "
"write a SQL query to get the average order amount per user."
)
response = await client.chat.completions.acreate(
model="gpt-4o",
messages=[{"role": "user", "content": prompt}]
)
print("Async generated SQL:\n", response.choices[0].message.content)
asyncio.run(generate_sql_async()) Async generated SQL: SELECT u.name, AVG(o.amount) AS average_amount FROM users u JOIN orders o ON u.id = o.user_id GROUP BY u.name;
Troubleshooting
If the AI generates incorrect or incomplete SQL, refine your prompt by adding more schema details or examples. Ensure your API key is set correctly in os.environ["OPENAI_API_KEY"]. For rate limits or errors, check your OpenAI dashboard and handle exceptions in code.
Key Takeaways
- Provide clear database schema context in prompts for accurate SQL generation.
- Use
gpt-4owith the OpenAI Python SDK for straightforward SQL query generation. - Async and streaming calls enable more flexible and responsive query generation workflows.