Learn with DataTug · English

SQL Window Functions Explained with Practical Examples

Keep individual rows while calculating totals, ranks and trends. Learn OVER, PARTITION BY, ROWS frames, LAG and top N with tested Chinook SQLite examples and charts.

By DataTug · AI-assisted writing · Published · Updated . SQL examples are checked against the downloadable Chinook SQLite database.

Show every sale alongside customer spending

An analyst wants one invoice per row, with the customer’s total spending beside it. GROUP BY provides totals but removes invoice detail. Joining a grouped subquery adds that detail back. A window aggregate often states the request more directly.

A window function calculates a value over a related set of query rows while preserving individual rows. OVER defines that set, its ordering and, for frame-sensitive functions, the portion used in the calculation. Think of it as adding an analytical column without automatically collapsing the input.

We will move from customer totals to ranks, cumulative revenue, moving averages and neighboring invoices. The hard part is choosing the correct population, tie policy and frame—not remembering a function name.

Prerequisite: GROUP BY foundations and WHERE vs HAVING. Related: Aggregation · Filtering · JOINs and Subqueries. Practice: this lesson’s exercises. Follow the SQL Learning Hub path.

The Chinook sample database

These examples use the complete Chinook music-store database available in the DataTug demo: 59 customers and 412 invoices, with every original table and value retained. All 59 customers have invoices. Queries that focus on one customer or qualifying invoices show that condition explicitly in the SQL.

The relationship is Customer.CustomerId → Invoice.CustomerId: one customer can have many invoices; each invoice belongs to one customer. InvoiceId is the invoice’s unique key. InvoiceDate is uniformly formatted YYYY-MM-DD HH:MM:SS text, which supports the date comparisons and ordering shown here; Total is a numeric amount in the dataset’s units. This source uses dates from 2021–2025; other Chinook releases differ. Use InvoiceDate rather than assuming invoice IDs guarantee date order.

Results are shown as a preview for clarity. Tables with more than 10 records initially show the first 10. Choose Show all to reveal every matching record, or Run to execute the exact displayed SQL in the DataTug editor against the same Chinook database. No query is saved to the project. The preview does not add a WHERE or LIMIT clause. Synthetic examples define their own rows with VALUES in the displayed query.

Download the complete Chinook SQLite database and open it with SQLite 3.25.0 or later, then run any query below. No account or network database is required. Query downloads contain every displayed example and exercise solution. Frozen result tables are checked against this database; each code header states its minimum SQLite version.

Chinook upstream and schema · Chinook licence · Source attribution. SQLite uses binary floating-point for many decimal operations; examples round money for display. Production finance needs an explicit exact-decimal or integer-minor-unit policy.

Invoice source rows

Which invoices belong to customer 1?

SQLite 3+ · Chinook
SELECT InvoiceId, CustomerId, InvoiceDate, Total
FROM Invoice WHERE CustomerId = 1
ORDER BY InvoiceDate, InvoiceId;
Invoice source rows: expected result · 7 records
InvoiceIdCustomerIdInvoiceDateTotal
9812022-03-11 00:00:003.98
12112022-06-13 00:00:003.96
14312022-09-15 00:00:005.94
19512023-05-06 00:00:000.99
31612024-10-27 00:00:001.98
32712024-12-07 00:00:0013.86
38212025-08-07 00:00:008.91

GROUP BY collapses; a window annotates

One input, two output grains

  1. 1412 invoice rows across 59 customers
  2. 2GROUP BY: 59 customer summary rows
  3. 3Window SUM: 412 invoice rows with customer totals
These are alternative output shapes. Choose whether the consumer needs summaries or invoice detail.

A grouped aggregate returns one row per group. A window aggregate computes across a partition but retains input rows. Compare the source and outputs, and highlight a customer to see how their total repeats.

Source, summary and annotated detail

All invoice source rows

Inspect the exact input population for the grouped and window outputs.

