HR wants a roster sorted by department (A to Z), then by salary from highest to lowest, and finally by emp_id ascending to break ties. Return emp_id, name, dept, salary. Rows with a NULL dept or NULL salary should appear last within their sort key. Order matters.
| emp_id | name | dept | salary | manager_id | hire_date |
|---|---|---|---|---|---|
| 1 | Anil Kapoor | Engineering | 95000 | NULL | 2019-04-01 |
| 2 | Meera Nair | Engineering | 72000 | 1 | 2020-07-15 |
| 3 | Rahul Verma | Sales | 48000 | 5 | 2021-01-10 |
| 4 | Divya Menon | Engineering | 72000 | 1 | 2022-03-21 |
| 5 | Suresh Patil | Sales | 81000 | NULL | 2018-11-05 |
| 6 | Asha Rao | HR | 55000 | 5 | 2023-02-01 |
| emp_id | name | dept | salary |
|---|---|---|---|
| 1 | Anil Kapoor | Engineering | 95000 |
| 2 | Meera Nair | Engineering | 72000 |
| 4 | Divya Menon | Engineering | 72000 |
| 6 | Asha Rao | HR | 55000 |
| 5 | Suresh Patil | Sales | 81000 |
| 3 | Rahul Verma | Sales | 48000 |
| emp_id | name | dept | salary | manager_id | hire_date |
|---|---|---|---|---|---|
| 11 | Harish Kumar | Finance | 60000 | NULL | 2017-06-01 |
| 12 | Lakshmi Iyer | Finance | 60000 | 11 | 2019-09-12 |
| 13 | Akash Jain | Finance | 45000 | 11 | 2021-02-02 |
| 14 | Ramesh | Ops | 52000 | 11 | 2022-08-30 |
| emp_id | name | dept | salary |
|---|---|---|---|
| 11 | Harish Kumar | Finance | 60000 |
| 12 | Lakshmi Iyer | Finance | 60000 |
| 13 | Akash Jain | Finance | 45000 |
| 14 | Ramesh | Ops | 52000 |
Return exactly emp_id, name, dept, salary, sorted by dept ASC (NULLs last), salary DESC (NULLs last), emp_id ASC.
Solve it in the live editor on this page — run on sample data, then submit against hidden tests.
‹ Tag Orders with Currency · Top 3 Most Expensive Products ›
{{ 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.