Websites, apps & code / Coding & debugging
Write a SQL query
Your data is in a database and you want a SQL query for the numbers or list you need.
Fill in your details
Leave blank to use the example. Names are remembered for other prompts too (stored only on your device).
Prompt
Please write a query for me. Database: Use MySQL syntax. Tables and columns: orders(id, customer_id, total, created_at); customers(id, name, city) What I want to know: Last month's order total per city, highest first Please: 1. Confirm your understanding of the need and ask about anything unclear. 2. Give the query, then explain it line by line in plain words. 3. Write read-only queries (SELECT) only. For anything that changes or deletes data, warn me, ask me to back up, and only write it after I ask again. 4. Explain how to check the result is right (for example by hand-checking a small sample). 5. Tell me whether it could be slow on a large dataset. Do not ask me to paste passwords, API keys, IC numbers or customer data; if my code contains any, remind me to replace them with [FAKE] values first.
Paste it into ChatGPT, Gemini, Claude or Meta AI.
Test result
What we noticed
The AI restated the need, asked about "last month" and cancelled orders, and gave a read-only SELECT explained line by line. It was not run against any database; it taught hand-checking with fake orders instead.
See what the AI actually wrote
First my understanding, then the query.
My understanding: you want last month's order total per city, highest first. The amount is orders.total, the city is customers.city, and they join on orders.customer_id = customers.id.
Two things I would like to confirm (they do not change the query below but do change the result):
- "Last month" means the last full calendar month (for example if today is 15 December, 1 to 30 November), right?
- Do orders have a status like "cancelled"? Your columns show none, so I count all of them.
The query (read-only, changes no data):
SELECT c.city, SUM(o.total) AS city_total FROM orders AS o JOIN customers AS c ON c.id = o.customer_id WHERE o.created_at >= DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 1 MONTH), '%Y-%m-01') AND o.created_at < DATE_FORMAT(CURDATE(), '%Y-%m-01') GROUP BY c.city ORDER BY city_total DESC;
Line by line:
SELECT c.city, SUM(o.total) AS city_total: take the city and the sum of that city's order amounts, naming the sum city_total.FROM orders AS o JOIN customers AS c ON ...: start from orders and use the customer id to attach each order to a customer, so we know the city.- The first
WHEREline: order time is on or after the 1st of last month.CURDATE()is today, minus one month, andDATE_FORMATturns it into the 1st of that month. - The
ANDline: and before the 1st of this month. Using<rather than<=the last day keeps orders placed late on the last day. GROUP BY c.city: orders of the same city are grouped to be added up.ORDER BY city_total DESC: biggest total first.
How to check it: in a test database put 5 fake orders in 2 cities, add each city up by hand, and compare with the result. You can also run SELECT SUM(total) ... with the same date condition and check the cities add up to it; if not, some orders may not match a customer (the JOIN drops them).
Could it be slow: with a very large number of orders (hundreds of thousands of rows), if created_at has no index the database scans the whole table and it may be slow. Ask your database admin or use EXPLAIN. An index on created_at usually helps, but that changes the database structure, so back up first and ask me again before I write it.
I wrote only a SELECT. For anything that changes or deletes data, back up first and tell me once more.
AI tools that suit this
- Claude Suggested tool Free plan
Strong at long documents, editing and code
- ChatGPT Suggested tool Free plan
The most widely used AI chat, for writing, questions and images
- Gemini Suggested tool Free plan
Google's AI; generates and edits images, works with Gmail and Docs
Recommendations only, not sponsored. Check each site for what the free plan includes.
This site is made by FullMark Tech
Want your own website, or your shop spreadsheets automated? FullMark Tech builds business websites and custom Excel tools.