SQLite 3+ · Chinook
SELECT InvoiceId, CustomerId, InvoiceDate, Total
FROM Invoice
ORDER BY CustomerId, InvoiceDate, InvoiceId;
Show all 412 recordsShow first 10 records
All invoice source rows: expected result · Showing 10 of 412 records for clarity412 records
InvoiceIdCustomerIdInvoiceDateTotal
9812022-03-11 00:00:003.98
12112022-06-13 00:00:003.96
14312022-09-15 00:00:005.94
19512023-05-06 00:00:000.99
31612024-10-27 00:00:001.98
32712024-12-07 00:00:0013.86
38212025-08-07 00:00:008.91
122021-01-01 00:00:001.98
1222021-02-11 00:00:0013.86
6722021-10-12 00:00:008.91
19622023-05-19 00:00:001.98
21922023-08-21 00:00:003.96
24122023-11-23 00:00:005.94
29322024-07-13 00:00:000.99
9932022-03-11 00:00:003.98
11032022-04-21 00:00:0013.86
16532022-12-20 00:00:008.91
29432024-07-26 00:00:001.98
31732024-10-28 00:00:003.96
33932025-01-30 00:00:005.94
39132025-09-20 00:00:000.99
242021-01-02 00:00:003.96
2442021-04-06 00:00:005.94
7642021-11-25 00:00:000.99
19742023-05-19 00:00:001.98
20842023-06-29 00:00:0015.86
26342024-02-27 00:00:008.91
39242025-10-03 00:00:001.98
7752021-12-08 00:00:001.98
10052022-03-12 00:00:003.96
12252022-06-14 00:00:005.94
17452023-02-02 00:00:000.99
29552024-07-26 00:00:001.98
30652024-09-05 00:00:0016.86
36152025-05-06 00:00:008.91
4662021-07-11 00:00:008.91
17562023-02-15 00:00:001.98
19862023-05-20 00:00:003.96
22062023-08-22 00:00:005.94
27262024-04-11 00:00:000.99
39362025-10-03 00:00:001.98
40462025-11-13 00:00:0025.86
7872021-12-08 00:00:001.98
8972022-01-18 00:00:0018.86
14472022-09-18 00:00:008.91
27372024-04-24 00:00:001.98
29672024-07-27 00:00:003.96
31872024-10-29 00:00:005.94
37072025-06-19 00:00:000.99
382021-01-03 00:00:005.94
5582021-08-24 00:00:000.99
17682023-02-15 00:00:001.98
18782023-03-28 00:00:0013.86
24282023-11-26 00:00:008.91
37182025-07-02 00:00:001.98
39482025-10-04 00:00:003.96
5692021-09-06 00:00:001.98
7992021-12-09 00:00:003.96
10192022-03-13 00:00:005.94
15392022-11-01 00:00:000.99
27492024-04-24 00:00:001.98
28592024-06-04 00:00:0013.86
34092025-02-02 00:00:008.91
25102021-04-09 00:00:008.91
154102022-11-14 00:00:001.98
177102023-02-16 00:00:003.96
199102023-05-21 00:00:005.94
251102024-01-09 00:00:000.99
372102025-07-02 00:00:001.98
383102025-08-12 00:00:0013.86
57112021-09-06 00:00:001.98
68112021-10-17 00:00:0013.86
123112022-06-17 00:00:008.91
252112024-01-22 00:00:001.98
275112024-04-25 00:00:003.96
297112024-07-28 00:00:005.94
349112025-03-18 00:00:000.99
34122021-05-23 00:00:000.99
155122022-11-14 00:00:001.98
166122022-12-25 00:00:0013.86
221122023-08-25 00:00:008.91
350122025-03-31 00:00:001.98
373122025-07-03 00:00:003.96
395122025-10-05 00:00:005.94
35132021-06-05 00:00:001.98
58132021-09-07 00:00:003.96
80132021-12-10 00:00:005.94
132132022-07-31 00:00:000.99
253132024-01-22 00:00:001.98
264132024-03-03 00:00:0013.86
319132024-11-01 00:00:008.91
4142021-01-06 00:00:008.91
133142022-08-13 00:00:001.98
156142022-11-15 00:00:003.96
178142023-02-17 00:00:005.94
230142023-10-08 00:00:000.99
351142025-03-31 00:00:001.98
362142025-05-11 00:00:0013.86
36152021-06-05 00:00:001.98
47152021-07-16 00:00:0013.86
102152022-03-16 00:00:009.91
231152023-10-21 00:00:001.98
254152024-01-23 00:00:003.96
276152024-04-26 00:00:005.94
328152024-12-15 00:00:000.99
13162021-02-19 00:00:000.99
134162022-08-13 00:00:001.98
145162022-09-23 00:00:0013.86
200162023-05-24 00:00:008.91
329162024-12-28 00:00:001.98
352162025-04-01 00:00:003.96
374162025-07-04 00:00:005.94
14172021-03-04 00:00:001.98
37172021-06-06 00:00:003.96
59172021-09-08 00:00:005.94
111172022-04-29 00:00:000.99
232172023-10-21 00:00:001.98
243172023-12-01 00:00:0013.86
298172024-07-31 00:00:0010.91
112182022-05-12 00:00:001.98
135182022-08-14 00:00:003.96
157182022-11-16 00:00:005.94
209182023-07-07 00:00:000.99
330182024-12-28 00:00:001.98
341182025-02-07 00:00:0013.86
396182025-10-08 00:00:008.91
15192021-03-04 00:00:001.98
26192021-04-14 00:00:0013.86
81192021-12-13 00:00:008.91
210192023-07-20 00:00:001.98
233192023-10-22 00:00:003.96
255192024-01-24 00:00:005.94
307192024-09-13 00:00:001.99
113202022-05-12 00:00:001.98
124202022-06-22 00:00:0013.86
179202023-02-20 00:00:008.91
308202024-09-26 00:00:003.98
331202024-12-29 00:00:003.96
353202025-04-02 00:00:005.94
405202025-11-21 00:00:000.99
16212021-03-05 00:00:003.96
38212021-06-07 00:00:005.94
90212022-01-26 00:00:000.99
211212023-07-20 00:00:001.98
222212023-08-30 00:00:0013.86
277212024-04-29 00:00:008.91
406212025-12-04 00:00:001.98
91222022-02-08 00:00:001.98
114222022-05-13 00:00:003.96
136222022-08-15 00:00:005.94
188222023-04-05 00:00:000.99
309222024-09-26 00:00:003.98
320222024-11-06 00:00:0013.86
375222025-07-07 00:00:008.91
5232021-01-11 00:00:0013.86
60232021-09-11 00:00:008.91
189232023-04-18 00:00:001.98
212232023-07-21 00:00:003.96
234232023-10-23 00:00:005.94
286232024-06-12 00:00:000.99
407232025-12-04 00:00:001.98
92242022-02-08 00:00:001.98
103242022-03-21 00:00:0015.86
158242022-11-19 00:00:008.91
287242024-06-25 00:00:001.98
310242024-09-27 00:00:007.96
332242024-12-30 00:00:005.94
384242025-08-20 00:00:000.99
17252021-03-06 00:00:005.94
69252021-10-25 00:00:000.99
190252023-04-18 00:00:001.98
201252023-05-29 00:00:0018.86
256252024-01-27 00:00:008.91
385252025-09-02 00:00:001.98
408252025-12-05 00:00:003.96
70262021-11-07 00:00:001.98
93262022-02-09 00:00:003.96
115262022-05-14 00:00:005.94
167262023-01-02 00:00:000.99
288262024-06-25 00:00:001.98
299262024-08-05 00:00:0023.86
354262025-04-05 00:00:008.91
39272021-06-10 00:00:008.91
168272023-01-15 00:00:001.98
191272023-04-19 00:00:003.96
213272023-07-22 00:00:005.94
265272024-03-11 00:00:000.99
386272025-09-02 00:00:001.98
397272025-10-13 00:00:0013.86
71282021-11-07 00:00:001.98
82282021-12-18 00:00:0013.86
137282022-08-18 00:00:008.91
266282024-03-24 00:00:001.98
289282024-06-26 00:00:003.96
311282024-09-28 00:00:0011.94
363282025-05-19 00:00:000.99
48292021-07-24 00:00:000.99
169292023-01-15 00:00:001.98
180292023-02-25 00:00:0013.86
235292023-10-26 00:00:008.91
364292025-06-01 00:00:001.98
387292025-09-03 00:00:003.96
409292025-12-06 00:00:005.94
49302021-08-06 00:00:001.98
72302021-11-08 00:00:003.96
94302022-02-10 00:00:005.94
146302022-10-01 00:00:000.99
267302024-03-24 00:00:001.98
278302024-05-04 00:00:0013.86
333302025-01-02 00:00:008.91
18312021-03-09 00:00:008.91
147312022-10-14 00:00:001.98
170312023-01-16 00:00:003.96
192312023-04-20 00:00:005.94
244312023-12-09 00:00:000.99
365312025-06-01 00:00:001.98
376312025-07-12 00:00:0013.86
50322021-08-06 00:00:001.98
61322021-09-16 00:00:0013.86
116322022-05-17 00:00:008.91
245322023-12-22 00:00:001.98
268322024-03-25 00:00:003.96
290322024-06-27 00:00:005.94
342322025-02-15 00:00:000.99
27332021-04-22 00:00:000.99
148332022-10-14 00:00:001.98
159332022-11-24 00:00:0013.86
214332023-07-25 00:00:008.91
343332025-02-28 00:00:001.98
366332025-06-02 00:00:003.96
388332025-09-04 00:00:005.94
28342021-05-05 00:00:001.98
51342021-08-07 00:00:003.96
73342021-11-09 00:00:005.94
125342022-06-30 00:00:000.99
246342023-12-22 00:00:001.98
257342024-02-01 00:00:0013.86
312342024-10-01 00:00:0010.91
126352022-07-13 00:00:001.98
149352022-10-15 00:00:003.96
171352023-01-17 00:00:005.94
223352023-09-07 00:00:000.99
344352025-02-28 00:00:001.98
355352025-04-10 00:00:0013.86
410352025-12-09 00:00:008.91
29362021-05-05 00:00:001.98
40362021-06-15 00:00:0013.86
95362022-02-13 00:00:008.91
224362023-09-20 00:00:001.98
247362023-12-23 00:00:003.96
269362024-03-26 00:00:005.94
321362024-11-14 00:00:000.99
6372021-01-19 00:00:000.99
127372022-07-13 00:00:001.98
138372022-08-23 00:00:0013.86
193372023-04-23 00:00:0014.91
322372024-11-27 00:00:001.98
345372025-03-01 00:00:003.96
367372025-06-03 00:00:005.94
7382021-02-01 00:00:001.98
30382021-05-06 00:00:003.96
52382021-08-08 00:00:005.94
104382022-03-29 00:00:000.99
225382023-09-20 00:00:001.98
236382023-10-31 00:00:0013.86
291382024-06-30 00:00:008.91
105392022-04-11 00:00:001.98
128392022-07-14 00:00:003.96
150392022-10-16 00:00:005.94
202392023-06-06 00:00:001.99
323392024-11-27 00:00:001.98
334392025-01-07 00:00:0013.86
389392025-09-07 00:00:008.91
8402021-02-01 00:00:001.98
19402021-03-14 00:00:0013.86
74402021-11-12 00:00:008.91
203402023-06-19 00:00:002.98
226402023-09-21 00:00:003.96
248402023-12-24 00:00:005.94
300402024-08-13 00:00:000.99
106412022-04-11 00:00:001.98
117412022-05-22 00:00:0013.86
172412023-01-20 00:00:008.91
301412024-08-26 00:00:001.98
324412024-11-28 00:00:003.96
346412025-03-02 00:00:005.94
398412025-10-21 00:00:000.99
9422021-02-02 00:00:003.96
31422021-05-07 00:00:005.94
83422021-12-26 00:00:000.99
204422023-06-19 00:00:003.98
215422023-07-30 00:00:0013.86
270422024-03-29 00:00:008.91
399422025-11-03 00:00:001.98
84432022-01-08 00:00:001.98
107432022-04-12 00:00:003.96
129432022-07-15 00:00:005.94
181432023-03-05 00:00:000.99
302432024-08-26 00:00:001.98
313432024-10-06 00:00:0016.86
368432025-06-06 00:00:008.91
53442021-08-11 00:00:008.91
182442023-03-18 00:00:001.98
205442023-06-20 00:00:007.96
227442023-09-22 00:00:005.94
279442024-05-12 00:00:000.99
400442025-11-03 00:00:001.98
411442025-12-14 00:00:0013.86
85452022-01-08 00:00:001.98
96452022-02-18 00:00:0021.86
151452022-10-19 00:00:008.91
280452024-05-25 00:00:001.98
303452024-08-27 00:00:003.96
325452024-11-29 00:00:005.94
377452025-07-20 00:00:000.99
10462021-02-03 00:00:005.94
62462021-09-24 00:00:000.99
183462023-03-18 00:00:001.98
194462023-04-28 00:00:0021.86
249462023-12-27 00:00:008.91
378462025-08-02 00:00:001.98
401462025-11-04 00:00:003.96
63472021-10-07 00:00:001.98
86472022-01-09 00:00:003.96
108472022-04-13 00:00:005.94
160472022-12-02 00:00:000.99
281472024-05-25 00:00:001.98
292472024-07-05 00:00:0013.86
347472025-03-05 00:00:008.91
32482021-05-10 00:00:008.91
161482022-12-15 00:00:001.98
184482023-03-19 00:00:003.96
206482023-06-21 00:00:008.94
258482024-02-09 00:00:000.99
379482025-08-02 00:00:001.98
390482025-09-12 00:00:0013.86
64492021-10-07 00:00:001.98
75492021-11-17 00:00:0013.86
130492022-07-18 00:00:008.91
259492024-02-22 00:00:001.98
282492024-05-26 00:00:003.96
304492024-08-28 00:00:005.94
356492025-04-18 00:00:000.99
41502021-06-23 00:00:000.99
162502022-12-15 00:00:001.98
173502023-01-25 00:00:0013.86
228502023-09-25 00:00:008.91
357502025-05-01 00:00:001.98
380502025-08-03 00:00:003.96
402502025-11-05 00:00:005.94
42512021-07-06 00:00:001.98
65512021-10-08 00:00:003.96
87512022-01-10 00:00:006.94
139512022-08-31 00:00:000.99
260512024-02-22 00:00:001.98
271512024-04-03 00:00:0013.86
326512024-12-02 00:00:008.91
11522021-02-06 00:00:008.91
140522022-09-13 00:00:001.98
163522022-12-16 00:00:003.96
185522023-03-20 00:00:005.94
237522023-11-08 00:00:000.99
358522025-05-01 00:00:001.98
369522025-06-11 00:00:0013.86
43532021-07-06 00:00:001.98
54532021-08-16 00:00:0013.86
109532022-04-16 00:00:008.91
238532023-11-21 00:00:001.98
261532024-02-23 00:00:003.96
283532024-05-27 00:00:005.94
335532025-01-15 00:00:000.99
20542021-03-22 00:00:000.99
141542022-09-13 00:00:001.98
152542022-10-24 00:00:0013.86
207542023-06-24 00:00:008.91
336542025-01-28 00:00:001.98
359542025-05-02 00:00:003.96
381542025-08-04 00:00:005.94
21552021-04-04 00:00:001.98
44552021-07-07 00:00:003.96
66552021-10-09 00:00:005.94
118552022-05-30 00:00:000.99
239552023-11-21 00:00:001.98
250552024-01-01 00:00:0013.86
305552024-08-31 00:00:008.91
119562022-06-12 00:00:001.98
142562022-09-14 00:00:003.96
164562022-12-17 00:00:005.94
216562023-08-07 00:00:000.99
337562025-01-28 00:00:001.98
348562025-03-10 00:00:0013.86
403562025-11-08 00:00:008.91
22572021-04-04 00:00:001.98
33572021-05-15 00:00:0013.86
88572022-01-13 00:00:0017.91
217572023-08-20 00:00:001.98
240572023-11-22 00:00:003.96
262572024-02-24 00:00:005.94
314572024-10-14 00:00:000.99
120582022-06-12 00:00:001.98
131582022-07-23 00:00:0013.86
186582023-03-23 00:00:008.91
315582024-10-27 00:00:001.98
338582025-01-29 00:00:003.96
360582025-05-03 00:00:005.94
412582025-12-22 00:00:001.99
23592021-04-05 00:00:003.96
45592021-07-08 00:00:005.94
97592022-02-26 00:00:001.99
218592023-08-20 00:00:001.98
229592023-09-30 00:00:0013.86
284592024-05-30 00:00:008.91

