How I Used Amazon Bedrock and Claude to Generate SQL Queries
How I Used Amazon Bedrock and Claude to Generate SQL Queries
Introduction
I tried using Amazon Bedrock with Claude to help write SQL queries. The goal was simple: give a request in plain English and get a working SQL query.
I also gave Claude the database schema, which means the table names and column names. This helped Claude generate accurate queries.
In this blog, I will show how I did it and what I learned.
Why I Tried This
Writing SQL can be hard:
• If you are not an expert, it is easy to make mistakes.
• Complex queries with joins and group by take time.
• Translating business questions into SQL can be frustrating.
So my approach was simple:
1. Give Claude the database schema and the request in plain English.
2. Get a SQL query that matches the schema.
How I Set It Up
Here is the Python code I used:
import boto3
import json
# Connect to Bedrock
bedrock = boto3.client(service_name="bedrock-runtime")
def generate_sql(user_input, schema):
# Include the schema in the request
prompt = f"Database Schema:\n{schema}\n\nRequest:\n{user_input}\n\nSQL:"
response = bedrock.invoke_model(
modelId="anthropic.claude-v2",
body=json.dumps({
"prompt": prompt,
"max_tokens_to_sample": 300,
"temperature": 0
})
)
return response['body'].read().decode("utf-8")
# Example schema
schema = """
employees(id, name, department, salary)
orders(id, customer_id, order_date, purchase_amount)
"""
# Example request
print(generate_sql("Find the top 5 customers by total purchase amount in 2024", schema))
Passing the schema helps Claude know the correct table and column names.
Examples I Tried
1. Request:
“Get the average salary of employees in the IT department.”
Output:
SELECT AVG(salary)
FROM employees
WHERE department = 'IT';
2. Request:
“List top 5 customers who spent the most in 2024.”
Output:
SELECT customer_id, SUM(purchase_amount) AS total_spent
FROM orders
WHERE YEAR(order_date) = 2024
GROUP BY customer_id
ORDER BY total_spent DESC
LIMIT 5;
3. Request:
“Show the monthly revenue trend for 2023.”
Output:
SELECT MONTH(order_date) AS month, SUM(purchase_amount) AS revenue
FROM orders
WHERE YEAR(order_date) = 2023
GROUP BY MONTH(order_date)
ORDER BY month;
Including the schema made these queries correct and usable every time.
What I Liked About This Approach
• Easy to use, just call the API.
• Queries matched the schema and worked immediately.
• Can be used in bigger workflows, like helping analysts.
• Safe, with built-in access control.
What I Learned
• Always include the database schema in the request.
• Keep temperature = 0 to get consistent results.
• Best for read queries; check updates or inserts carefully.
• Adding example queries in the request improves results.
Closing Thoughts
Using Bedrock and Claude can save time and reduce mistakes when writing SQL queries. Including the database schema makes the queries more accurate.
This approach is useful for analysts who want results quickly without getting stuck writing complex SQL.
Next, I plan to use this for a tool where analysts can ask questions in plain English and get SQL queries automatically.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article