Library › Data AI › Business analysts

Write SQL for top customers by net revenue

  • For Business analysts
  • You practice Setting limits and rules
  • Time about 5 minutes
  • Level Intermediate

About this exercise

A sales leader asked: who are our 5 biggest customers by net revenue in 2025? Ask for one Postgres query in a single code block, with at most 40 words of writing outside it. Net revenue means completed orders only, minus refunds, shown in dollars.

Common mistakes when you ask AI for this

  • Joining refunds straight to orders. The AI joins one-to-many and sums order totals again for every refund row. Try: Add: "Sum refunds per order first, then LEFT JOIN so nothing is counted twice."
  • Forgetting the status and year. Without being told, the AI counts every order, including cancelled and pending ones. Try: Add: "Completed orders only, order_date in 2025."
  • Leaving money in cents. The AI returns raw cents and a long unranked list. Try: Add: "Divide cents by 100, order by net revenue descending, LIMIT 5."
  • Leaving the format to chance. The AI picks its own length and layout unless told the exact shape. Try: Add: "Put the code in exactly 1 code block. Keep the writing outside the code under 40 words."

What a strong prompt includes

  • Names the filters
  • Warns about several refunds per order
  • Says the units
  • Asks for ranked top 5
  • Caps the writing

Practice this now ↑

Related exercises