{"id":1470,"date":"2026-08-26T09:57:07","date_gmt":"2026-08-26T12:57:07","guid":{"rendered":"https:\/\/prompteiro.com\/en\/prompt\/sql-query-from-your-question\/"},"modified":"2026-08-26T09:57:08","modified_gmt":"2026-08-26T12:57:08","slug":"sql-query-from-your-question","status":"publish","type":"prompt","link":"https:\/\/prompteiro.com\/en\/prompt\/sql-query-from-your-question\/","title":{"rendered":"An SQL Query From Your Own Question"},"featured_media":1471,"template":"","meta":{"_titulo_seo":"How to write an SQL query with AI without errors","_prompt_text":"Act as a senior data person who once spent a night fixing a report that looked beautiful and counted the same order twice, and who because of that never hands over a query without handing over the way to check it.\n\nContext: my database is [WHICH DATABASE AND VERSION - EX: PostgreSQL 15]. The question I need to answer is [THE QUESTION IN PLAIN WORDS, THE WAY YOU WOULD TELL A COLLEAGUE - EX: how many new customers bought more than once last quarter]. These are the tables and columns I have, and there are no others:\n\n[PASTE EACH TABLE HERE WITH ITS COLUMNS AND TYPES]\n\nThe volume is [ROUGH SIZE - EX: about 2 million rows in orders]. My permission is [WHAT YOU CAN DO - EX: read only]. The time zone of the data is [TIME ZONE - EX: UTC, and I need the result in my local time].\n\nTask: write the query that answers exactly that question, explain in plain words what it does and give me a way to check the result.\n\nRules:\n1. Use only the tables and columns I listed. If something is missing to answer the question, say what is missing instead of inventing a column.\n2. Write read only. No DELETE, UPDATE, DROP or ALTER, not even as a commented suggestion.\n3. Explain the query in plain words, block by block, saying what each join and each filter is leaving out. The reader should not need to know SQL to follow it.\n4. Handle null on purpose and say which decision you took: a row with no value counts, drops out, or becomes zero.\n5. Watch what duplicates rows. If any join could multiply the result, point out where and show how you avoided it.\n6. Dates and time zones come out converted to the zone I asked for, and the range is explicit: say whether the last day is in or out.\n7. Also deliver a checking query, simpler and by a different route, that proves the number from the first one is right.\n8. If my question is ambiguous, take the most common reading, carry on, and say at the end which reading you used and what the other one would be.\n9. Format it for a human to read: one clause per line, short table aliases and a comment only where the line does not explain itself.\n10. Say one thing that would make the query faster at the volume I told you about, and say when it is not worth doing.\n\nAnswer format: first the finished query, in a code block, with no text in the middle; then the explanation in plain words, block by block; then the checking query, in another code block, with one sentence saying what its result has to match; then the list of what you assumed; and at the end the note on performance.","_como_usar":"Writing the query is the easy part. The expensive part is finding out a week later that the number came out inflated because a join duplicated rows, and the report has already gone around. This prompt gives you the query, the explanation in plain words and a second query to check the first. It works in ChatGPT, Claude and Gemini.\n\n1. Paste the column names with their types. Without that the AI invents a plausible column and the query breaks on the database.\n2. Ask the question in plain words, the way you would tell a colleague. A question translated into half SQL gets half SQL back.\n3. Give the time zone of the data: it is the silent source of the report that is one day out.\n4. Run the checking query before you send the number to anyone. If the two do not match, the error is there.\n5. Read the list of what was assumed: that is where the decision about rows with no value shows up, and it changes the total.\n\nTip: keep the queries that worked with the original question above them. It becomes your dictionary of the database.","_views":0,"_likes":0,"_nota_sum":0,"_nota_count":0,"_nota_avg":0,"_nota_weight":0,"_i18n_grupo":1929},"macro":[3],"subcategoria":[1329],"prompt_tag":[1082,1341,34,1338],"ferramenta":[13,14,15],"class_list":["post-1470","prompt","type-prompt","status-publish","has-post-thumbnail","hentry","macro-text","subcategoria-programming","prompt_tag-analysis","prompt_tag-code","prompt_tag-explanation","prompt_tag-programming","ferramenta-chatgpt","ferramenta-claude","ferramenta-gemini"],"_links":{"self":[{"href":"https:\/\/prompteiro.com\/en\/wp-json\/wp\/v2\/prompt\/1470","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prompteiro.com\/en\/wp-json\/wp\/v2\/prompt"}],"about":[{"href":"https:\/\/prompteiro.com\/en\/wp-json\/wp\/v2\/types\/prompt"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/prompteiro.com\/en\/wp-json\/wp\/v2\/media\/1471"}],"wp:attachment":[{"href":"https:\/\/prompteiro.com\/en\/wp-json\/wp\/v2\/media?parent=1470"}],"wp:term":[{"taxonomy":"macro","embeddable":true,"href":"https:\/\/prompteiro.com\/en\/wp-json\/wp\/v2\/macro?post=1470"},{"taxonomy":"subcategoria","embeddable":true,"href":"https:\/\/prompteiro.com\/en\/wp-json\/wp\/v2\/subcategoria?post=1470"},{"taxonomy":"prompt_tag","embeddable":true,"href":"https:\/\/prompteiro.com\/en\/wp-json\/wp\/v2\/prompt_tag?post=1470"},{"taxonomy":"ferramenta","embeddable":true,"href":"https:\/\/prompteiro.com\/en\/wp-json\/wp\/v2\/ferramenta?post=1470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}