SELECT retrieves columns from one or more BigQuery tables and can filter, sort, aggregate, and join data.
SELECT lets you pull specific columns, calculated expressions, or aggregated results from BigQuery tables. It supports WHERE, GROUP BY, HAVING, ORDER BY, LIMIT, JOIN, and subqueries, enabling detailed analysis without exporting data.
Start with SELECT column_list FROM dataset.table;
. Replace column_list
with * for all columns or with a comma-separated list of columns you need. Qualify tables with dataset names to avoid ambiguity.
Use WHERE
to limit results. Combine conditions with AND/OR and use parameterized queries for safety and caching.
SELECT id, name
FROM ecommerce.Customers
WHERE created_at > DATE_SUB(CURRENT_DATE(), INTERVAL 30 DAY);
ORDER BY sorts rows; LIMIT returns the first N rows. Combine both to paginate or preview data.
SELECT id, total_amount
FROM ecommerce.Orders
ORDER BY total_amount DESC
LIMIT 10;
INNER, LEFT, RIGHT, and FULL JOINs combine data across tables. Always alias tables to keep queries readable and prevent name clashes.
SELECT c.name, o.order_date, o.total_amount
FROM ecommerce.Customers AS c
JOIN ecommerce.Orders AS o
ON c.id = o.customer_id;
Use aggregate functions like COUNT, SUM, AVG, MIN, and MAX with GROUP BY. HAVING filters aggregated results.
SELECT customer_id, SUM(total_amount) AS lifetime_value
FROM ecommerce.Orders
GROUP BY customer_id
HAVING lifetime_value > 500;
Qualify table names with project.dataset.table to avoid accidental cross-project scans. Use partition filters to minimize read cost. Prefer previewing columns in the UI before querying large tables.
Yes. Place a SELECT inside FROM or WITH clauses for complex transformations.
Use @param syntax in the Cloud Console or client libraries to pass values safely and enable cache reuse.