Introduction to BigQuery
Matthew Forrest
Field CTO
SELECT
SUM(sales) AS total_sales,
AVG(quantity) AS avg_quantity,
MAX(price) AS max_price,
MIN(price) AS min_price
FROM sales_data;
| total_sales | avg_quantity | max_price | min_price |
|-------------|--------------|-----------|-----------|
| 50 | 8 | 100 | 2 |
SELECT
-- Later we will group by the 'order_id' column
order_id,
SUM(sales) AS order_total
FROM total_sales
-- Here we can GROUP and ORDER our query
GROUP BY order_id
ORDER BY order_total DESC;
| order_id | order_total |
|----------|-------------|
| 1 | 500 |
| 2 | 850 |
SELECT
category,
-- COUNT the number of rows returned
COUNT(order_id) AS record_count
FROM total_sales
GROUP BY category;
| category | record_count |
|-------------|--------------|
| shoes | 238 |
| electronics | 183 |
SELECT
category,
SUM(cost) AS total_cost,
AVG(cost) AS average_payment
FROM total_sales
GROUP BY category;
| category | total_cost | avg_cost |
|-------------|------------|----------|
| shoes | 10345 | 54 |
| electronics | 9340 | 34 |
SELECT
MIN(product_photos_qty) as min_photo_count,
MAX(product_photos_qty) as max_photo_count
FROM ecommerce.ecomm_products;
| min__photo_count | max__photo_count |
|----------|----------|
| 1 | 20 |
SELECT
category,
-- Counts only if the cost is over $500 grams
COUNTIF(cost > 500) AS large_items
FROM total_sales
GROUP BY category;
| category | large_items |
|-------------|-------------|
| shoes | 2 |
| electronics | 35 |
SELECT
category,
COUNT(order_id) as orders
FROM total_sales
-- Here we add the condition for item categories with an average cost of over $75
HAVING AVG(cost) > 75;
| category | orders |
|-------------|--------|
| shoes | 25 |
| electronics | 98 |
SELECT
order_id,
-- Will return an arbitrary category
ANY_VALUE(category) as cat
-- Returns the category with the highest cost
ANY_VALUE(category HAVING MAX cost) AS max_cat
FROM total_sales
GROUP BY order_id;
| order_id | cat | max_cat |
|----------|-----------|-------------|
| 1 | shoes | electronics |
| 2 | household | exercise |
Introduction to BigQuery