Using spark.sql over a temp view, return for each region the number of orders and total_revenue = SUM(amount) rounded to 2. Sort by total_revenue descending, then region ascending.
| order_id | customer_id | region | product | qty | amount | order_date |
|---|---|---|---|---|---|---|
| 1 | 101 | North | Laptop | 1 | 55000.0 | 2024-01-05 |
| 2 | 102 | South | Mobile | 2 | 30000.0 | 2024-01-07 |
| 3 | 101 | North | Keyboard | 1 | 1500.0 | 2024-02-02 |
| 4 | 103 | East | Monitor | 1 | 18000.0 | 2024-02-10 |
| 5 | 104 | South | Headphones | 3 | 6000.0 | 2024-03-01 |
| region | orders | total_revenue |
|---|---|---|
| North | 2 | 56500 |
| South | 2 | 36000 |
| East | 1 | 18000 |
| order_id | customer_id | region | product | qty | amount | order_date |
|---|---|---|---|---|---|---|
| 1 | 1 | West | A | 1 | 100.0 | 2024-01-01 |
| 2 | 1 | East | A | 1 | 100.0 | 2024-01-01 |
| 3 | 1 | North | A | 1 | 50.0 | 2024-01-01 |
| region | orders | total_revenue |
|---|---|---|
| East | 1 | 100 |
| West | 1 | 100 |
| North | 1 | 50 |
Return columns region, orders, total_revenue, sorted as described. Solve it with createOrReplaceTempView + spark.sql.
Solve it in the live editor on this page — run on sample data, then submit against hidden tests.
‹ Candidates with All Required Skills · Bucket Orders with CASE WHEN ›
{{ 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.