All 412 invoices across 59 customers. Every row is retained by the detail-preserving window example.

GROUP BY output

Find total spending per customer.

SQLite 3+ · Chinook
SELECT CustomerId, ROUND(SUM(Total), 2) AS CustomerTotal
FROM Invoice GROUP BY CustomerId ORDER BY CustomerId;
Show all 59 recordsShow first 10 records
GROUP BY output: expected result · Showing 10 of 59 records for clarity59 records
CustomerIdCustomerTotal
139.62
237.62
339.62
439.62
540.62
649.62
742.62
837.62
937.62
1037.62
1137.62
1237.62
1337.62
1437.62
1538.62
1637.62
1739.62
1837.62
1938.62
2039.62
2137.62
2239.62
2337.62
2443.62
2542.62
2647.62
2737.62
2843.62
2937.62
3037.62
3137.62
3237.62
3337.62
3439.62
3537.62
3637.62
3743.62
3837.62
3938.62
4038.62
4137.62
4239.62
4340.62
4441.62
4545.62
4645.62
4737.62
4840.62
4937.62
5037.62
5138.62
5237.62
5337.62
5437.62
5537.62
5637.62
5746.62
5838.62
5936.64

412 invoice rows become 59 customer summary rows. Every customer appears because all 59 have invoices.

Window output

Show every invoice alongside its customer’s total.

SQLite 3.25.0+ · Chinook
SELECT InvoiceId, CustomerId, Total,
 ROUND(SUM(Total) OVER (PARTITION BY CustomerId), 2) AS CustomerTotal
FROM Invoice
ORDER BY CustomerId, InvoiceDate, InvoiceId;
Show all 412 recordsShow first 10 records
Window output: expected result · Showing 10 of 412 records for clarity412 records
InvoiceIdCustomerIdTotalCustomerTotal
9813.9839.62
12113.9639.62
14315.9439.62
19510.9939.62
31611.9839.62
327113.8639.62
38218.9139.62
121.9837.62
12213.8637.62
6728.9137.62
19621.9837.62
21923.9637.62
24125.9437.62
29320.9937.62
9933.9839.62
110313.8639.62
16538.9139.62
29431.9839.62
31733.9639.62
33935.9439.62
39130.9939.62
243.9639.62
2445.9439.62
7640.9939.62
19741.9839.62
208415.8639.62
26348.9139.62
39241.9839.62
7751.9840.62
10053.9640.62
12255.9440.62
17450.9940.62
29551.9840.62
306516.8640.62
36158.9140.62
4668.9149.62
17561.9849.62
19863.9649.62
22065.9449.62
27260.9949.62
39361.9849.62
404625.8649.62
7871.9842.62
89718.8642.62
14478.9142.62
27371.9842.62
29673.9642.62
31875.9442.62
37070.9942.62
385.9437.62
5580.9937.62
17681.9837.62
187813.8637.62
24288.9137.62
37181.9837.62
39483.9637.62
5691.9837.62
7993.9637.62
10195.9437.62
15390.9937.62
27491.9837.62
285913.8637.62
34098.9137.62
25108.9137.62
154101.9837.62
177103.9637.62
199105.9437.62
251100.9937.62
372101.9837.62
3831013.8637.62
57111.9837.62
681113.8637.62
123118.9137.62
252111.9837.62
275113.9637.62
297115.9437.62
349110.9937.62
34120.9937.62
155121.9837.62
1661213.8637.62
221128.9137.62
350121.9837.62
373123.9637.62
395125.9437.62
35131.9837.62
58133.9637.62
80135.9437.62
132130.9937.62
253131.9837.62
2641313.8637.62
319138.9137.62
4148.9137.62
133141.9837.62
156143.9637.62
178145.9437.62
230140.9937.62
351141.9837.62
3621413.8637.62
36151.9838.62
471513.8638.62
102159.9138.62
231151.9838.62
254153.9638.62
276155.9438.62
328150.9938.62
13160.9937.62
134161.9837.62
1451613.8637.62
200168.9137.62
329161.9837.62
352163.9637.62
374165.9437.62
14171.9839.62
37173.9639.62
59175.9439.62
111170.9939.62
232171.9839.62
2431713.8639.62
2981710.9139.62
112181.9837.62
135183.9637.62
157185.9437.62
209180.9937.62
330181.9837.62
3411813.8637.62
396188.9137.62
15191.9838.62
261913.8638.62
81198.9138.62
210191.9838.62
233193.9638.62
255195.9438.62
307191.9938.62
113201.9839.62
1242013.8639.62
179208.9139.62
308203.9839.62
331203.9639.62
353205.9439.62
405200.9939.62
16213.9637.62
38215.9437.62
90210.9937.62
211211.9837.62
2222113.8637.62
277218.9137.62
406211.9837.62
91221.9839.62
114223.9639.62
136225.9439.62
188220.9939.62
309223.9839.62
3202213.8639.62
375228.9139.62
52313.8637.62
60238.9137.62
189231.9837.62
212233.9637.62
234235.9437.62
286230.9937.62
407231.9837.62
92241.9843.62
1032415.8643.62
158248.9143.62
287241.9843.62
310247.9643.62
332245.9443.62
384240.9943.62
17255.9442.62
69250.9942.62
190251.9842.62
2012518.8642.62
256258.9142.62
385251.9842.62
408253.9642.62
70261.9847.62
93263.9647.62
115265.9447.62
167260.9947.62
288261.9847.62
2992623.8647.62
354268.9147.62
39278.9137.62
168271.9837.62
191273.9637.62
213275.9437.62
265270.9937.62
386271.9837.62
3972713.8637.62
71281.9843.62
822813.8643.62
137288.9143.62
266281.9843.62
289283.9643.62
3112811.9443.62
363280.9943.62
48290.9937.62
169291.9837.62
1802913.8637.62
235298.9137.62
364291.9837.62
387293.9637.62
409295.9437.62
49301.9837.62
72303.9637.62
94305.9437.62
146300.9937.62
267301.9837.62
2783013.8637.62
333308.9137.62
18318.9137.62
147311.9837.62
170313.9637.62
192315.9437.62
244310.9937.62
365311.9837.62
3763113.8637.62
50321.9837.62
613213.8637.62
116328.9137.62
245321.9837.62
268323.9637.62
290325.9437.62
342320.9937.62
27330.9937.62
148331.9837.62
1593313.8637.62
214338.9137.62
343331.9837.62
366333.9637.62
388335.9437.62
28341.9839.62
51343.9639.62
73345.9439.62
125340.9939.62
246341.9839.62
2573413.8639.62
3123410.9139.62
126351.9837.62
149353.9637.62
171355.9437.62
223350.9937.62
344351.9837.62
3553513.8637.62
410358.9137.62
29361.9837.62
403613.8637.62
95368.9137.62
224361.9837.62
247363.9637.62
269365.9437.62
321360.9937.62
6370.9943.62
127371.9843.62
1383713.8643.62
1933714.9143.62
322371.9843.62
345373.9643.62
367375.9443.62
7381.9837.62
30383.9637.62
52385.9437.62
104380.9937.62
225381.9837.62
2363813.8637.62
291388.9137.62
105391.9838.62
128393.9638.62
150395.9438.62
202391.9938.62
323391.9838.62
3343913.8638.62
389398.9138.62
8401.9838.62
194013.8638.62
74408.9138.62
203402.9838.62
226403.9638.62
248405.9438.62
300400.9938.62
106411.9837.62
1174113.8637.62
172418.9137.62
301411.9837.62
324413.9637.62
346415.9437.62
398410.9937.62
9423.9639.62
31425.9439.62
83420.9939.62
204423.9839.62
2154213.8639.62
270428.9139.62
399421.9839.62
84431.9840.62
107433.9640.62
129435.9440.62
181430.9940.62
302431.9840.62
3134316.8640.62
368438.9140.62
53448.9141.62
182441.9841.62
205447.9641.62
227445.9441.62
279440.9941.62
400441.9841.62
4114413.8641.62
85451.9845.62
964521.8645.62
151458.9145.62
280451.9845.62
303453.9645.62
325455.9445.62
377450.9945.62
10465.9445.62
62460.9945.62
183461.9845.62
1944621.8645.62
249468.9145.62
378461.9845.62
401463.9645.62
63471.9837.62
86473.9637.62
108475.9437.62
160470.9937.62
281471.9837.62
2924713.8637.62
347478.9137.62
32488.9140.62
161481.9840.62
184483.9640.62
206488.9440.62
258480.9940.62
379481.9840.62
3904813.8640.62
64491.9837.62
754913.8637.62
130498.9137.62
259491.9837.62
282493.9637.62
304495.9437.62
356490.9937.62
41500.9937.62
162501.9837.62
1735013.8637.62
228508.9137.62
357501.9837.62
380503.9637.62
402505.9437.62
42511.9838.62
65513.9638.62
87516.9438.62
139510.9938.62
260511.9838.62
2715113.8638.62
326518.9138.62
11528.9137.62
140521.9837.62
163523.9637.62
185525.9437.62
237520.9937.62
358521.9837.62
3695213.8637.62
43531.9837.62
545313.8637.62
109538.9137.62
238531.9837.62
261533.9637.62
283535.9437.62
335530.9937.62
20540.9937.62
141541.9837.62
1525413.8637.62
207548.9137.62
336541.9837.62
359543.9637.62
381545.9437.62
21551.9837.62
44553.9637.62
66555.9437.62
118550.9937.62
239551.9837.62
2505513.8637.62
305558.9137.62
119561.9837.62
142563.9637.62
164565.9437.62
216560.9937.62
337561.9837.62
3485613.8637.62
403568.9137.62
22571.9846.62
335713.8646.62
885717.9146.62
217571.9846.62
240573.9646.62
262575.9446.62
314570.9946.62
120581.9838.62
1315813.8638.62
186588.9138.62
315581.9838.62
338583.9638.62
360585.9438.62
412581.9938.62
23593.9636.64
45595.9436.64
97591.9936.64
218591.9836.64
2295913.8636.64
284598.9136.64

