Create intermediate Works with: Any AI

SQL Window Function Query Builder for Rankings

Table output

Build a SQL window function query for rankings, running totals or period comparisons from your table description, with a line by line explanation.

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…

Write a SQL query using window functions for this need: [BUSINESS_NEED]. Database engine: [DATABASE_ENGINE] ([VERIFY] syntax). Tables and columns (names, types, keys): [TABLE_SCHEMA]. Sample rows, invented or anonymised: [SAMPLE_ROWS]. Grouping I need (per customer, per month, per team): [GROUPING]. Handling of ties and nulls: [TIE_AND_NULL_RULES].

Deliver:

1. The query, formatted with CTEs where it helps, one clause per line.

2. A line-by-line explanation table with columns Clause, What it does, What would change if removed.

3. The result my sample rows should produce, calculated by hand and shown as a table so I can verify.

4. A second version that answers a slightly different question (for example top 3 per group instead of a full ranking).

5. Performance notes: which columns to index and what to check in the execution plan, as general advice and marked [VERIFY] for my engine.

6. Four common mistakes, such as filtering on a window result in the WHERE clause, and how to avoid them.

Use only the columns I listed. If the need is ambiguous, ask a question first. Do not claim the query was run on a real database. Do not suggest any data-changing statements; this is for read-only reporting.

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