Window Functions and Running Totals in SQL

3 Views
0 Uses

Prompt

ChatGPT
Claude
Grok
OpenRouter
You are a SQL tutor-analyst who uses window functions to solve real reporting problems.

SQL dialect: {{SQL_DIALECT}}
Table and columns (with types) and two or three sample rows: {{TABLE_SAMPLE}}
The result I want, for example running total, rank within group, previous-row difference, moving average, first or last value per customer: {{RESULT_NEEDED}}

Write the query. Then:
1. Explain PARTITION BY, ORDER BY and the frame clause (ROWS versus RANGE) as they apply here, with the plain-language meaning of each.
2. Say how ties are treated and whether ROW_NUMBER, RANK or DENSE_RANK fits, with why.
3. Show the expected output for my sample rows, computed by hand so I can compare with the real output.
4. Explain how to filter on a window result, since it can't go in WHERE, and show the pattern.
5. Mention a version difference if my dialect lacks a feature you used, and offer a fallback.

State assumptions. If the sample doesn't show the ordering column clearly, ask. Warn me about two traps: a running total with no ORDER BY in the window returns the partition total on every row, and with ORDER BY but no frame clause the default RANGE frame gives tied order values the same running total.

Customize this prompt

Fill in the variables below to personalize this prompt:

ChatGPT
Claude
Grok
OpenRouter

Model Settings

Recommended Models

ChatGPT Claude Gemini

Temperature

0.7

Max Tokens

2000

Additional Notes

Best for: Running totals, ranks and row-to-row changes without self-joins

About this prompt

“Window Functions and Running Totals in SQL” is a free prompt for ChatGPT in the Coding & Software category, 203 words long, added to The Prompt Index on October 28, 2026 by The Prompt Index. Copy it with the button above, paste it into ChatGPT or any other AI chat assistant, and send it.

It has 3 placeholders to fill in first: {{SQL_DIALECT}}, {{TABLE_SAMPLE}}, {{RESULT_NEEDED}}. Use the “Customize this prompt” fields to personalise it before you copy.

Recommended models: ChatGPT, Claude, Gemini.

0 upvotes, sent to an AI tool 0 times, viewed 3 times.

Browse all Coding & Software prompts, the AI prompt database or use the AI prompt finder to search every prompt.