All 412 rows remain. Each customer total repeats on that customer’s invoice rows. Summing CustomerTotal again would count the same spending multiple times.

All three panels use the same 412 invoices. Highlight customer 1 to compare their seven source invoices, one summary row and seven annotated window rows. Do not sum a repeated customer-total column as if it were an invoice amount.

Windows see rows surviving the current WHERE and grouping. Filter to one customer first and an unpartitioned total means that customer’s total. Group first and the window sees grouped rows; it does not reach back into every raw table row.

OVER() and PARTITION BY define the population

SUM(Total) OVER () uses one partition containing the current result. It is useful for shares of a grand total. PARTITION BY CustomerId divides input into customer-specific sets, restarting the calculation for each customer. Partitioning removes no rows.

OVER() without a partition

What fraction of total revenue is each invoice?

SQLite 3.25.0+ · Chinook
SELECT InvoiceId, Total,
 ROUND(100.0 * Total / SUM(Total) OVER (), 2) AS PercentOfRevenue
FROM Invoice ORDER BY InvoiceId;
Show all 412 recordsShow first 10 records
OVER() without a partition: expected result · Showing 10 of 412 records for clarity412 records
InvoiceIdTotalPercentOfRevenue
11.980.09
23.960.17
35.940.26
48.910.38
513.860.6
60.990.04
71.980.09
81.980.09
93.960.17
105.940.26
118.910.38
1213.860.6
130.990.04
141.980.09
151.980.09
163.960.17
175.940.26
188.910.38
1913.860.6
200.990.04
211.980.09
221.980.09
233.960.17
245.940.26
258.910.38
2613.860.6
270.990.04
281.980.09
291.980.09
303.960.17
315.940.26
328.910.38
3313.860.6
340.990.04
351.980.09
361.980.09
373.960.17
385.940.26
398.910.38
4013.860.6
410.990.04
421.980.09
431.980.09
443.960.17
455.940.26
468.910.38
4713.860.6
480.990.04
491.980.09
501.980.09
513.960.17
525.940.26
538.910.38
5413.860.6
550.990.04
561.980.09
571.980.09
583.960.17
595.940.26
608.910.38
6113.860.6
620.990.04
631.980.09
641.980.09
653.960.17
665.940.26
678.910.38
6813.860.6
690.990.04
701.980.09
711.980.09
723.960.17
735.940.26
748.910.38
7513.860.6
760.990.04
771.980.09
781.980.09
793.960.17
805.940.26
818.910.38
8213.860.6
830.990.04
841.980.09
851.980.09
863.960.17
876.940.3
8817.910.77
8918.860.81
900.990.04
911.980.09
921.980.09
933.960.17
945.940.26
958.910.38
9621.860.94
971.990.09
983.980.17
993.980.17
1003.960.17
1015.940.26
1029.910.43
10315.860.68
1040.990.04
1051.980.09
1061.980.09
1073.960.17
1085.940.26
1098.910.38
11013.860.6
1110.990.04
1121.980.09
1131.980.09
1143.960.17
1155.940.26
1168.910.38
11713.860.6
1180.990.04
1191.980.09
1201.980.09
1213.960.17
1225.940.26
1238.910.38
12413.860.6
1250.990.04
1261.980.09
1271.980.09
1283.960.17
1295.940.26
1308.910.38
13113.860.6
1320.990.04
1331.980.09
1341.980.09
1353.960.17
1365.940.26
1378.910.38
13813.860.6
1390.990.04
1401.980.09
1411.980.09
1423.960.17
1435.940.26
1448.910.38
14513.860.6
1460.990.04
1471.980.09
1481.980.09
1493.960.17
1505.940.26
1518.910.38
15213.860.6
1530.990.04
1541.980.09
1551.980.09
1563.960.17
1575.940.26
1588.910.38
15913.860.6
1600.990.04
1611.980.09
1621.980.09
1633.960.17
1645.940.26
1658.910.38
16613.860.6
1670.990.04
1681.980.09
1691.980.09
1703.960.17
1715.940.26
1728.910.38
17313.860.6
1740.990.04
1751.980.09
1761.980.09
1773.960.17
1785.940.26
1798.910.38
18013.860.6
1810.990.04
1821.980.09
1831.980.09
1843.960.17
1855.940.26
1868.910.38
18713.860.6
1880.990.04
1891.980.09
1901.980.09
1913.960.17
1925.940.26
19314.910.64
19421.860.94
1950.990.04
1961.980.09
1971.980.09
1983.960.17
1995.940.26
2008.910.38
20118.860.81
2021.990.09
2032.980.13
2043.980.17
2057.960.34
2068.940.38
2078.910.38
20815.860.68
2090.990.04
2101.980.09
2111.980.09
2123.960.17
2135.940.26
2148.910.38
21513.860.6
2160.990.04
2171.980.09
2181.980.09
2193.960.17
2205.940.26
2218.910.38
22213.860.6
2230.990.04
2241.980.09
2251.980.09
2263.960.17
2275.940.26
2288.910.38
22913.860.6
2300.990.04
2311.980.09
2321.980.09
2333.960.17
2345.940.26
2358.910.38
23613.860.6
2370.990.04
2381.980.09
2391.980.09
2403.960.17
2415.940.26
2428.910.38
24313.860.6
2440.990.04
2451.980.09
2461.980.09
2473.960.17
2485.940.26
2498.910.38
25013.860.6
2510.990.04
2521.980.09
2531.980.09
2543.960.17
2555.940.26
2568.910.38
25713.860.6
2580.990.04
2591.980.09
2601.980.09
2613.960.17
2625.940.26
2638.910.38
26413.860.6
2650.990.04
2661.980.09
2671.980.09
2683.960.17
2695.940.26
2708.910.38
27113.860.6
2720.990.04
2731.980.09
2741.980.09
2753.960.17
2765.940.26
2778.910.38
27813.860.6
2790.990.04
2801.980.09
2811.980.09
2823.960.17
2835.940.26
2848.910.38
28513.860.6
2860.990.04
2871.980.09
2881.980.09
2893.960.17
2905.940.26
2918.910.38
29213.860.6
2930.990.04
2941.980.09
2951.980.09
2963.960.17
2975.940.26
29810.910.47
29923.861.02
3000.990.04
3011.980.09
3021.980.09
3033.960.17
3045.940.26
3058.910.38
30616.860.72
3071.990.09
3083.980.17
3093.980.17
3107.960.34
31111.940.51
31210.910.47
31316.860.72
3140.990.04
3151.980.09
3161.980.09
3173.960.17
3185.940.26
3198.910.38
32013.860.6
3210.990.04
3221.980.09
3231.980.09
3243.960.17
3255.940.26
3268.910.38
32713.860.6
3280.990.04
3291.980.09
3301.980.09
3313.960.17
3325.940.26
3338.910.38
33413.860.6
3350.990.04
3361.980.09
3371.980.09
3383.960.17
3395.940.26
3408.910.38
34113.860.6
3420.990.04
3431.980.09
3441.980.09
3453.960.17
3465.940.26
3478.910.38
34813.860.6
3490.990.04
3501.980.09
3511.980.09
3523.960.17
3535.940.26
3548.910.38
35513.860.6
3560.990.04
3571.980.09
3581.980.09
3593.960.17
3605.940.26
3618.910.38
36213.860.6
3630.990.04
3641.980.09
3651.980.09
3663.960.17
3675.940.26
3688.910.38
36913.860.6
3700.990.04
3711.980.09
3721.980.09
3733.960.17
3745.940.26
3758.910.38
37613.860.6
3770.990.04
3781.980.09
3791.980.09
3803.960.17
3815.940.26
3828.910.38
38313.860.6
3840.990.04
3851.980.09
3861.980.09
3873.960.17
3885.940.26
3898.910.38
39013.860.6
3910.990.04
3921.980.09
3931.980.09
3943.960.17
3955.940.26
3968.910.38
39713.860.6
3980.990.04
3991.980.09
4001.980.09
4013.960.17
4025.940.26
4038.910.38
40425.861.11
4050.990.04
4061.980.09
4071.980.09
4083.960.17
4095.940.26
4108.910.38
41113.860.6
4121.990.09

Empty OVER() treats all 412 rows as one partition. 100.0 avoids integer division on engines where integer / integer truncates. Rounded percentages need not sum to exactly 100.

Window output

Show every invoice alongside its customer’s total.

SQLite 3.25.0+ · Chinook
SELECT InvoiceId, CustomerId, Total,
 ROUND(SUM(Total) OVER (PARTITION BY CustomerId), 2) AS CustomerTotal
