Prompt Details
Model
Chat - (gpt-5.6)
Token size
304
Example input
[REQUEST]: the top 10 customers by total amount spent in 2025
[SCHEMA]: customers(id, name, country), orders(id, customer_id, amount, status, created_at)
[DIALECT]: PostgreSQL
[CONSTRAINTS]: only completed orders, year 2025, highest spend first, limit 10
[OUTPUT_PREFERENCE]: the query plus a short explanation
Example output
1. QUERY
SQL
SELECT
c.id,
c.name,
c.country,
SUM(o.amount) AS total_spent
FROM customers AS c
JOIN orders AS o
ON c.id = o.customer_id
WHERE o.status = 'completed'
AND o.created_at >= DATE '2025-01-01'
AND o.created_at < DATE '2026-01-01'
GROUP BY
c.id,
c.name,
c.country
ORDER BY
total_spent DESC
LIMIT 10;
2. EXPLANATION
Joins customers to orders using customers.id = orders.customer_id.
Filters to:
only orders with status = 'completed'
only orders created during the 2025 calendar year.
Groups the results by customer so all qualifying orders for each customer are aggregated.
Uses SUM(o.amount) to calculate each customer's total spend.
Sorts by total spend in descending order.
Returns only the top 10 customers.
Using a date range (>= '2025-01-01' and < '2026-01-01') is generally more efficient than extracting the year because it allows PostgreSQL to use an index on created_at.
3. ASSUMPTIONS
orders.customer_id is a foreign key referencing customers.id.
orders.amount contains the monetary value of each order.
A completed order is represented by status = 'completed'.
created_at is a DATE, TIMESTAMP, or TIMESTAMPTZ column.
Only customers with at least one completed order in 2025 should appear in the results.
4. PERFORMANCE TIPS
For a large orders table, consider these indexes:
SQL
CREATE INDEX idx_orders_status_created_customer
ON orders (status, created_at, customer_id);
Or, if completed orders are the overwhelming majority of queries:
SQL
CREATE INDEX idx_orders_completed_2025
ON orders (created_at, customer_id)
WHERE status = 'completed';
Additional tips:
Keep the date filter as a range instead of using EXTRACT(YEAR FROM created_at), since range predicates are index-friendly.
Ensure customers.id is the primary key (typically indexed automatically).
5. VARIATION
If you only need the customer ID and name (not country), you can simplify the grouping:
SQL
SELECT
c.id,
c.name,
SUM(o.amount) AS total_spent
FROM orders AS o
JOIN customers AS c
ON c.id = o.customer_id
WHERE o.status = 'completed'
AND o.created_at >= DATE '2025-01-01'
AND o.created_at < DATE '2026-01-01'
GROUP BY
c.id,
c.name
ORDER BY
total_spent DESC
LIMIT 10;
This version produces the same ranking while returning fewer customer attributes.
By purchasing this prompt, you agree to our terms of service
GPT-5.6
Turn a plain-English request into a correct SQL query for your database. Give the question, your tables and columns, the dialect, and any filters or sorting — and get a ready-to-run query, a line-by-line explanation, the assumptions made, and tips to make it faster. Handles joins, grouping, aggregates, and date filters. Perfect for analysts, developers, and anyone who knows what they want but not the exact syntax. From question to query in one step.
...more
Added over 1 month ago
