PySpark Koans

Learn by fixing tests

Progress0/39

© 2025-2026 Alex Cole. All Rights Reserved.

Spark Koans is an independent community tool.

Delta LakeKoan 103

MERGE - Upsert Pattern

Use MERGE to update existing rows and insert new ones. Replace ___ with the correct code.

How it works: Replace the ___ blanks in the code editor with the correct PySpark code, then hit Run Code. Stuck? Try the Hint button.
Setup (read-only)
_reset_delta_tables()

# Create target table
target_data = [("Alice", 100), ("Bob", 200)]
target_df = spark.createDataFrame(target_data, ["name", "balance"])
target_df.write.format("delta").save("/data/accounts")

# Source data with updates and new records
source_data = [("Alice", 150), ("Charlie", 300)]  # Alice updated, Charlie is new
source_df = spark.createDataFrame(source_data, ["name", "balance"])
Your CodeCtrl/Cmd+Enter to run
Output
Output will appear here...