FROM Invoice
ORDER BY CustomerId, InvoiceDate, InvoiceId;
Expected results (412 rows)
Show all 412 recordsShow first 10 records
Window output: expected result · Showing 10 of 412 records for clarity412 records
InvoiceIdCustomerIdTotalCustomerTotal
9813.9839.62
12113.9639.62
14315.9439.62
19510.9939.62
31611.9839.62
327113.8639.62
38218.9139.62
121.9837.62
12213.8637.62
6728.9137.62
19621.9837.62
21923.9637.62
24125.9437.62
29320.9937.62
9933.9839.62
110313.8639.62
16538.9139.62
29431.9839.62
31733.9639.62
33935.9439.62
39130.9939.62
243.9639.62
2445.9439.62
7640.9939.62
19741.9839.62
208415.8639.62
26348.9139.62
39241.9839.62
7751.9840.62
10053.9640.62
12255.9440.62
17450.9940.62
29551.9840.62
306516.8640.62
36158.9140.62
4668.9149.62
17561.9849.62
19863.9649.62
22065.9449.62
27260.9949.62
39361.9849.62
404625.8649.62
7871.9842.62
89718.8642.62
14478.9142.62
27371.9842.62
29673.9642.62
31875.9442.62
37070.9942.62
385.9437.62
5580.9937.62
17681.9837.62
187813.8637.62
24288.9137.62
37181.9837.62
39483.9637.62
5691.9837.62
7993.9637.62
10195.9437.62
15390.9937.62
27491.9837.62
285913.8637.62
34098.9137.62
25108.9137.62
154101.9837.62
177103.9637.62
199105.9437.62
251100.9937.62
372101.9837.62
3831013.8637.62
57111.9837.62
681113.8637.62
123118.9137.62
252111.9837.62
275113.9637.62
297115.9437.62
349110.9937.62
34120.9937.62
155121.9837.62
1661213.8637.62
221128.9137.62
350121.9837.62
373123.9637.62
395125.9437.62
35131.9837.62
58133.9637.62
80135.9437.62
132130.9937.62
253131.9837.62
2641313.8637.62
319138.9137.62
4148.9137.62
133141.9837.62
156143.9637.62
178145.9437.62
230140.9937.62
351141.9837.62
3621413.8637.62
36151.9838.62
471513.8638.62
102159.9138.62
231151.9838.62
254153.9638.62
276155.9438.62
328150.9938.62
13160.9937.62
134161.9837.62
1451613.8637.62
200168.9137.62
329161.9837.62
352163.9637.62
374165.9437.62
14171.9839.62
37173.9639.62
59175.9439.62
111170.9939.62
232171.9839.62
2431713.8639.62
2981710.9139.62
112181.9837.62
135183.9637.62
157185.9437.62
209180.9937.62
330181.9837.62
3411813.8637.62
396188.9137.62
15191.9838.62
261913.8638.62
81198.9138.62
210191.9838.62
233193.9638.62
255195.9438.62
307191.9938.62
113201.9839.62
1242013.8639.62
179208.9139.62
308203.9839.62
331203.9639.62
353205.9439.62
405200.9939.62
16213.9637.62
38215.9437.62
90210.9937.62
211211.9837.62
2222113.8637.62
277218.9137.62
406211.9837.62
91221.9839.62
114223.9639.62
136225.9439.62
188220.9939.62
309223.9839.62
3202213.8639.62
375228.9139.62
52313.8637.62
60238.9137.62
189231.9837.62
212233.9637.62
234235.9437.62
286230.9937.62
407231.9837.62
92241.9843.62
1032415.8643.62
158248.9143.62
287241.9843.62
310247.9643.62
332245.9443.62
384240.9943.62
17255.9442.62
69250.9942.62
190251.9842.62
2012518.8642.62
256258.9142.62
385251.9842.62
408253.9642.62
70261.9847.62
93263.9647.62
115265.9447.62
167260.9947.62
288261.9847.62
2992623.8647.62
354268.9147.62
39278.9137.62
168271.9837.62
191273.9637.62
213275.9437.62
265270.9937.62
386271.9837.62
3972713.8637.62
71281.9843.62
822813.8643.62
137288.9143.62
266281.9843.62
289283.9643.62
3112811.9443.62
363280.9943.62
48290.9937.62
169291.9837.62
1802913.8637.62
235298.9137.62
364291.9837.62
387293.9637.62
409295.9437.62
49301.9837.62
72303.9637.62
94305.9437.62
146300.9937.62
267301.9837.62
2783013.8637.62
333308.9137.62
18318.9137.62
147311.9837.62
170313.9637.62
192315.9437.62
244310.9937.62
365311.9837.62
3763113.8637.62
50321.9837.62
613213.8637.62
116328.9137.62
245321.9837.62
268323.9637.62
290325.9437.62
342320.9937.62
27330.9937.62
148331.9837.62
1593313.8637.62
214338.9137.62
343331.9837.62
366333.9637.62
388335.9437.62
28341.9839.62
51343.9639.62
73345.9439.62
125340.9939.62
246341.9839.62
2573413.8639.62
3123410.9139.62
126351.9837.62
149353.9637.62
171355.9437.62
223350.9937.62
344351.9837.62
3553513.8637.62
410358.9137.62
29361.9837.62
403613.8637.62
95368.9137.62
224361.9837.62
247363.9637.62
269365.9437.62
321360.9937.62
6370.9943.62
127371.9843.62
1383713.8643.62
1933714.9143.62
322371.9843.62
345373.9643.62
367375.9443.62
7381.9837.62
30383.9637.62
52385.9437.62
104380.9937.62
225381.9837.62
2363813.8637.62
291388.9137.62
105391.9838.62
128393.9638.62
150395.9438.62
202391.9938.62
323391.9838.62
3343913.8638.62
389398.9138.62
8401.9838.62
194013.8638.62
74408.9138.62
203402.9838.62
226403.9638.62
248405.9438.62
300400.9938.62
106411.9837.62
1174113.8637.62
172418.9137.62
301411.9837.62
324413.9637.62
346415.9437.62
398410.9937.62
9423.9639.62
31425.9439.62
83420.9939.62
204423.9839.62
2154213.8639.62
270428.9139.62
399421.9839.62
84431.9840.62
107433.9640.62
129435.9440.62
181430.9940.62
302431.9840.62
3134316.8640.62
368438.9140.62
53448.9141.62
182441.9841.62
205447.9641.62
227445.9441.62
279440.9941.62
400441.9841.62
4114413.8641.62
85451.9845.62
964521.8645.62
151458.9145.62
280451.9845.62
303453.9645.62
325455.9445.62
377450.9945.62
10465.9445.62
62460.9945.62
183461.9845.62
1944621.8645.62
249468.9145.62
378461.9845.62
401463.9645.62
63471.9837.62
86473.9637.62
108475.9437.62
160470.9937.62
281471.9837.62
2924713.8637.62
347478.9137.62
32488.9140.62
161481.9840.62
184483.9640.62
206488.9440.62
258480.9940.62
379481.9840.62
3904813.8640.62
64491.9837.62
754913.8637.62
130498.9137.62
259491.9837.62
282493.9637.62
304495.9437.62
356490.9937.62
41500.9937.62
162501.9837.62
1735013.8637.62
228508.9137.62
357501.9837.62
380503.9637.62
402505.9437.62
42511.9838.62
65513.9638.62
87516.9438.62
139510.9938.62
260511.9838.62
2715113.8638.62
326518.9138.62
11528.9137.62
140521.9837.62
163523.9637.62
185525.9437.62
237520.9937.62
358521.9837.62
3695213.8637.62
43531.9837.62
545313.8637.62
109538.9137.62
238531.9837.62
261533.9637.62
283535.9437.62
335530.9937.62
20540.9937.62
141541.9837.62
1525413.8637.62
207548.9137.62
336541.9837.62
359543.9637.62
381545.9437.62
21551.9837.62
44553.9637.62
66555.9437.62
118550.9937.62
239551.9837.62
2505513.8637.62
305558.9137.62
119561.9837.62
142563.9637.62
164565.9437.62
216560.9937.62
337561.9837.62
3485613.8637.62
403568.9137.62
22571.9846.62
335713.8646.62
885717.9146.62
217571.9846.62
240573.9646.62
262575.9446.62
314570.9946.62
120581.9838.62
1315813.8638.62
186588.9138.62
315581.9838.62
338583.9638.62
360585.9438.62
412581.9938.62
23593.9636.64
45595.9436.64
97591.9936.64
218591.9836.64
2295913.8636.64
284598.9136.64

All 412 rows remain. Each customer total repeats on that customer’s invoice rows. Summing CustomerTotal again would count the same spending multiple times.

Independent customer partitions

  1. 1Customer 1: 7 invoices → total 39.62
  2. 2Customer 2: 7 invoices → total 37.62
  3. 3Customer 3: 7 invoices → total 39.62
Three of the 59 customer partitions are shown here. Every customer has invoice input rows; each receives their own total.

Partition keys can be composite, such as customer and year. Adding a partition changes the denominator, not just formatting. If you need one row per customer, GROUP BY remains simpler.

Calculation order and display order differ

ORDER BY inside OVER defines the sequence for ranking, neighboring-row functions and ordered aggregates. The final query ORDER BY controls displayed rows. Neither guarantees the other. Request a final order when reports or applications rely on it.

Our chronology uses InvoiceDate, InvoiceId. The date is the business order; unique InvoiceId resolves equal-date ties. InvoiceId alone is deterministic but not necessarily chronological. Decide what timestamp and tie policy “latest” means for your report.

Conceptual processing order

  1. 1FROM / JOIN → WHERE → GROUP BY / HAVING
  2. 2Window calculations over surviving rows
  3. 3Outer filtering if needed → final ORDER BY
Logical processing is not the physical execution plan. Use a CTE or derived table to filter a computed rank at a new query level.

ROW_NUMBER, RANK and DENSE_RANK disagree on ties

ROW_NUMBER assigns consecutive positions; make its ordering unique for deterministic ties. RANK gives equal values the same position and leaves gaps. DENSE_RANK shares positions without gaps. Choose the business rule: exactly three rows, three competition places, or three distinct values?

The synthetic VALUES set deliberately has a tie followed by a lower amount. Switch to the frame view to see how the same peers affect aggregates.

A tied dataset: ranking and frame views

