Lesson 9 of 12
Structured learning draftCompare each sale with its customer total
Sometimes a report needs every original sale and the customer's total beside it. Use a window function to preserve individual records while computing a related summary, rather than collapsing the result to one row per customer.
Learning objectives
- Keep individual sale rows in an analytical result.
- Define a window partition.
- Distinguish a window total from a grouped result.
Compare each sale with its customer total: the working sequence
Trace the three stages of compare each sale with its customer total and compare them with the example.
Each output row still represents one sale.
The window groups related rows by customer for calculation only.
Each Amina row displays the same known customer total of 200.
A window aggregate computes across a partition while keeping each input row. Here PARTITION BY customer defines the related records. There is no window ordering, so SUM covers the entire customer partition.
WITH sales(id, customer, amount, status) AS (
VALUES (1, 'Amina', 120, 'paid'),
(2, 'Amina', 80, 'pending'),
(3, 'Brian', 50, 'paid'),
(4, 'Chen', NULL, 'paid')
)
SELECT id, customer, amount,
SUM(amount) OVER (PARTITION BY customer) AS customer_total
FROM sales
ORDER BY id;id | customer | amount | customer_total
1 | Amina | 120 | 200
2 | Amina | 80 | 200
3 | Brian | 50 | 50
4 | Chen | NULL | NULLTry it, then change it
- Compare the four returned rows with a GROUP BY customer query.
- Add another Amina sale and predict both the row count and repeated total.
- Explain why adding ORDER BY inside OVER can change the default frame and is not a cosmetic edit.
Common mistakes
- Adding repeated window totals together and counting the same customer total multiple times.
- Confusing a window partition with collapsed grouped output.
- Adding window ordering without considering the frame.
Practice activity
Apply the lesson
Add COUNT(*) OVER (PARTITION BY customer) to the query. Explain why Amina's rows both show two while the result still contains four sales. Keep the changed example, its output and a short explanation of whether the result matched your prediction.
How to check your work
- Write down the expected result before running or submitting your work.
- Try a normal example, an empty or missing input, and a boundary case where the rule changes.
- Compare the actual result with your prediction. Record the smallest example that explains a difference.
- Explain one change you made and why it improved the result. Save your evidence in the lesson notes.
When you get stuck · databases
A join returns more rows than expected.
- Find the cause
- Count matching records for each join key in both tables. A key repeated on both sides creates multiple combinations.
- Try this
- Decide the intended row grain first: one row per customer, order or item. Aggregate at that grain or correct the join condition; do not hide the problem with DISTINCT.
- Check the fix
- Use two customers and three orders as a fixture. Predict every result row and check unmatched records separately.
Use this course-wide example when it fits your task. For a different failure, record the input, expected result, actual result and smallest steps that reproduce it.
Watch, pause, practise
Free video lessons
Choose the chapter that matches your task. Videos open on the original provider’s site; playback uses additional data.
CS50: databases with SQL
Watch Querying or Relating to see selection and joins explained using actual tables. Examples begin with SQLite; check syntax when using PostgreSQL.
Try it yourself: Draw two small tables, predict the joined rows, then compare your prediction with the query result.
Source checked 2026-10-07 · Free learning material; optional certificates may cost extra.Check your understanding
How many rows does this window query return for the four-row fixture?
Lesson summary
- Window functions preserve input row grain.
- PARTITION BY defines related rows for the calculation.
- A repeated partition total must not be summed as if it were a row amount.
Sources and further reading
- Window functionsPostgreSQL Global Development Group - accessed 2026-10-07
- PostgreSQL SQL language guidePostgreSQL Global Development Group - accessed 2026-10-07
Personal study note
Your notes
Keep the input, expected result, actual result and what you changed. Save before leaving this lesson. Download a copy to keep it outside this browser.