Duration
23s
Input Tokens
782
Output Tokens
1043
Cost
$0.00
Context
Input
Find the name of the dealership that has the highest total revenue from vehicle sales
Expected output
SELECT d.name FROM dealerships d JOIN sales s ON d.branch_id = s.branch_id GROUP BY d.name ORDER BY SUM(s.sale_price) DESC LIMIT 1;
Model output
WITH revenue AS (
SELECT branch_id, SUM(sale_price) AS total_revenue
FROM sales
GROUP BY branch_id
), ranked AS (
SELECT branch_id, total_revenue, RANK() OVER (ORDER BY total_revenue DESC) AS rnk
FROM revenue
)
SELECT d.name
FROM ranked r
JOIN dealerships d ON d.branch_id = r.branch_id
WHERE r.rnk = 1;