Ranking with ties

Should tied sales share a position?

SQLite 3.25.0+ · Synthetic VALUES
WITH sales(SaleId, Team, Amount) AS (
 VALUES (1, 'A', 10), (2, 'A', 10), (3, 'A', 20),
 (4, 'B', 5), (5, 'B', 15), (6, 'A', 5)
)
SELECT SaleId, Team, Amount,
 ROW_NUMBER() OVER (PARTITION BY Team ORDER BY Amount DESC, SaleId) AS RowNumber,
 RANK() OVER (PARTITION BY Team ORDER BY Amount DESC) AS RankWithGaps,
 DENSE_RANK() OVER (PARTITION BY Team ORDER BY Amount DESC) AS DenseRank
FROM sales ORDER BY Team, Amount DESC, SaleId;
Ranking with ties: expected result · 6 records
SaleIdTeamAmountRowNumberRankWithGapsDenseRank
3A20111
1A10222
2A10322
6A5443
5B15111
4B5222

Team A amounts 20, 10, 10, 5 receive row numbers 1,2,3,4; ranks 1,2,2,4; dense ranks 1,2,2,3. SaleId breaks ROW_NUMBER ties only: adding it to RANK would remove the tie.

ROWS versus RANGE

Why does a running total jump across peers?

SQLite 3.25.0+ · Synthetic VALUES
WITH sales(SaleId, Team, Amount) AS (
 VALUES (1, 'A', 10), (2, 'A', 10), (3, 'A', 20),
 (4, 'B', 5), (5, 'B', 15), (6, 'A', 5)
)
SELECT SaleId, Team, Amount,
 SUM(Amount) OVER (PARTITION BY Team ORDER BY Amount) AS DefaultRange,
 SUM(Amount) OVER (PARTITION BY Team ORDER BY Amount
 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS ExplicitRange,
 SUM(Amount) OVER (PARTITION BY Team ORDER BY Amount, SaleId
 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS ExplicitRows
FROM sales ORDER BY Team, Amount, SaleId;
ROWS versus RANGE: expected result · 6 records
SaleIdTeamAmountDefaultRangeExplicitRangeExplicitRows
6A5555
1A10252515
2A10252525
3A20454545
4B5555
5B15202020

The team-A sales of 10 both include the preceding 5 and both 10s: RANGE returns 25. ROWS steps through 15 then 25. DefaultRange equals ExplicitRange in SQLite.

Adding SaleId to RANK’s ordering makes every tuple unique and destroys the intended amount tie. Here it belongs only in ROW_NUMBER’s ordering. Do not add a unique key mechanically to every ranking expression.

Make the running-total frame explicit

A running total answers “how much has accumulated through this invoice?” The frame starts at the first row and ends at the current one. ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW makes this visible. SQLite’s default cumulative RANGE frame also gives this running total with the unique (InvoiceDate, InvoiceId) ordering. Explicit ROWS documents the intended row-by-row behavior, including how ties would be handled if the ordering changed.

Running revenue

How does customer 1’s revenue accumulate?

SQLite 3.25.0+ · Chinook
SELECT InvoiceId, InvoiceDate, Total,
 ROUND(SUM(Total) OVER (
 ORDER BY InvoiceDate, InvoiceId
 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
 ), 2) AS RunningTotal
FROM Invoice WHERE CustomerId = 1
ORDER BY InvoiceDate, InvoiceId;
0204098121143195316327382
Invoice ID in chronological order (even spacing, not elapsed time). Amount in dataset units. ━ Total ┄ RunningTotal

Running revenue: expected result · 7 records
InvoiceIdInvoiceDateTotalRunningTotal
982022-03-11 00:00:003.983.98
1212022-06-13 00:00:003.967.94
1432022-09-15 00:00:005.9413.88
1952023-05-06 00:00:000.9914.87
3162024-10-27 00:00:001.9816.85
3272024-12-07 00:00:0013.8630.71
3822025-08-07 00:00:008.9139.62

WHERE selects seven invoices before the window runs. The final total is 39.62. The uniformly formatted YYYY-MM-DD HH:MM:SS dates determine chronology; InvoiceId breaks any date ties.

Invoice amounts rise and fall; cumulative revenue only rises here because amounts are positive. Refunds could make it fall. The chart uses equally spaced invoices: a long calendar gap occupies no extra width. All exact values remain in the table.

Moving averages smooth a row sequence

A frame slides with each row

  1. 1Invoice 1: average row 1 only
  2. 2Invoice 2: average rows 1–2
  3. 3Later: current row and 2 preceding rows
ROWS counts records, not days. Early rows use a shorter available frame; no zeroes are inserted.

Three-invoice moving average

Smooth customer 1’s invoice amounts over three consecutive invoices.

SQLite 3.25.0+ · Chinook
SELECT InvoiceId, InvoiceDate, Total,
 ROUND(AVG(Total) OVER (
 ORDER BY InvoiceDate, InvoiceId
 ROWS BETWEEN 2 PRECEDING AND CURRENT ROW
 ), 2) AS MovingAverage
FROM Invoice WHERE CustomerId = 1
ORDER BY InvoiceDate, InvoiceId;
07.51598121143195316327382
Invoice ID in chronological order (even spacing, not elapsed time). Amount in dataset units. ━ Total ┄ MovingAverage

Three-invoice moving average: expected result · 7 records
InvoiceIdInvoiceDateTotalMovingAverage
982022-03-11 00:00:003.983.98
1212022-06-13 00:00:003.963.97
1432022-09-15 00:00:005.944.63
1952023-05-06 00:00:000.993.63
3162024-10-27 00:00:001.982.97
3272024-12-07 00:00:0013.865.61
3822025-08-07 00:00:008.918.25

The first frame has one row, the second two, later frames three. This is a three-invoice average, not a three-day average: dates are irregular.

Use the series controls to compare the smoothed trend with individual invoices. An invoice affects its own frame and the next two frames, where those rows exist; it then leaves the moving average. A three-day average needs date-aware logic and a missing-day policy; a ROWS frame cannot express that by changing its number alone.

AVG ignores NULL amounts: a three-row frame can average fewer than three values. Our totals are non-null, but check another schema. To require exactly three observations, calculate a frame count and filter at a later query level.

LAG and LEAD compare neighbors

LAG and LEAD

Compare each invoice with its previous and following invoices.

SQLite 3.25.0+ · Chinook
SELECT InvoiceId, Total,
 LAG(Total) OVER (ORDER BY InvoiceDate, InvoiceId) AS PreviousTotal,
 LEAD(Total) OVER (ORDER BY InvoiceDate, InvoiceId) AS NextTotal,
 ROUND(Total - LAG(Total) OVER (
 ORDER BY InvoiceDate, InvoiceId), 2) AS ChangeFromPrevious
FROM Invoice WHERE CustomerId = 1
ORDER BY InvoiceDate, InvoiceId;
LAG and LEAD: expected result · 7 records
InvoiceIdTotalPreviousTotalNextTotalChangeFromPrevious
983.98NULL3.96NULL
1213.963.985.94-0.02
1435.943.960.991.98
1950.995.941.98-4.95
3161.980.9913.860.99
32713.861.988.9111.88
3828.9113.86NULL-4.95

The first previous and last next values are NULL: no neighbor, not zero sales. LAG/LEAD address ordered rows; changing an aggregate frame does not change those offsets.

LAG reads a previous ordered row; LEAD reads a following row. Use them for invoice changes, status transitions or event gaps. “Previous” means the previous observed row, not yesterday. Partition by customer if neighbors must belong to the same customer; here we filter to customer 1 first.

A missing neighbor is NULL. Replacing it with zero makes the first invoice appear to increase from zero, which could be a useful baseline or misleading. Treat that as a reporting decision.

ROWS, RANGE and default peers

A partition is the whole calculation group; a frame is a moving subset. For ordered aggregate windows, SQLite defaults to RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, including peers. With no ORDER BY, all rows are peers and the frame covers the partition. State the frame when reviewing cumulative logic.

ROWS versus RANGE

Why does a running total jump across peers?

SQLite 3.25.0+ · Synthetic VALUES
WITH sales(SaleId, Team, Amount) AS (
 VALUES (1, 'A', 10), (2, 'A', 10), (3, 'A', 20),
 (4, 'B', 5), (5, 'B', 15), (6, 'A', 5)
)
SELECT SaleId, Team, Amount,
 SUM(Amount) OVER (PARTITION BY Team ORDER BY Amount) AS DefaultRange,
 SUM(Amount) OVER (PARTITION BY Team ORDER BY Amount
 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS ExplicitRange,
 SUM(Amount) OVER (PARTITION BY Team ORDER BY Amount, SaleId
 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS ExplicitRows
FROM sales ORDER BY Team, Amount, SaleId;
ROWS versus RANGE: expected result · 6 records
SaleIdTeamAmountDefaultRangeExplicitRangeExplicitRows
6A5555
1A10252515
2A10252525
3A20454545
4B5555
5B15202020

The team-A sales of 10 both include the preceding 5 and both 10s: RANGE returns 25. ROWS steps through 15 then 25. DefaultRange equals ExplicitRange in SQLite.

ROWS counts positions. RANGE includes equal ordering values as peers at CURRENT ROW, giving tied sales the same cumulative value. Value-offset RANGE has extra restrictions: SQLite requires one ORDER BY expression, and offset/type behavior varies by engine. Here we use only unbounded RANGE.

Ranking and LAG/LEAD do not use aggregate frames like SUM/AVG. LAST_VALUE is frame-sensitive: a default frame ending at the current row often surprises people expecting the last value of the entire partition. Check each function’s documented behavior.

Top N per group: filter one level later

Top three per customer

Keep at most three largest invoices per customer.

SQLite 3.25.0+ · Chinook
WITH numbered AS (
 SELECT InvoiceId, CustomerId, Total,
 ROW_NUMBER() OVER (
 PARTITION BY CustomerId ORDER BY Total DESC, InvoiceId
 ) AS Position FROM Invoice
)
SELECT InvoiceId, CustomerId, Total, Position
FROM numbered WHERE Position <= 3
ORDER BY CustomerId, Position;
Show all 177 recordsShow first 10 records
Top three per customer: expected result · Showing 10 of 177 records for clarity177 records
InvoiceIdCustomerIdTotalPosition
327113.861
38218.912
14315.943
12213.861
6728.912
24125.943
110313.861
16538.912
33935.943
208415.861
26348.912
2445.943
306516.861
36158.912
12255.943
404625.861
4668.912
22065.943
89718.861
14478.912
31875.943
187813.861
24288.912
385.943
285913.861
34098.912
10195.943
3831013.861
25108.912
199105.943
681113.861
123118.912
297115.943
1661213.861
221128.912
395125.943
2641313.861
319138.912
80135.943
3621413.861
4148.912
178145.943
471513.861
102159.912
276155.943
1451613.861
200168.912
374165.943
2431713.861
2981710.912
59175.943
3411813.861
396188.912
157185.943
261913.861
81198.912
255195.943
1242013.861
179208.912
353205.943
2222113.861
277218.912
38215.943
3202213.861
375228.912
136225.943
52313.861
60238.912
234235.943
1032415.861
158248.912
310247.963
2012518.861
256258.912
17255.943
2992623.861
354268.912
115265.943
3972713.861
39278.912
213275.943
822813.861
3112811.942
137288.913
1802913.861
235298.912
409295.943
2783013.861
333308.912
94305.943
3763113.861
18318.912
192315.943
613213.861
116328.912
290325.943
1593313.861
214338.912
388335.943
2573413.861
3123410.912
73345.943
3553513.861
410358.912
171355.943
403613.861
95368.912
269365.943
1933714.911
1383713.862
367375.943
2363813.861
291388.912
52385.943
3343913.861
389398.912
150395.943
194013.861
74408.912
248405.943
1174113.861
172418.912
346415.943
2154213.861
270428.912
31425.943
3134316.861
368438.912
129435.943
4114413.861
53448.912
205447.963
964521.861
151458.912
325455.943
1944621.861
249468.912
10465.943
2924713.861
347478.912
108475.943
3904813.861
206488.942
32488.913
754913.861
130498.912
304495.943
1735013.861
228508.912
402505.943
2715113.861
326518.912
87516.943
3695213.861
11528.912
185525.943
545313.861
109538.912
283535.943
1525413.861
207548.912
381545.943
2505513.861
305558.912
66555.943
3485613.861
403568.912
164565.943
885717.911
335713.862
262575.943
1315813.861
186588.912
360585.943
2295913.861
284598.912
45595.943

A CTE computes positions before the outer WHERE filters them. RANK/DENSE_RANK can keep more than three invoices if ties are included; that is a different business rule.

The CTE gives Position a name before the outer WHERE filters it. SQLite cannot filter a window result in the same SELECT’s WHERE and does not support QUALIFY, available in some other engines. An outer query is a clear, widely usable pattern.

This query keeps at most three invoices per customer. For “all invoices with one of the three highest amounts”, DENSE_RANK is a different rule and may return more rows. For detail plus totals, windows can avoid joining a grouped summary back to detail. For a yes/no related-row test, EXISTS is usually clearer.

Correctness traps and performance

  • Missing ordering: a row number needs a meaningful sequence. An unordered window sum is a whole-partition total, not cumulative.
  • Unexpected ties: choose exact row counts versus rank bands first.
  • Wrong frame: default peers can make cumulative sums jump; three rows are not three days.
  • Wrong filtering level: filtering before a window changes its population. A computed rank requires outer filtering in SQLite.
  • Confused orderings: window ordering does not promise presentation ordering.
  • Portability: check frames, NULL ordering, QUALIFY and date arithmetic in your engine. These queries target SQLite 3.25+, where window functions were introduced.

Windows often require sorting on partition and order keys. Matching indexes may help but do not guarantee avoiding a sort. Several windows with different orders may require different work. Large partitions and wide intermediate rows increase memory or temporary-storage pressure. Inspect plans with representative data before declaring a window faster than a self-join or correlated subquery.

No production benchmark is inferred from this sample database. The maintainability benefit is directly expressing analytical intent; performance remains an engine-specific measurement.

Three exercises, three decisions

Use the unchanged Chinook database. State the population, partition, ordering, frame and tie policy before writing SQL.

Beginner · count without collapsing

Preserve every invoice and add its customer’s invoice count.

Hint

Use COUNT(*) with a customer partition. No ordering is needed for a whole-customer count.

Solution and checked result

Beginner solution

Preserve every invoice and add its customer’s invoice count.

SQLite 3.25.0+ · Chinook
SELECT InvoiceId, CustomerId,
 COUNT(*) OVER (PARTITION BY CustomerId) AS InvoiceCount
FROM Invoice ORDER BY CustomerId, InvoiceId;
Show all 412 recordsShow first 10 records
Beginner solution: expected result · Showing 10 of 412 records for clarity412 records
InvoiceIdCustomerIdInvoiceCount
9817
12117
14317
19517
31617
32717
38217
127
1227
6727
19627
21927
24127
29327
9937
11037
16537
29437
31737
33937
39137
247
2447
7647
19747
20847
26347
39247
7757
10057
12257
17457
29557
30657
36157
4667
17567
19867
22067
27267
39367
40467
7877
8977
14477
27377
29677
31877
37077
387
5587
17687
18787
24287
37187
39487
5697
7997
10197
15397
27497
28597
34097
25107
154107
177107
199107
251107
372107
383107
57117
68117
123117
252117
275117
297117
349117
34127
155127
166127
221127
350127
373127
395127
35137
58137
80137
132137
253137
264137
319137
4147
133147
156147
178147
230147
351147
362147
36157
47157
102157
231157
254157
276157
328157
13167
134167
145167
200167
329167
352167
374167
14177
37177
59177
111177
232177
243177
298177
112187
135187
157187
209187
330187
341187
396187
15197
26197
81197
210197
233197
255197
307197
113207
124207
179207
308207
331207
353207
405207
16217
38217
90217
211217
222217
277217
406217
91227
114227
136227
188227
309227
320227
375227
5237
60237
189237
212237
234237
286237
407237
92247
103247
158247
287247
310247
332247
384247
17257
69257
190257
201257
256257
385257
408257
70267
93267
115267
167267
288267
299267
354267
39277
168277
191277
213277
265277
386277
397277
71287
82287
137287
266287
289287
311287
363287
48297
169297
180297
235297
364297
387297
409297
49307
72307
94307
146307
267307
278307
333307
18317
147317
170317
192317
244317
365317
376317
50327
61327
116327
245327
268327
290327
342327
27337
148337
159337
214337
343337
366337
388337
28347
51347
73347
125347
246347
257347
312347
126357
149357
171357
223357
344357
355357
410357
29367
40367
95367
224367
247367
269367
321367
6377
127377
138377
193377
322377
345377
367377
7387
30387
52387
104387
225387
236387
291387
105397
128397
150397
202397
323397
334397
389397
8407
19407
74407
203407
226407
248407
300407
106417
117417
172417
301417
324417
346417
398417
9427
31427
83427
204427
215427
270427
399427
84437
107437
129437
181437
302437
313437
368437
53447
182447
205447
227447
279447
400447
411447
85457
96457
151457
280457
303457
325457
377457
10467
62467
183467
194467
249467
378467
401467
63477
86477
108477
160477
281477
292477
347477
32487
161487
184487
206487
258487
379487
390487
64497
75497
130497
259497
282497
304497
356497
41507
162507
173507
228507
357507
380507
402507
42517
65517
87517
139517
260517
271517
326517
11527
140527
163527
185527
237527
358527
369527
43537
54537
109537
238537
261537
283537
335537
20547
141547
152547
207547
336547
359547
381547
21557
44557
66557
118557
239557
250557
305557
119567
142567
164567
216567
337567
348567
403567
22577
33577
88577
217577
240577
262577
314577
120587
131587
186587
315587
338587
360587
412587
23596
45596
97596
218596
229596
284596

Intermediate · cumulative spending

Restart cumulative spending for each customer.

Hint

Add customer partitioning to the running total. Keep chronological ordering and an explicit ROWS frame.

Solution and checked result

Intermediate solution

Restart cumulative spending for each customer.

SQLite 3.25.0+ · Chinook
SELECT InvoiceId, CustomerId, Total,
 ROUND(SUM(Total) OVER (
 PARTITION BY CustomerId ORDER BY InvoiceDate, InvoiceId
 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
 ), 2) AS RunningTotal
FROM Invoice ORDER BY CustomerId, InvoiceDate, InvoiceId;
Show all 412 recordsShow first 10 records
Intermediate solution: expected result · Showing 10 of 412 records for clarity412 records
InvoiceIdCustomerIdTotalRunningTotal
9813.983.98
12113.967.94
14315.9413.88
19510.9914.87
31611.9816.85
327113.8630.71
38218.9139.62
121.981.98
12213.8615.84
6728.9124.75
19621.9826.73
21923.9630.69
24125.9436.63
29320.9937.62
9933.983.98
110313.8617.84
16538.9126.75
29431.9828.73
31733.9632.69
33935.9438.63
39130.9939.62
243.963.96
2445.949.9
7640.9910.89
19741.9812.87
208415.8628.73
26348.9137.64
39241.9839.62
7751.981.98
10053.965.94
12255.9411.88
17450.9912.87
29551.9814.85
306516.8631.71
36158.9140.62
4668.918.91
17561.9810.89
19863.9614.85
22065.9420.79
27260.9921.78
39361.9823.76
404625.8649.62
7871.981.98
89718.8620.84
14478.9129.75
27371.9831.73
29673.9635.69
31875.9441.63
37070.9942.62
385.945.94
5580.996.93
17681.988.91
187813.8622.77
24288.9131.68
37181.9833.66
39483.9637.62
5691.981.98
7993.965.94
10195.9411.88
15390.9912.87
27491.9814.85
285913.8628.71
34098.9137.62
25108.918.91
154101.9810.89
177103.9614.85
199105.9420.79
251100.9921.78
372101.9823.76
3831013.8637.62
57111.981.98
681113.8615.84
123118.9124.75
252111.9826.73
275113.9630.69
297115.9436.63
349110.9937.62
34120.990.99
155121.982.97
1661213.8616.83
221128.9125.74
350121.9827.72
373123.9631.68
395125.9437.62
35131.981.98
58133.965.94
80135.9411.88
132130.9912.87
253131.9814.85
2641313.8628.71
319138.9137.62
4148.918.91
133141.9810.89
156143.9614.85
178145.9420.79
230140.9921.78
351141.9823.76
3621413.8637.62
36151.981.98
471513.8615.84
102159.9125.75
231151.9827.73
254153.9631.69
276155.9437.63
328150.9938.62
13160.990.99
134161.982.97
1451613.8616.83
200168.9125.74
329161.9827.72
352163.9631.68
374165.9437.62
14171.981.98
37173.965.94
59175.9411.88
111170.9912.87
232171.9814.85
2431713.8628.71
2981710.9139.62
112181.981.98
135183.965.94
157185.9411.88
209180.9912.87
330181.9814.85
3411813.8628.71
396188.9137.62
15191.981.98
261913.8615.84
81198.9124.75
210191.9826.73
233193.9630.69
255195.9436.63
307191.9938.62
113201.981.98
1242013.8615.84
179208.9124.75
308203.9828.73
331203.9632.69
353205.9438.63
405200.9939.62
16213.963.96
38215.949.9
90210.9910.89
211211.9812.87
2222113.8626.73
277218.9135.64
406211.9837.62
91221.981.98
114223.965.94
136225.9411.88
188220.9912.87
309223.9816.85
3202213.8630.71
375228.9139.62
52313.8613.86
60238.9122.77
189231.9824.75
212233.9628.71
234235.9434.65
286230.9935.64
407231.9837.62
92241.981.98
1032415.8617.84
158248.9126.75
287241.9828.73
310247.9636.69
332245.9442.63
384240.9943.62
17255.945.94
69250.996.93
190251.988.91
2012518.8627.77
256258.9136.68
385251.9838.66
408253.9642.62
70261.981.98
93263.965.94
115265.9411.88
167260.9912.87
288261.9814.85
2992623.8638.71
354268.9147.62
39278.918.91
168271.9810.89
191273.9614.85
213275.9420.79
265270.9921.78
386271.9823.76
3972713.8637.62
71281.981.98
822813.8615.84
137288.9124.75
266281.9826.73
289283.9630.69
3112811.9442.63
363280.9943.62
48290.990.99
169291.982.97
1802913.8616.83
235298.9125.74
364291.9827.72
387293.9631.68
409295.9437.62
49301.981.98
72303.965.94
94305.9411.88
146300.9912.87
267301.9814.85
2783013.8628.71
333308.9137.62
18318.918.91
147311.9810.89
170313.9614.85
192315.9420.79
244310.9921.78
365311.9823.76
3763113.8637.62
50321.981.98
613213.8615.84
116328.9124.75
245321.9826.73
268323.9630.69
290325.9436.63
342320.9937.62
27330.990.99
148331.982.97
1593313.8616.83
214338.9125.74
343331.9827.72
366333.9631.68
388335.9437.62
28341.981.98
51343.965.94
73345.9411.88
125340.9912.87
246341.9814.85
2573413.8628.71
3123410.9139.62
126351.981.98
149353.965.94
171355.9411.88
223350.9912.87
344351.9814.85
3553513.8628.71
410358.9137.62
29361.981.98
403613.8615.84
95368.9124.75
224361.9826.73
247363.9630.69
269365.9436.63
321360.9937.62
6370.990.99
127371.982.97
1383713.8616.83
1933714.9131.74
322371.9833.72
345373.9637.68
367375.9443.62
7381.981.98
30383.965.94
52385.9411.88
104380.9912.87
225381.9814.85
2363813.8628.71
291388.9137.62
105391.981.98
128393.965.94
150395.9411.88
202391.9913.87
323391.9815.85
3343913.8629.71
389398.9138.62
8401.981.98
194013.8615.84
74408.9124.75
203402.9827.73
226403.9631.69
248405.9437.63
300400.9938.62
106411.981.98
1174113.8615.84
172418.9124.75
301411.9826.73
324413.9630.69
346415.9436.63
398410.9937.62
9423.963.96
31425.949.9
83420.9910.89
204423.9814.87
2154213.8628.73
270428.9137.64
399421.9839.62
84431.981.98
107433.965.94
129435.9411.88
181430.9912.87
302431.9814.85
3134316.8631.71
368438.9140.62
53448.918.91
182441.9810.89
205447.9618.85
227445.9424.79
279440.9925.78
400441.9827.76
4114413.8641.62
85451.981.98
964521.8623.84
151458.9132.75
280451.9834.73
303453.9638.69
325455.9444.63
377450.9945.62
10465.945.94
62460.996.93
183461.988.91
1944621.8630.77
249468.9139.68
378461.9841.66
401463.9645.62
63471.981.98
86473.965.94
108475.9411.88
160470.9912.87
281471.9814.85
2924713.8628.71
347478.9137.62
32488.918.91
161481.9810.89
184483.9614.85
206488.9423.79
258480.9924.78
379481.9826.76
3904813.8640.62
64491.981.98
754913.8615.84
130498.9124.75
259491.9826.73
282493.9630.69
304495.9436.63
356490.9937.62
41500.990.99
162501.982.97
1735013.8616.83
228508.9125.74
357501.9827.72
380503.9631.68
402505.9437.62
42511.981.98
65513.965.94
87516.9412.88
139510.9913.87
260511.9815.85
2715113.8629.71
326518.9138.62
11528.918.91
140521.9810.89
163523.9614.85
185525.9420.79
237520.9921.78
358521.9823.76
3695213.8637.62
43531.981.98
545313.8615.84
109538.9124.75
238531.9826.73
261533.9630.69
283535.9436.63
335530.9937.62
20540.990.99
141541.982.97
1525413.8616.83
207548.9125.74
336541.9827.72
359543.9631.68
381545.9437.62
21551.981.98
44553.965.94
66555.9411.88
118550.9912.87
239551.9814.85
2505513.8628.71
305558.9137.62
119561.981.98
142563.965.94
164565.9411.88
216560.9912.87
337561.9814.85
3485613.8628.71
403568.9137.62
22571.981.98
335713.8615.84
885717.9133.75
217571.9835.73
240573.9639.69
262575.9445.63
314570.9946.62
120581.981.98
1315813.8615.84
186588.9124.75
315581.9826.73
338583.9630.69
360585.9436.63
412581.9938.62
23593.963.96
45595.949.9
97591.9911.89
218591.9813.87
2295913.8627.73
284598.9136.64

Without PARTITION BY, spending leaks across customers. Without final ORDER BY, display order is not guaranteed.

Advanced · tied amount bands

Keep the two highest distinct invoice amounts per customer, including ties.

Hint

DENSE_RANK counts distinct amounts. Omit InvoiceId from its ordering and filter at an outer level.

Solution and checked result

Advanced solution

Keep the two highest distinct invoice amounts per customer, including ties.

SQLite 3.25.0+ · Chinook
WITH ranked AS (
 SELECT InvoiceId, CustomerId, Total,
 DENSE_RANK() OVER (
 PARTITION BY CustomerId ORDER BY Total DESC
 ) AS AmountRank FROM Invoice
)
SELECT InvoiceId, CustomerId, Total, AmountRank
FROM ranked WHERE AmountRank <= 2
ORDER BY CustomerId, AmountRank, InvoiceId;
Show all 118 recordsShow first 10 records
Advanced solution: expected result · Showing 10 of 118 records for clarity118 records
InvoiceIdCustomerIdTotalAmountRank
327113.861
38218.912
12213.861
6728.912
110313.861
16538.912
208415.861
26348.912
306516.861
36158.912
404625.861
4668.912
89718.861
14478.912
187813.861
24288.912
285913.861
34098.912
3831013.861
25108.912
681113.861
123118.912
1661213.861
221128.912
2641313.861
319138.912
3621413.861
4148.912
471513.861
102159.912
1451613.861
200168.912
2431713.861
2981710.912
3411813.861
396188.912
261913.861
81198.912
1242013.861
179208.912
2222113.861
277218.912
3202213.861
375228.912
52313.861
60238.912
1032415.861
158248.912
2012518.861
256258.912
2992623.861
354268.912
3972713.861
39278.912
822813.861
3112811.942
1802913.861
235298.912
2783013.861
333308.912
3763113.861
18318.912
613213.861
116328.912
1593313.861
214338.912
2573413.861
3123410.912
3553513.861
410358.912
403613.861
95368.912
1933714.911
1383713.862
2363813.861
291388.912
3343913.861
389398.912
194013.861
74408.912
1174113.861
172418.912
2154213.861
270428.912
3134316.861
368438.912
4114413.861
53448.912
964521.861
151458.912
1944621.861
249468.912
2924713.861
347478.912
3904813.861
206488.942
754913.861
130498.912
1735013.861
228508.912
2715113.861
326518.912
3695213.861
11528.912
545313.861
109538.912
1525413.861
207548.912
2505513.861
305558.912
3485613.861
403568.912
885717.911
335713.862
1315813.861
186588.912
2295913.861
284598.912

This keeps two amount bands, not necessarily two rows. RANK <= 2 may omit the second distinct amount if the highest amount is tied.

References and next steps

Download every example and solution · Related: JOINs vs Subqueries · SQL & Data Skills.

Corrections: report an issue with the query, SQLite version and expected result. AI-assisted writing with automated SQL checks and independent technical review.