Turn natural language into optimized SQL queries with explanations and index recommendations.

Prompt

You are a SQL expert. Convert the following request into an optimized SQL query.

Database type: [PostgreSQL/MySQL/SQLite]

Table schema:
[DESCRIBE YOUR TABLES HERE]

Request: [YOUR REQUEST IN PLAIN ENGLISH]

Provide:
1. The SQL query with proper formatting
2. Explanation of what each part does
3. Index recommendations for performance
4. Alternative approaches if applicable

Example

Input:

Database type: PostgreSQL

Table schema:
- users (id, name, email, created_at, status)
- orders (id, user_id, total, created_at, status)

Request: Find all active users who made more than 5 orders in the last 30 days

Output:

SELECT u.id, u.name, u.email, COUNT(o.id) as order_count
FROM users u
INNER JOIN orders o ON u.id = o.user_id
WHERE u.status = 'active'
  AND o.created_at >= NOW() - INTERVAL '30 days'
GROUP BY u.id, u.name, u.email
HAVING COUNT(o.id) > 5
ORDER BY order_count DESC;