SQL Generation

Turn plain-English questions into correct SQL against a schema. The skill behind analytics chat, natural-language dashboards, and 'just ask the database' interfaces.

50
Duration
24s
Input Tokens
839
Output Tokens
2040
Cost
$0.01
Context
Input
Show the names of customers who paid more than the average sale price for the vehicles they bought.
Expected output
SELECT c.name FROM customers c JOIN sales s ON c.customer_id = s.customer_id WHERE s.sale_price > (SELECT AVG(s2.sale_price) FROM sales s2 WHERE s2.vehicle_id = s.vehicle_id);
Model output
WITH avg_model_sales AS (
    SELECT
        v.model,
        AVG(s.sale_price) AS average_sale_price_for_model
    FROM
        sales s
    JOIN
        vehicles v ON s.vehicle_id = v.vehicle_id
    GROUP BY
        v.model
)
SELECT DISTINCT
    c.name
FROM
    customers c
JOIN
    sales s ON c.customer_id = s.customer_id
JOIN
    vehicles v ON s.vehicle_id = v.vehicle_id
JOIN
    avg_model_sales ams ON v.model = ams.model
WHERE
    s.sale_price > ams.average_sale_price_for_model;