Skip to main content
Back to Library
Prompt Engineering Guide

Mastering Write SQL query
on Grok-1

Stop guessing. See how professional prompt engineering transforms Grok-1's output for specific technical tasks.

The "Vibe" Prompt

"Write a SQL query to get the top 5 customers by total order amount from the 'orders' table. Include customer_id, customer_name, and total_amount. Join with the 'customers' table on customer_id."
Low specificity, inconsistent output

Optimized Version

STABLE
{ "target_task": "SQL Query Generation", "context": "Database Schema", "schema_details": { "tables": [ { "name": "orders", "columns": [ { "name": "order_id", "type": "INT", "is_pk": true }, { "name": "customer_id", "type": "INT", "is_fk": true, "references": "customers" }, { "name": "order_amount", "type": "DECIMAL" }, { "name": "order_date", "type": "DATE" } ] }, { "name": "customers", "columns": [ { "name": "customer_id", "type": "INT", "is_pk": true }, { "name": "customer_name", "type": "VARCHAR" }, { "name": "email", "type": "VARCHAR" } ] } ] }, "request": "Generate an SQL query to retrieve the top 5 customers based on their total order amount. The output should include the customer's ID, name, and their aggregated total order amount.", "constraints": [ "Use JOIN for linking tables.", "Aggregate `order_amount` from the `orders` table for each customer.", "Order results in descending order by total amount.", "Limit the output to the top 5.", "Column aliases: `customer_id`, `customer_name`, `total_amount`" ], "chain_of_thought_steps": [ "1. Identify necessary tables: `orders` and `customers`.", "2. Determine the join condition: `orders.customer_id = customers.customer_id`.", "3. Identify columns for selection: `customers.customer_id`, `customers.customer_name`.", "4. Calculate the total order amount: `SUM(orders.order_amount)` and alias it as `total_amount`.", "5. Group the results by customer: `GROUP BY customers.customer_id, customers.customer_name`.", "6. Order the grouped results: `ORDER BY total_amount DESC`.", "7. Limit the output: `LIMIT 5`.", "8. Construct the final SQL query based on these steps." ] }
Structured, task-focused, reduced hallucinations

Engineering Rationale

The optimized prompt leverages a structured JSON format explicitly defining the task, context, schema, and constraints, which significantly reduces ambiguity. The 'chain_of_thought_steps' guides the AI through the logical construction of the SQL query, mirroring how a human would approach the problem. This pre-computation of steps helps the model focus its reasoning and ensures all requirements are met systematically. By providing the schema upfront and using clear, isolated instructions, it prevents the model from needing to infer database structure or task sub-steps, leading to more accurate and reliable output.

0%
Token Efficiency Gain
The generated SQL query must correctly join 'orders' and 'customers'.
The query must calculate the sum of 'order_amount' per customer.
The query must group results by customer_id and customer_name.

Ready to stop burning tokens?

Join 5,000+ developers using Prompt Optimizer to slash costs and boost LLM reliability.

Optimize My Prompts