SUM OVER: Show Grand Total on Every Row

SQL coding challenge · Difficulty: easy · Topic: Window Functions · +50 XP

Problem

A sales report needs each row to carry the grand total of the whole table

next to it, so a reader can see at a glance what share of the total that one sale

represents. Every row shows the same total.

Engine: queries run on MySQL 8.0, so use MySQL-compatible syntax and functions.

Tables

daily_sales — one row per sale.

| column | type | description |
| --- | --- | --- |
| sale_id | INT | identifies the sale |
| day_name | VARCHAR | the day's label; not necessarily unique |
| amount | INT | the sale value; may be negative, or NULL if unrecorded |

Return every row of daily_sales, with one extra column:

Sort the result by sale_id ascending.

The same number, on every row

The rows are not grouped or collapsed — all of them come back, and every one

carries the identical figure. The total covers the whole table: it does not

restart per day, and it does not accumulate as you read down the rows. Row 1

already shows the full total, not just its own amount.

That also means the answer cannot depend on the order of the rows. A grand total

is the same number whichever way the table is read, so nothing about sorting

belongs in how you compute it — sorting is only how the result is displayed.

NULL amounts are skipped, and an empty total is NULL

An unrecorded amount contributes nothing to the total; the other rows still add

up normally. But if every amount is NULL there is nothing at all to add, and

the grand total is NULL — not 0. Do not substitute a zero there.

A total of 0 is a different thing entirely and a real possibility: a table whose

refunds cancel its takings exactly totals 0, and that is a genuine zero.

Example input

daily_sales

| sale_id | day_name | amount |
| --- | --- | --- |
| 1 | Mon | 100 |
| 2 | Tue | 200 |
| 3 | Wed | 300 |
| 4 | Thu | 400 |
| 5 | Fri | 500 |

Expected output

| sale_id | day_name | amount | grand_total |
| --- | --- | --- | --- |
| 1 | Mon | 100 | 1500 |
| 2 | Tue | 200 | 1500 |
| 3 | Wed | 300 | 1500 |
| 4 | Thu | 400 | 1500 |
| 5 | Fri | 500 | 1500 |

100 + 200 + 300 + 400 + 500 = 1500, and that one figure is repeated down the

column. Monday's row already reads 1500, not 100 — a running total that

built up to 1500 only on the last row would be a different answer.

What this SQL challenge teaches you

“SUM OVER: Show Grand Total on Every Row” is a easy-level SQL challenge focused on Window Functions. Working through it gives you hands-on practice with SUM OVER, Grand Total, OVER() — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.

Concepts covered

How to approach it

If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:

  1. Every row stays in the result and gains a column, so a plain GROUP BY is the wrong tool -- it would collapse the five rows you are asked to return into one.
  2. You need an aggregate that is computed over the whole table but reported on every row. A window function does this; so does a scalar subquery or a cross join against the total -- all three are accepted.
  3. If your first row shows its own amount rather than the full total, you have built a running total. Something in your query is making the aggregate depend on row order -- a grand total must not.
  4. Likewise, if each row shows a figure specific to its day, you have split the table into groups. The total here spans every row, and day_name values are not even guaranteed to be unique.
  5. Leave NULL amounts alone: summing skips them automatically. Do not wrap the result in a zero-default -- a table where every amount is NULL has a grand total of NULL, and forcing that to 0 is a different answer.

Where this comes up

Variations of this problem have been reported in interviews at Amazon, Google, Cloudflare, DataDog. Interviewers use it to check whether you can express the logic cleanly and reason about correctness on edge cases such as ties, nulls and empty groups.

How to practise it on PySpark.in

Open the challenge, write your SQL query in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.

Related SQL challenges

Helpful resources

Frequently asked questions

Do I need to install Spark or a database to solve this?

No. The SQL environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.

Is this challenge free?

Yes - the problem, the sample dataset, the hints and unlimited test runs are free.

What level is it?

It is rated easy and covers Window Functions.

Solve this challenge free on PySpark.in