A 10-minute SQL window functions lesson from a chat app, and I re-ran its queries

2026-11-04来源 dev.to AIEnglish

Quick note first: I do marketing for HelloAI (tryhello.app), and this is a test I ran on its free plan. I'll show the prompt, what it gave me, and how I checked it.

The prompt, word for word:

Teach me SQL window functions in 10 minutes. Use this table: sales(rep, month, amount) with rows (Ana,Jan,100),(Ana,Feb,150),(Ana,Mar,120),(Ben,Jan,90),(Ben,Feb,200),(Ben,Mar,110). Show a running total per rep and a rank of reps by amount within each month, give the exact result tables, then 3 practice questions with answers.

It came back in about ten seconds as an editable document, roughly 430 words. Here is the useful part.

The idea it opened with

A window function calculates a value over a set of related rows while keeping each original row. Unlike GROUP BY, it doesn't collapse rows. PARTITION BY splits rows into groups, ORDER BY sets the order inside each group.

Running total per rep

Month names are text, so it spelled out the calendar order and the frame instead of leaning on defaults:

SELECT rep, month, amount, SUM(amount) OVER ( PARTITION BY rep ORDER BY CASE month WHEN 'Jan' THEN 1 WHEN 'Feb' THEN 2 WHEN 'Mar' THEN 3 END ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) AS running_total FROM sales ORDER BY rep, CASE month WHEN 'Jan' THEN 1 WHEN 'Feb' THEN 2 WHEN 'Mar' THEN 3 END; 
rep month amount running_total
Ana Jan 100 100
Ana Feb 150 250
Ana Mar 120 370
Ben Jan 90 90
Ben Feb 200 290
Ben Mar 110 400

Rank reps within each month

SELECT rep, month, amount, RANK() OVER (PARTITION BY month ORDER BY amount DESC) AS month_rank FROM sales ORDER BY CASE month WHEN 'Jan' THEN 1 WHEN 'Feb' THEN 2 WHEN 'Mar' THEN 3 END, month_rank; 
rep month amount month_rank
Ana Jan 100 1
Ben Jan 90 2
Ben Feb 200 1
Ana Feb 150 2
Ana Mar 120 1
Ben Mar 110 2

It also noted that with this data there are no ties, so RANK(), ROW_NUMBER() and DENSE_RANK() agree here, and that RANK() leaves gaps after a tie while DENSE_RANK() doesn't.

Practice questions it wrote

  1. What is Ana's running total in March? (370)
  2. What is Ben's rank in February? (1, his 200 is the highest that month)
  3. What does PARTITION BY rep do in the running-total query? (Each rep's rows get their own window, so the total restarts for Ben.)

How I checked it

I loaded the same six rows into SQLite 3.37.2 and ran both queries exactly as written. Both result tables matched what it showed, row for row. That covers this sample and these two queries, nothing more. The three practice answers also hold against that data.

Small nits: the document header and footer disagreed on the word count (523 vs 427), and the last sentence of question 3's answer reads a bit awkwardly. Nothing that changed the SQL or the numbers.

What I liked: it gave me runnable queries, exact tables and practice questions in one pass, and I could edit the document afterwards. It also offered to turn the same topic into a guided tutor lesson, which I haven't tried.

原文:https://www.a8q9.cn/en/article/a-10-minute-sql-window-functions-lesson-from-a-chat-app-and-i-re-ran-its-queries-7fefa1.html

相关文章