Skip to content

37. Joining on Nullable Keys

Difficulty: Medium · Topics: Joins, Null Handling, Filtering & Selection

targets and actuals are keyed by (country, city). Country-level rows have city = null, and those should match each other too.

Inner-join the two so that null matches null. Return country, city, target, actual.

Row order does not matter; column names must match. Your code is graded on 3 test cases, including hidden edge cases.

Sample data

targets

countrycitytarget
INnull1000
INPune300
USNYC500

actuals

countrycityactual
INnull950
INPune320
USLA100

Expected output

countrycitytargetactual
INnull1000950
INPune300320

Hints

Hint 1A normal == never matches nulls: null == null is null, not true.
Hint 2Use the null-safe equality eqNullSafe (SQL <=>) for each key.
Hint 3Alias the tables so you can pick one copy of each key column.

PySpark functions you'll practise

Related problems

Browse

Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings

Difficulty: Easy · Medium · Hard · PySpark interview roadmap · All problems