For each product compute revenue (sum of amount) and pct_of_total = revenue × 100 / total revenue of all products, rounded to 2 decimals. Ignore orders with a NULL product. Sort by revenue descending, then product ascending. Order matters.
| order_id | customer_id | region | product | qty | amount | order_date |
|---|---|---|---|---|---|---|
| 1 | 1 | North | Laptop | 1 | 55000.0 | 2024-01-05 |
| 2 | 2 | South | Phone | 2 | 30000.0 | 2024-01-18 |
| 3 | 1 | North | Mouse | 3 | 1500.0 | 2024-02-02 |
| 4 | 3 | West | Laptop | 1 | 55000.0 | 2024-02-15 |
| 5 | 4 | South | Phone | 1 | 15000.0 | 2024-03-01 |
| 6 | 2 | West | Mouse | 2 | 1000.0 | 2024-03-09 |
| 7 | 1 | North | Phone | 1 | 30000.0 | 2024-03-20 |
| product | revenue | pct_of_total |
|---|---|---|
| Laptop | 110000 | 58.67 |
| Phone | 75000 | 40 |
| Mouse | 2500 | 1.33 |
| order_id | customer_id | region | product | qty | amount | order_date |
|---|---|---|---|---|---|---|
| 11 | 5 | East | Tablet | 1 | 20000.0 | 2024-04-12 |
| 12 | 5 | East | Tablet | 1 | 20000.0 | 2024-04-14 |
| 13 | 6 | East | Desk | 1 | 12000.0 | 2024-05-02 |
| 14 | 7 | Central | Desk | 2 | 24000.0 | 2024-05-30 |
| product | revenue | pct_of_total |
|---|---|---|
| Tablet | 40000 | 52.63 |
| Desk | 36000 | 47.37 |
Return exactly product, revenue, pct_of_total sorted by revenue DESC, product ASC.
Solve it in the live editor on this page — run on sample data, then submit against hidden tests.
‹ Quarterly Revenue Pivot · Best-Selling Product per Region ›
{{ totalCount }} questions · {{ shHardPct }}% Hard. Real PySpark DataFrame and Spark SQL interview questions. Write PySpark in your browser on TechTown’s Spark engine, run sample tests, then submit against hidden tests.
Guests can solve {{ freeLimit }} problems for free. Log in or sign up (free) to unlock everything, keep your streak and appear on the leaderboard. New to a topic? Learn it first in the Spark course.
{{ p.statement }}
{{ p.constraints }}
No submissions yet. Press Submit to judge your code against all tests.
The model solution and step-by-step explanation unlock when you solve this problem or after 3 unsuccessful submissions ({{ shFailsLabel }}). Struggling? Try the hints on the Question tab first.
{{ p.solution }}
Write real PySpark: from pyspark.sql import functions as F, Window, spark.sql(...). It runs in your browser on a lightweight Spark engine, and the same code runs unchanged on Databricks or any Spark cluster. Use df.show() or print() to debug.
Create your free TechTown account to unlock every problem, track your progress, bookmark questions and keep preparing for interviews.