For each order, compare its amount to the average amount of its own region. Return orders whose amount is strictly above their region's average, with the average rounded to 2 decimals. Orders with a NULL region or NULL amount are ignored. Return order_id, region, amount, region_avg. Row order does not matter.
| order_id | customer_id | region | product | qty | amount | order_date |
|---|---|---|---|---|---|---|
| 101 | 1 | North | Laptop | 1 | 55000.0 | 2024-01-05 |
| 102 | 2 | South | Phone | 2 | 30000.0 | 2024-01-18 |
| 103 | 1 | North | Mouse | 3 | 1500.0 | 2024-02-02 |
| 104 | 3 | West | Tablet | 1 | 22000.0 | 2024-02-15 |
| 105 | 4 | South | Laptop | 1 | 55000.0 | 2024-03-01 |
| 106 | 2 | East | Headphones | 2 | 4000.0 | 2024-03-09 |
| order_id | region | amount | region_avg |
|---|---|---|---|
| 101 | North | 55000 | 28250 |
| 105 | South | 55000 | 42500 |
| order_id | customer_id | region | product | qty | amount | order_date |
|---|---|---|---|---|---|---|
| 201 | 5 | East | Phone | 1 | 15000.0 | 2024-04-12 |
| 202 | 6 | West | Keyboard | 4 | 6000.0 | 2024-04-20 |
| 203 | 5 | North | Mouse | 2 | 999.5 | 2024-05-03 |
| 204 | 7 | North | Tablet | 1 | 25000.0 | 2024-05-21 |
| order_id | region | amount | region_avg |
|---|---|---|---|
| 204 | North | 25000 | 12999.75 |
Return exactly order_id, region, amount, region_avg (region_avg rounded to 2 decimals; compare against the unrounded average).
Solve it in the live editor on this page — run on sample data, then submit against hidden tests.
{{ 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.