Create intermediate Works with: Any AI

SQL Window Functions Explained With Worked Examples

Structured brief

Learn ranking, running totals and comparisons across rows.

The Open buttons send the prompt text as currently shown below to that site only when you click. Fill in the placeholders first.

[VERIFY] is an editorial flag, not a blank to fill. Wherever the prompt shows [VERIFY], check that item against a reliable source before you use it.

Rate this prompt

Loading ratings…

Teach me SQL window functions using my own tables. Database system and version: [DATABASE]. Table names, columns and a few sample rows (invented or anonymised): [TABLES_AND_SAMPLES]. What I need to calculate: [GOALS], for example rank customers by spend within each region, running total of sales by date, difference from the previous month.

Use only the tables and columns I listed; if you need more detail, ask. For each goal give the query in a code block followed by a small result table on my sample rows, and explain it clause by clause: the function, the partition, the ordering and the frame, in plain words. Show the arithmetic for one running total. Compare the window approach with GROUP BY or a self-join and say when each is better. Common mistakes: forgetting the partition, ties in rank functions (compare ROW_NUMBER, RANK and DENSE_RANK with two equal values), and frames that default to something surprising for running totals. Add three practice questions with answers. Test queries on a copy or read-only account, and mark dialect syntax [VERIFY].

Fill in before use

Optional: type your details and the prompt above updates. Nothing is saved or sent.

Replace each bracketed placeholder with your own details, then paste into Any AI or another assistant.

Related prompts