Practical SQL

Where does QUALIFY fit in the order of execution in BigQuery?

Constantin LunguUpdated 1 min read

Photo by Markus Spiske on Unsplash

Here's an example of how QUALIFY fits into the order of execution in SQL.

In the BigQuery example below, we want to compute the second-to-last order_updated event for each order.

To do this, we filter to keep only the rows WHERE order_status = 'order_updated'.

We use there rows then to retrieve the event occurring second - sorting decreasingly by event_ts and partitioning by order_id, using QUALIFY.

The output is then ORDER BY the second_to_last_order_update_ts decreasingly.

Input data: order events with order_id, event_ts and order_status for orders 1 and 2 on 2021-01-01; the order_updated rows are boxed and the second-to-last update of each order (11:30 for order 1, 13:15 for order 2) is highlighted.

SELECT
  order_id,
  event_ts AS second_to_last_order_update_ts,
  order_status

FROM input_data

WHERE order_status = 'order_updated'

QUALIFY ROW_NUMBER() OVER(PARTITION BY order_id ORDER BY event_ts DESC) = 2

ORDER BY second_to_last_order_update_ts DESC

BigQuery results: order 2 with second_to_last_order_update_ts 2021-01-01 13:15:00 UTC and order 1 with 2021-01-01 11:30:00 UTC, both order_updated.