Join orders to products on product to get each order's category (orders whose product is missing from products are dropped). Compute yearly revenue per category (revenue = SUM(amount) rounded to 2, yr = YEAR(order_date)). yoy_pct = (revenue - previous year's revenue) / previous year's revenue * 100, rounded to 2. It is NULL when the category has no revenue row for exactly the previous calendar year (yr - 1) or that revenue is 0.
| order_id | customer_id | region | product | qty | amount | order_date |
|---|---|---|---|---|---|---|
| 1 | 1 | N | Laptop | 1 | 100000.0 | 2023-03-01 |
| 2 | 1 | N | Mobile | 1 | 20000.0 | 2023-05-01 |
| 3 | 1 | N | Laptop | 1 | 150000.0 | 2024-02-01 |
| 4 | 1 | N | Pen | 1 | 50.0 | 2024-04-01 |
| product | category | price |
|---|---|---|
| Laptop | Electronics | 50000.0 |
| Mobile | Electronics | 20000.0 |
| Pen | Stationery | 10.0 |
| category | yr | revenue | yoy_pct |
|---|---|---|---|
| Electronics | 2023 | 120000 | NULL |
| Electronics | 2024 | 150000 | 25 |
| Stationery | 2024 | 50 | NULL |
| order_id | customer_id | region | product | qty | amount | order_date |
|---|---|---|---|---|---|---|
| 5 | 1 | N | Pen | 1 | 100.0 | 2022-01-01 |
| 6 | 1 | N | Pen | 1 | 80.0 | 2023-01-01 |
| 7 | 1 | N | Pen | 1 | 120.0 | 2025-01-01 |
| product | category | price |
|---|---|---|
| Pen | Stationery | 10.0 |
| category | yr | revenue | yoy_pct |
|---|---|---|---|
| Stationery | 2022 | 100 | NULL |
| Stationery | 2023 | 80 | -20 |
| Stationery | 2025 | 120 | NULL |
Return columns category, yr, revenue, yoy_pct. Row order does not matter.
Solve it in the live editor on this page — run on sample data, then submit against hidden tests.
‹ Build SCD Type-2 Validity Ranges · Customers Who Churned in 2024 ›
{{ 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.