Use the WHERE clause to return only rows that meet specified conditions, improving speed and reducing cost.
Filtering trims scanned bytes, speeds queries, and delivers just the rows you need for analysis or dashboards—ideal for cost-aware, high-volume ecommerce workloads.
Write SELECT columns FROM dataset.table WHERE condition. Combine conditions with AND/OR, group with parentheses, or negate with NOT.
Use equality operators: WHERE name = 'Alice'
or WHERE product_id = 42
.
Compare dates/timestamps: WHERE order_date >= DATE_SUB(CURRENT_DATE(), INTERVAL 7 DAY)
. Works well for rolling-window revenue checks.
Yes, with IN
: WHERE status IN ('SHIPPED','DELIVERED')
. BigQuery rewrites it efficiently.
Use BETWEEN for readability: WHERE total_amount BETWEEN 100 AND 500
. Equivalent to two comparisons joined with AND.
Use IS NULL
or IS NOT NULL
. Equality operators skip NULL because three-valued logic treats comparisons as unknown.
Qualify tables with project.dataset.table, prefer DATE/TIMESTAMP literals over strings, and cast once—avoid functions on columns inside WHERE to keep filters sargable.
1. Comparing different data types: WHERE created_at = '2024-06-01'
forces a cast and can fail. Fix by using DATE or TIMESTAMP literals.
2. Forgetting to quote strings: unquoted text is parsed as identifiers and errors out. Always quote string literals in single quotes.
Use WHERE LOWER(name) = 'alice'
or compare against COLLATE NOCASE
if available. Be aware that functions on columns can prevent index usage.
Use UNNEST to flatten: WHERE 42 IN UNNEST(product_ids)
. The IN predicate checks membership efficiently.
Yes. In scripts, declare variables (DECLARE min_date DATE) and reference them: WHERE order_date >= min_date
. External tools can pass parameters via the API.