We are going to write a SQL query that uses a window function to calculate a running total.
Let's assume we have a table named sales with at least the following columns:
sale_date (the date of the sale)
amount (the amount of the sale)
We want to calculate the running total of amount ordered by sale_date.
If there are multiple sales on the same date, we might want to order by something else (like an id) to have a deterministic order.
Let's assume we have an id column that is unique and we can use to break ties.
So the query would look like:
SELECT
id,
sale_date,
amount,
SUM(amount) OVER (ORDER BY sale_date, id) AS running_total
FROM sales
ORDER BY sale_date, id;
However, note that the running total is calculated over the entire result set (because we didn't specify a partition).
If we wanted to partition by some column (like by a category or by a customer) we would add a PARTITION BY clause.
But the problem says "for example, to calculate a running total", so let's stick to the simple case.
If the table does not have an id and we only have sale_date and amount, and there might be multiple rows with the same date,
then the order within the same date might be arbitrary. So it's better to have a unique column to order by.
Alternatively, if we don't have a unique column, we could use:
SUM(amount) OVER (ORDER BY sale_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
But note that without a unique ordering, the running total for the same date might be calculated in an arbitrary order.
However, the standard way is to order by the date and then by a unique identifier if available.
Let me adjust: if we don't have a unique column, we can just order by sale_date and then the running total will be the same for all rows on the same date?
Actually, no: the window function will process rows in the order of the ORDER BY. If there are multiple rows with the same sale_date, then the order among them is not defined (unless we have a tie-breaker).
So, to be safe, let's assume we have an id column.
But if we don't, and we want the running total to be the same for all rows on the same date (i.e., the running total up to the end of that date), then we might want to use:
SUM(amount) OVER (ORDER BY sale_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
However, without a unique ordering, the rows with the same date will be ordered arbitrarily and the running total for the second row on the same date would include the first row on that date.
If we want the running total as of the end of the previous day, we might use RANGE instead of ROWS? But that's a different requirement.
Since the problem says "running total", it typically means cumulative up to the current row in the specified order.
So, let's write two versions:
Version 1: With a unique column (like id) to break ties.
Version 2: Without a unique column, but then we must accept that the order of rows with the same date is arbitrary and the running total might be computed in an arbitrary order for that date.
But note: if we use only sale_date in the ORDER BY and there are multiple rows on the same date, then the window function will treat them as having the same sort key and the order among them is not guaranteed.
However, in practice, many databases will return the rows in the order they are read if the ORDER BY doesn't uniquely order, but that's not guaranteed.
So, it's better to have a unique column.
Since the problem doesn't specify the table structure, I'll write a query that uses a unique column (id) and then one that doesn't.
But let me stick to the safe version with a unique column.
However, if the table doesn't have a unique column, we can use the sale_date and then the amount? That might not be unique either.
Alternatively, we can use the sale_date and then the ROW_NUMBER() to generate a unique order? But that's more complicated.
Since the problem is about window functions, let's assume we have a unique column.
Query with unique column (id):
SELECT
id,
sale_date,
amount,
SUM(amount) OVER (ORDER BY sale_date, id) AS running_total
FROM sales
ORDER BY sale_date, id;
If we don't have a unique column, and we just want the running total up to the current row in the order of sale_date (and any order for same date), we can do:
SELECT
sale_date,
amount,
SUM(amount) OVER (ORDER BY sale_date) AS running_total
FROM sales
ORDER BY sale_date;
But note: the same date rows will have the same running total? Actually, no. The window function will process the rows in some order for the same date, so the running total for the first row of a given date will be the total up to the previous row (which might be from a previous date or the same date if there are multiple on that date). And then the second row on the same date will include the first row on that date.
Example:
sale_date | amount | running_total (with ORDER BY sale_date only)
2020-01-01 | 10 | 10
2020-01-01 | 20 | 30 (because it comes after the first row of the same date)
2020-01-02 | 5 | 35
So, without a tie-breaker, the running total for the two rows on 2020-01-01 will be 10 and 30.
If we want the running total as of the end of the day (so both rows on 2020-01-01 show 30), then we need to use RANGE or we need to aggregate by day first.
But the problem says "running total", which is typically row-by-row.
So, I think the first version with a unique column is the intended one.
However, if the requirement is to have the running total by date (so each date shows the total up to that date, and if there are multiple rows on the same date, they all show the same total for that date), then we would do:
SELECT
sale_date,
SUM(amount) AS daily_total,
SUM(SUM(amount)) OVER (ORDER BY sale_date) AS running_total_by_date
FROM sales
GROUP BY sale_date
ORDER BY sale_date;
But that's a different query because it first aggregates by date.
Since the problem says "calculate a running total" and uses a window function, and typically window functions operate on rows, I think the row-by-row version is expected.
Let me re-read: "Write a SQL query that uses a window function, for example, to calculate a running total"
So, I'll provide the row-by-row version with a unique column.
But if the table doesn't have a unique column, we can generate one with ROW_NUMBER()? Actually, we can use the built-in rowid in SQLite or ctid in PostgreSQL, but that's not standard.
Alternatively, we can use:
SUM(amount) OVER (ORDER BY sale_date, (SELECT NULL))
But that doesn't guarantee a unique order.
Given the ambiguity, I think it's best to state our assumption.
Assumption: The table has a unique column (like an id) that we can use to break ties in the ORDER BY.
So, the query:
SELECT
id,
sale_date,
amount,
SUM(amount) OVER (ORDER BY sale_date, id) AS running_total
FROM sales
ORDER BY sale_date, id;
If we don't have a unique column, we might have to accept an arbitrary order for same dates, or we can use:
SUM(amount) OVER (ORDER BY sale_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
But note that without a unique order, the result for the same date might be non-deterministic.
However, the SQL standard says that without a unique order, the rows with the same sort key are considered peers and the window function may process them in any order.
So, to be deterministic, we must have a unique order.
Therefore, I will write the query with the assumption of a unique column.
If we are in a database that has a way to get a unique row identifier (like the hidden rowid in SQLite or ctid in PostgreSQL), we can use that.
But since the problem doesn't specify the database, I'll stick to the standard and assume an id column.
Let me write the query accordingly.
However, note: if we are using a database that doesn't guarantee the order of rows with the same without a unique column, then we must have a unique column.
So, final answer:
SELECT
id,
sale_date,
amount,
SUM(amount) OVER (ORDER BY sale_date, id) AS running_total
FROM sales
ORDER BY sale_date, id;
But wait, what if we want the running total to be reset for each group? For example, per customer? Then we would use PARTITION BY.
The problem doesn't specify, so we do a running total over the entire table.
However, the problem says "for example", so let's also show an example with partitioning.
But the instruction says "Write a SQL query that uses a window function, for example, to calculate a running total"
So, I think one example is enough.
I'll provide the simple one without partition.
If we want to partition by a column (say customer_id), then:
SELECT
customer_id,
sale_date,
amount,
SUM(amount) OVER (PARTITION BY customer_id ORDER BY sale_date, id) AS running_total
FROM sales
ORDER BY customer_id, sale_date, id;
But since the problem doesn't specify, I'll stick to the first one.
However, to be thorough, let me write both and let the user choose.
But the problem asks for one query.
I think the most common example is without partition.
So, I'll write the one without partition.
But note: the problem says "for example", so maybe they want to see the window function syntax.
So, I'll write:
Example 1: Running total without partition (over entire table)
Example 2: Running total with partition (by customer)
Since the problem says "for example", I think one is enough. But I'll write the one without partition and then mention that we can add PARTITION BY.
However, the instruction says "Write a SQL query", so I'll write one query.
Let me decide: the most basic is without partition.
So, the answer:
SELECT
id,
sale_date,
amount,
SUM(amount) OVER (ORDER BY sale_date, id) AS running_total
FROM sales
ORDER BY sale_date, id;
But note: the outer ORDER BY is not necessary for the window function, but it's common to order the result set the same way as the window function.
Alternatively, we can omit the outer ORDER BY and let the database return in any order? But we usually want the result in the order of the running total.
So, I'll keep it.
However, if the table is large and we don't care about the order of the result set, we can remove the outer ORDER BY.
But for presentation, we want it ordered.
So, I'll keep it.
Final query:
SELECT
id,
sale_date,
amount,
SUM(amount) OVER (ORDER BY sale_date, id) AS running_total
FROM sales
ORDER BY sale_date, id;
Here's a SQL query that calculates a running total using a window function:
SELECT
order_date,
amount,
SUM(amount) OVER (
ORDER BY order_date
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS running_total
FROM sales
ORDER BY order_date;
Explanation:
-
SUM(amount) OVER (...)
The SUM() aggregate becomes a window function when paired with OVER(). It calculates a cumulative sum without collapsing rows.
-
ORDER BY order_date
Specifies the chronological order for the running total. Rows are processed from oldest to newest.
-
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
Explicitly defines the window frame:
UNBOUNDED PRECEDING: Start from the first row in the partition.
CURRENT ROW: End at the current row.
(Note: This is the default frame for ORDER BY in most databases, so it’s optional. You can omit it and just write SUM(amount) OVER (ORDER BY order_date).)
-
Result Example:
| order_date | amount | running_total |
|---|
| 2023-01-01 | 100 | 100 |
| 2023-01-02 | 50 | 150 |
| 2023-01-03 | 200 | 350 |
Optional Enhancements:
1. Partition by Category (e.g., per product/category):
SUM(amount) OVER (
PARTITION BY category
ORDER BY order_date
) AS running_total_by_category
2. Handle Ties (if multiple rows share the same date, add a tie-breaker like id):
ORDER BY order_date, id
3. Database-Specific Notes:
- PostgreSQL/MySQL 8.0+/SQL Server: Supports full window function syntax as shown.
- Oracle: Use
RANGE instead of ROWS if you want to include all rows with the same order_date value in the cumulative sum.
- BigQuery/Snowflake: Default frame is
RANGE for ORDER BY—use ROWS for row-based cumulative totals.
Why Use Window Functions?
- No self-joins or subqueries needed (unlike traditional running totals).
- Clearer and more efficient for large datasets.
- Flexible (can combine with partitions, filters, and other aggregates).
Let me know if you'd like an example with partitions or a different aggregate (like moving average)!