BigQuery SQL Best Practices for Analysts: How to Reduce Query Cost
On-demand BigQuery queries are billed by the bytes they read. These practices, each checked against Google's documentation, show how to read fewer bytes and how to see the estimate before you run a query.
On this page · 21 sections
- How BigQuery bills a query
- BigQuery practices and what each one saves
- Select only the columns you need
- What LIMIT does and does not do
- Partition large tables and filter on the partitioning column
- Write filters that prune
- Require a partition filter
- Cluster tables on the columns you filter by
- Estimate the cost before you run a query
- Cap the bytes billed for a query
- Use approximate aggregation when an estimate is enough
- Save results you query repeatedly
- Join and filter advice from Google's optimization pages
- Applying these practices to dbt models on BigQuery
- Common mistakes
- Frequently Asked Questions
- How can query costs be minimized in Google BigQuery?
- Does LIMIT reduce the cost of a BigQuery query?
- How do partitioning and clustering make BigQuery queries more efficient?
- What are the best practices for structuring JOIN operations in BigQuery?
- Primary sources and further reading
- Topic
- BigQuery for Analytics Engineers
- Category
- SQL
BigQuery SQL best practices come down to making each query read less data. Under on-demand pricing, Google bills a query by the bytes it processes, so to reduce BigQuery query cost you select only the columns you need, filter on the partitioning column of partitioned tables, cluster large tables on the columns you filter by, and check the bytes estimate before you run anything. Each practice below cites the Google documentation page behind it.
All SQL here is BigQuery (GoogleSQL) syntax taken from Google's documentation on November 22, 2026. None of it was run. For related guides, see the BigQuery hub.
How BigQuery bills a query
BigQuery has two pricing models for queries.
- On-demand pricing. Queries are billed by the bytes read, per TiB (tebibyte) of logical, meaning uncompressed, data. Fewer bytes read means a lower charge.
- Capacity pricing. You pay for the compute resources, called slots, that process queries, set up through reservations. Google states that capacity costs follow the slot capacity provisioned over time, not the data a specific query scans, and that the exact cost of one query cannot be estimated before it runs.
This article quotes no rates: they vary by location and change, so check Google's pricing page. Queries answered from cached results are not charged.
BigQuery practices and what each one saves
| Practice | What it saves | Google documentation page |
|---|---|---|
| Select only the columns you need | Bytes in the columns you leave out | Optimize query computation |
| Filter on the partitioning column | Bytes in the pruned partitions | Query partitioned tables |
| Require a partition filter | Unfiltered scans of the whole table | Create partitioned tables |
| Filter on clustered columns | Bytes in the pruned storage blocks | Introduction to clustered tables |
| Dry run the query first | Nothing directly. It shows the bytes first | Run a query |
| Set maximum bytes billed | Oversized queries fail without a charge | Estimate and control costs |
| Use approximate aggregate functions | Time and resources, for an estimate | Best practices for functions |
| Use materialized views or staged tables | Repeated reads of large tables | Introduction to materialized views |
| Aggregate before joining | Rows the join has to process | Optimize query computation |
Select only the columns you need
BigQuery stores table data in columnar format: each column is stored separately, so a query can read one column without reading every field of every row. On-demand queries are charged for the data in the columns you select, so SELECT * reads, and bills, every column.
Name the columns you want. SELECT * EXCEPT leaves out the columns you list, which Google says can greatly reduce the data read.
-- BigQuery (GoogleSQL). Adapted from Google's documentation; not run.
SELECT transaction_id, transaction_amount
FROM mydataset.transactions
WHERE transaction_date >= '2026-01-01';
SELECT * EXCEPT (customer_id)
FROM mydataset.transactions
WHERE transaction_date >= '2026-01-01';
To look at a table, use the console's Preview tab or the bq head command, which show rows at no charge. For external tables stored as Parquet or ORC, the bytes charged are likewise limited to the columns read.
What LIMIT does and does not do
LIMIT caps the rows a query returns. On a table that is not clustered, it does not change the data read: Google states that you are billed for all the bytes the query reads, even though only some rows come back. On a clustered table, LIMIT can reduce the bytes scanned, because scanning stops once enough blocks have been read. Use it to limit output, not cost.
Partition large tables and filter on the partitioning column
A partitioned table is divided into segments, called partitions, by one partitioning column: a date, timestamp, or datetime column, the ingestion time, or an integer range. When a query filters on that column, BigQuery scans the matching partitions and skips the rest. This is partition pruning, and pruned partitions are not counted in the bytes scanned.
This table is partitioned by day, clustered by customer, and rejects queries that have no partition filter:
-- BigQuery (GoogleSQL). Built from Google's documented CREATE TABLE examples; not run.
CREATE TABLE mydataset.transactions
(
transaction_id INT64,
customer_id STRING,
transaction_date DATE,
transaction_amount NUMERIC
)
PARTITION BY transaction_date
CLUSTER BY customer_id
OPTIONS (
require_partition_filter = TRUE
);
Write filters that prune
Pruning needs a filter BigQuery can evaluate without reading table data:
-- BigQuery (GoogleSQL). WHERE clauses adapted from Google's documentation; not run.
-- Prunes: compared with constants
WHERE transaction_date >= '2026-01-01' AND transaction_date < '2026-02-01'
-- Still prunes: a second condition joined by AND
WHERE customer_id = 'C1001' AND transaction_date >= '2026-01-01'
-- Does not prune: a subquery value is not constant
WHERE transaction_date = (SELECT MAX(transaction_date) FROM mydataset.staging_transactions)
-- Does not prune: with OR, any partition can still qualify
WHERE transaction_date >= '2026-01-01' OR customer_id = 'C1001'
Keep the partitioning column alone on one side of the comparison. A few functions, including DATE_TRUNC, DATE_ADD, and DATE_SUB, still allow pruning when their other arguments are constant. Other functions on the column cause a full table scan.
Require a partition filter
With require_partition_filter = TRUE, a query without a qualifying filter fails with an error. At least one condition must reference only the partitioning column, so the OR filter above is rejected. The rule also covers views and materialized views on the table.
To choose a partitioning column, see partitioning strategies in Snowflake and BigQuery.
Cluster tables on the columns you filter by
A clustered table sorts its data into storage blocks by the values of up to four clustered columns. A query that filters or aggregates on those columns scans only the relevant blocks, which Google calls block pruning.
Google calls clustering generally a good first option and says tables or partitions larger than 64 MB are likely to benefit, especially when queries filter on columns with many distinct values. Filter on the first clustered column, alone or with the ones after it. Wrapping a clustered column in a function, or comparing it with another column, prevents block pruning.
The trade-off is predictability: the blocks to scan are not known until the query runs, so the estimate is an upper bound. Google suggests partitioning as well when you need a strict estimate beforehand, or when partitions would average at least 10 GB.
Estimate the cost before you run a query
BigQuery reports the bytes a query will process before you run it. In the console's query editor, the query validator shows the estimate for any valid query. A dry run returns an estimate from the command line or the API, and Google states that dry runs are not charged.
# bq command-line tool. Adapted from Google's documentation; not run.
bq query \
--use_legacy_sql=false \
--dry_run \
'SELECT transaction_id, transaction_amount
FROM mydataset.transactions
WHERE transaction_date >= "2026-01-01"'
The estimate is an upper bound, and it shows whether a partition filter pruned.
Cap the bytes billed for a query
The maximum bytes billed setting puts a ceiling on one on-demand query. If the estimate is over the limit, the query fails without a charge. Set it in the console's query settings, or replace --dry_run above with a flag such as --maximum_bytes_billed=1000000000. A query on a clustered table can fail this check even when the bytes billed would have been under the limit. Custom quotas set a daily cap for a project or a user.
Use approximate aggregation when an estimate is enough
APPROX_COUNT_DISTINCT(customer_id) estimates COUNT(DISTINCT customer_id), and APPROX_QUANTILES returns approximate quantile boundaries. Google's reference states the trade-off: these functions typically use fewer resources and perform better on very large inputs, but they return a statistical estimate whose precision you cannot set. Google describes the gain as speed and resource use and does not claim fewer bytes billed. Keep exact counts for figures that must reconcile, such as finance reports.
Save results you query repeatedly
- Materialized views. A materialized view is a precomputed view whose stored results BigQuery refreshes in the background when the base tables change. BigQuery can also reroute a query on the base table to the view, which Google calls smart tuning. You still pay to query, refresh, and store it, and the SQL allowed in one is restricted.
- Staged tables. Google recommends breaking a large multi-stage query into stages that each write to a destination table, then querying the smaller table.
- Not common table expressions. Google warns that a
WITHclause is for readability, not performance. A common table expression (CTE) referenced several times may be evaluated each time.
Join and filter advice from Google's optimization pages
- Aggregate before you join. Reduce the rows with
GROUP BYfirst, so the join processes less data. - Put the largest table first, then the smallest, then the rest by decreasing size. A large table followed by a small one gives a broadcast join, which sends the small table to every slot processing the large one.
- Join on INT64 keys where you can. Google says they compare faster and cost less than STRING keys.
- Replace self-joins with a window function or the
PIVOToperator, and avoid joins that output more rows than they take in. - Filter on simple types. Operations on BOOL, INT64, FLOAT64, and DATE columns are typically faster than on STRING or BYTES columns.
For advice that applies to any database, see query optimization basics, and practice in the SQL hub. For table design, see data warehouse schema design.
Applying these practices to dbt models on BigQuery
In dbt, models create the tables, so partitioning and clustering are set in each model's configuration. The dbt documentation for BigQuery names the settings: partition_by, a dictionary with field, data_type, and granularity; cluster_by, one column name or a list; and require_partition_filter, which also affects other models and tests that select from the model.
-- dbt model configuration for BigQuery, as named in the dbt documentation; not run.
{{ config(
materialized = 'table',
partition_by = {
"field": "transaction_date",
"data_type": "date",
"granularity": "day"
},
cluster_by = "customer_id",
require_partition_filter = true
) }}
dbt's page repeats the rule that pruning needs literal values, not a subquery. For incremental models, which transform only new or changed rows on each run, dbt lists three strategies on BigQuery: merge (the default), insert_overwrite, and microbatch. It documents that merge scans the destination table as well as the source tables, and that insert_overwrite replaces whole partitions and requires partition_by. The dbt platform's BigQuery connection also has an optional Maximum bytes billed field, and dbt queries over it fail.
Common mistakes
- Using SELECT * with LIMIT to look at a table. On a table that is not clustered it reads the whole table.
- Filtering a partitioned table with a subquery value or with OR. Neither prunes.
- Wrapping a partitioning or clustered column in a function. Most functions stop pruning.
- Expecting byte savings to lower a capacity bill directly. Capacity pricing charges for slots over time.
Frequently Asked Questions
How can query costs be minimized in Google BigQuery?
Under on-demand pricing, read fewer bytes. Select only the columns you need, filter on the partitioning column, and cluster large tables on the columns you filter by. Check the estimate with a dry run, and set maximum bytes billed so an oversized query fails before it is charged.
Does LIMIT reduce the cost of a BigQuery query?
Not on a table that is not clustered. Google's documentation says you are billed for all the bytes the query reads, even though only some rows are returned. On a clustered table, LIMIT can reduce the bytes scanned.
How do partitioning and clustering make BigQuery queries more efficient?
Partitioning divides a table into segments by one column, and a filter on that column lets BigQuery skip whole partitions. Clustering sorts data into blocks by up to four columns, and a filter on those columns lets BigQuery skip blocks. A table can use both.
What are the best practices for structuring JOIN operations in BigQuery?
Google's guidance is to aggregate before the join, place the largest table first, join on INT64 keys instead of STRING keys where possible, and replace self-joins with window functions.
Primary sources and further reading
- Optimize query computation, Google Cloud. Column selection,
SELECT * EXCEPT, joins, filters, CTEs. - Estimate and control costs, Google Cloud. Pricing models, query validator, maximum bytes billed,
LIMIT, previews, staged results. - Run a query, Google Cloud. Dry runs.
- Overview of BigQuery storage, Google Cloud. Columnar storage.
- Introduction to partitioned tables, Query partitioned tables, and Create partitioned tables, Google Cloud. Partition types, pruning rules,
require_partition_filter. - Introduction to clustered tables and Query clustered tables, Google Cloud. Block pruning, when to cluster.
- Data definition language statements, GoogleSQL reference.
CREATE TABLEsyntax. - Best practices for functions and Approximate aggregate functions, Google Cloud. Approximate functions and their trade-off.
- Introduction to materialized views and Create materialized views, Google Cloud. Refresh, smart tuning, costs, restrictions.
- Use cached query results, Google Cloud. The query cache.
- BigQuery pricing, Google Cloud. Current rates, and charging by the columns selected.
- BigQuery configurations and Connect BigQuery, dbt documentation. Partition, cluster, and incremental settings, Maximum bytes billed.
These pages were read on November 22, 2026. No statement in this article was run. Prices and product behavior change.
- Fundamentals
Parquet in BigQuery: How to Load, Query, and Optimize Parquet Files
Explore how to efficiently store and query Parquet data in BigQuery. Learn best practices to optimize performance and reduce storage costs significantly.
- SQL
SQL QUALIFY Clause: Filter Window Functions Without Subqueries
QUALIFY lets you filter window-function outputs inline, avoiding extra subqueries and CTEs. Learn syntax, patterns, performance tips, and cross-database equivalents.
- SQL
BigQuery SQL Best Practices for Analysts: How to Reduce Query Cost
On-demand BigQuery queries are billed by the bytes they read. These practices, each checked against Google's documentation, show how to read fewer bytes and how to see the estimate before you run a query.
Drill it in the exercise library.
Portfolio-ready builds on this topic.
- intermediate · open →
SQL Alien Invasion Challenge: Defend Earth
Crisis-response analytics: defend Earth with multi-table joins, aggregation, and CTEs.
- advanced · open →
SQL Mystery Challenge: The Case of the Vanishing Artifacts
Investigative SQL: follow the evidence across museum audit logs to unmask a thief.
