Why is the ORDER BY clause expensive in BigQuery?

Quick Overview

Asks why the ORDER BY clause is expensive in BigQuery. Tests how well you understand distributed query execution, where a global sort fits into it, and how to write queries that need less sorting on large tables.

Why is the ORDER BY clause expensive in BigQuery?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Why is the `ORDER BY` clause expensive in BigQuery? Explain what the query engine has to do to return a sorted result, and what that means for runtime and failures on large tables. ```hint Parallel stages Filters and aggregations split cleanly across many workers. Ask what a single total ordering of the whole result requires from the last stage of the query. ``` ### What a Strong Answer Covers - Why a global sort cannot be finished in parallel, and where the work ends up - What happens to memory when the sorted result is large, and the failure it causes - How adding `LIMIT` changes the work the engine does - Where in a query a sort is worth doing, and when a partitioned window avoids a global order - Whether the sort shows up in bytes billed or only in compute time ### Follow-up Questions - Does adding `ORDER BY ... LIMIT 100` reduce the bytes billed under on-demand pricing? Why or why not? - `ROW_NUMBER() OVER (ORDER BY event_ts)` has no `PARTITION BY`. Does it avoid the problem? - You need a 500 GB table exported in timestamp order. How would you produce it without one giant sort? - How would you confirm that the sort is the slow part of a query?

Overview: Asks why the ORDER BY clause is expensive in BigQuery. Tests how well you understand distributed query execution, where a global sort fits into it, and how to write queries that need less sorting on large tables.

General logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Why is the ORDER BY clause expensive in BigQuery?

Explain what the query engine has to do to return a sorted result, and what that means for runtime and failures on large tables.

What a Strong Answer Covers Guidance

  • Why a global sort cannot be finished in parallel, and where the work ends up
  • What happens to memory when the sorted result is large, and the failure it causes
  • How adding LIMIT changes the work the engine does
  • Where in a query a sort is worth doing, and when a partitioned window avoids a global order
  • Whether the sort shows up in bytes billed or only in compute time

Follow-up Questions Guidance

  • Does adding ORDER BY ... LIMIT 100 reduce the bytes billed under on-demand pricing? Why or why not?
  • ROW_NUMBER() OVER (ORDER BY event_ts) has no PARTITION BY . Does it avoid the problem?
  • You need a 500 GB table exported in timestamp order. How would you produce it without one giant sort?
  • How would you confirm that the sort is the slow part of a query?