Cert EspressoLearn. Practise. Find your flow.

A LITTLE FOCUS.
A BIG NEXT STEP.

Your pace. Your space.
Databricks · Databricks Data Engineer Professional

Window functions, joins, and VARIANT

Databricks Data Engineer Professional · Lesson 10 of 21 · 1:20:52

YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗

Learning with these videos? Subscribe for new courses and exam-style questions.

In this lesson

Prepare for the Databricks Certified Data Engineer Professional exam. In this lesson: window functions, joins and aggregations at scale, and the VARIANT type for semi-structured data. Includes three original practice questions.

Study notes

The text of this lesson, to read at your own pace. Information as of November 2026.

Slow, fragile queries

Quayside Eats analysts need the latest status of every order, a seven day moving average of deliveries per restaurant, and fields from courier app events whose JSON shape changes at every release. Today, the queries use self joins and string parsing, and they are slow and break often. This lesson covers window functions, joins at scale, and the VARIANT type.

Window functions

A window function computes a value for each row from a group of related rows, with an over clause, partition by for the groups, and order by for the order inside each group. Ranking functions number the rows: row_number gives unique numbers, rank leaves gaps after ties, and dense_rank does not. lag and lead return a value from the previous or the next row, for example to measure the time between two statuses of an order. Aggregates over a frame give running totals and moving averages: rows between six preceding and current row averages the last seven days, when there is one row per day. And to keep the latest row per key, number the rows of each key by descending timestamp, then keep row number one.

Joins and aggregations at scale

To join a large table with a small one, Spark broadcasts the small table to every executor, which avoids a shuffle, and when both sides are large, it uses a sort merge join. Adaptive query execution, on by default, adjusts the plan at runtime: it switches to a broadcast join when one side turns out small, combines small shuffle partitions, and splits skewed ones. Join hints override the choice of the optimizer: broadcast, merge, shuffle hash, and skew. A broadcast hint on a large table can run the driver or the executors out of memory, so keep it for small dimensions. For aggregations, group by computes exact results, while approx_count_distinct trades a small error for much less memory and time on large data. And filter early, and select only the columns that you need, so that less data moves between executors.

The VARIANT type

The VARIANT type stores semi-structured data, like JSON, in an efficient binary form, so new or changed fields need no schema change. parse_json turns a JSON string into a VARIANT, and fails on malformed JSON, while try_parse_json returns null instead. A colon path, like payload, colon, device, dot, O S, reads a field, and these paths are case sensitive. variant_get takes a path and a target type, returns null when the path is missing, and fails when the value cannot be cast, while try_variant_get returns null instead. Databricks recommends VARIANT over JSON strings, with faster reads and writes, since the text is not parsed again at every query.

Recap

Let's recap. Window functions use an over clause, with partition by and order by, and row_number equal to one keeps the latest row per key. rank leaves gaps after ties, dense_rank does not, and a frame of rows gives moving averages. Spark broadcasts small tables, adaptive query execution adjusts joins at runtime, and hints override the choice. VARIANT stores semi-structured data: parse_json fills it, and colon paths and variant_get read it. And the try versions of these functions return null instead of failing.

Quick checks for this lesson

The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.

  1. A leaderboard ranks couriers by deliveries in each city. Couriers with the same count must share a rank, and the next rank must follow with no gap. Which function fits?

    • dense_rank()
    • rank()
    • row_number()
    • ntile(4)
    Show the answer

    dense_rank()

    Dense_rank gives tied rows the same rank, and the next rank follows with no gap. “rank()” would also share ranks between ties, but leave a gap after them. “row_number()” would give tied couriers different numbers. And “ntile(4)” would split the couriers into a fixed number of groups, which is not a rank.

  2. A join between a huge orders table and a restaurants table of a few megabytes still shuffles both sides, according to the query plan. What should the data engineer do?

    • Add a MERGE hint on the orders table
    • Broadcast the orders table to every executor
    • Raise the number of shuffle partitions
    • Add a BROADCAST hint on the restaurants table
    Show the answer

    Add a BROADCAST hint on the restaurants table

    A broadcast hint sends the small restaurants table to every executor, so the large orders table is not shuffled. “Add a MERGE hint on the orders table” would ask for a sort merge join, which shuffles both sides. “Broadcast the orders table to every executor” would send the huge table to every executor, and can run them out of memory. And “Raise the number of shuffle partitions” would change how many partitions the shuffle uses, but both sides would still be shuffled.

  3. A query reads the app build from a VARIANT column with variant_get, as an integer. A few events carry a build like 4.2b, and the whole query fails. The team wants NULL for those rows. What should the query use?

    • parse_json on the column again
    • try_variant_get with the same path and type
    • The path with uppercase keys
    • try_parse_json on the column
    Show the answer

    try_variant_get with the same path and type

    Try_variant_get returns null when a value cannot be cast to the target type, so the query completes. “parse_json on the column again” would parse a string again, while the column is already a VARIANT. “The path with uppercase keys” would change the case of the path, which does not fix a value that cannot be cast. And “try_parse_json on the column” only applies when parsing JSON strings, not when reading a VARIANT.