BigQuery / DuckDB — Data Engineering

BigQuery and DuckDB are both columnar. DuckDB runs inside your program with no server (pip install duckdb, version 1.5.6), reads Parquet and CSV directly…

An analyst at a book marketplace wants a peek at the sales table in BigQuery, so she runs SELECT * FROM sales LIMIT 10. The table has 1 billion rows, and the bill isn't for 10 rows but for the whole table. On a laptop, the same query through DuckDB is free. BigQuery and DuckDB are both analytical SQL engines that store data by column, but they live in opposite places. BigQuery is a warehouse in Google's cloud: there is no server for you to run, and you pay per byte of data your queries read. DuckDB is a library that runs inside your own program, on your laptop, and reads Parquet and CSV files directly. It's a bit like laundry. BigQuery is the laundromat that charges by the kilo: hand it over, the machines aren't your problem, you pay per kilo. DuckDB is the washing machine at home: free every time you use it, but a load is only as big as your machine.

Two columnar engines, two places

DuckDB BigQuery
Where it runs inside your Python, Node or CLI process (in-process) on Google's servers (serverless: the servers exist, they just aren't your problem)
Getting started pip install duckdb a Google Cloud account, a project, a dataset
Data Parquet, CSV, JSON files on disk or in S3, DuckDB's own tables BigQuery tables (whose storage you also pay for)
Cost free, open source per byte your queries read, plus storage
Scale one machine: up to hundreds of GB is still comfortable terabytes to petabytes, many people at once

DuckDB's main advantage over a cloud warehouse: it runs locally with no server, so it suits exploring data on a developer's laptop, testing pipelines, and scripts that process files. Every DuckDB output in this lesson comes from DuckDB 1.5.6 (the latest release on PyPI as of 11 October 2026; DuckDB 2.0.0 came out on 21 October 2026).

DuckDB: the owner's question on a laptop

The data is order_lines.csv and books.csv from the pandas lesson. This script writes a sales.parquet file (18 clean rows, without a week column), then answers the owner's question straight from that file:

import duckdb

print(duckdb.__version__)

# 1. The extract as a Parquet file: 18 clean rows, no server
duckdb.sql("""
    COPY (
        SELECT DISTINCT r.order_id, r.book_id,
               CAST(r.created_at AS DATE) AS order_date,
               COALESCE(r.city, 'Unknown') AS city,
               r.title AS title,
               r.qty,
               b.catalog_price AS price
        FROM 'order_lines.csv' r
        JOIN 'books.csv' b USING (book_id)
    ) TO 'sales.parquet' (FORMAT parquet)
""")

# 2. The owner's question, straight from the Parquet file
duckdb.sql("""
    SELECT date_trunc('week', order_date)::DATE AS week, city, title, SUM(qty) AS sold
    FROM 'sales.parquet'
    WHERE order_date BETWEEN '2026-09-28' AND '2026-10-11'
    GROUP BY week, city, title
    QUALIFY RANK() OVER (PARTITION BY week, city ORDER BY sold DESC) = 1
    ORDER BY week, city, title
""").show()
1.5.6
┌────────────┬────────────┬───────────────────────┬────────┐
│    week    │    city    │         title         │  sold  │
│    date    │  varchar   │        varchar        │ int128 │
├────────────┼────────────┼───────────────────────┼────────┤
│ 2026-09-28 │ Bristol    │ This Earth of Mankind │      2 │
│ 2026-09-28 │ London     │ The Rainbow Troops    │      3 │
│ 2026-09-28 │ Manchester │ Beauty Is a Wound     │      2 │
│ 2026-09-28 │ Unknown    │ The Rainbow Troops    │      1 │
│ 2026-10-05 │ Bristol    │ The Dancer            │      2 │
│ 2026-10-05 │ London     │ Beauty Is a Wound     │      2 │
│ 2026-10-05 │ Manchester │ The Dancer            │      1 │
│ 2026-10-05 │ Manchester │ The Rainbow Troops    │      1 │
│ 2026-10-05 │ Unknown    │ This Earth of Mankind │      1 │
└────────────┴────────────┴───────────────────────┴────────┘

BigQuery: paying per byte read

In BigQuery you don't rent a server. Each query is priced on the bytes in the columns it reads, at the on-demand price of US$6.25 per TiB (1 TiB = 1,024 GiB), and the first 1 TiB each month is free. The first 10 GiB of storage each month is free too. These figures come from the official cloud.google.com/bigquery/pricing page as of 11 October 2026, for the us-central1 (Iowa) region; other regions can cost differently. More rules from the same page:

The size of each value is fixed as well: INT64 and DATE are 8 bytes, STRING is 2 bytes plus the length of the text. For the sales table from the previous lesson:

Column Type Bytes for 18 rows
order_id, book_id, qty, price INT64 144 per column
order_date, week DATE 144 per column
city STRING 167
title STRING 339

SELECT * reads 1,370 bytes, the owner's question (order_date, city, title, qty) 794 bytes; both are billed at the 10 MB minimum, and both are free while you stay under 1 TiB a month. The difference only shows on a big table. Say a book marketplace with 1 billion sales rows of 75 bytes on average over 3 years (1,096 days), and a dashboard that runs its query every 5 minutes (8,640 queries a month):

Query Read per query Cost per query A month, after the free 1 TiB
SELECT * 69.95 GiB US$0.43 US$3,682.68
the 4 columns needed 40.15 GiB US$0.25 US$2,111.06
4 columns, last 2 weeks, partitioned table 0.51 GiB US$0.0031 US$20.84

Partitions and clusters: cutting bytes

CREATE TABLE `bookshop.warehouse.sales` (
  order_id INT64, book_id INT64,
  order_date DATE, week DATE,
  city STRING, title STRING,
  qty INT64, price INT64
)
PARTITION BY order_date
CLUSTER BY city;

One question, three dialects

SQL in each engine is similar, but not the same. The owner's question in three places:

SQLite (the Playground and the Lab) DuckDB BigQuery
The week's Monday date(order_date, 'weekday 0', '-6 days') date_trunc('week', order_date)::DATE DATE_TRUNC(order_date, WEEK(MONDAY))
Filtering on RANK() a CTE, then WHERE in the outer query QUALIFY QUALIFY
Table name sales 'sales.parquet' `bookshop.warehouse.sales`

In BigQuery the arguments come in the opposite order to DuckDB, and WEEK without (MONDAY) starts on Sunday. SQLite's date(...) is shorthand for strftime('%Y-%m-%d', ...). The BigQuery version (not run in this lesson, since it needs a Google Cloud account):

SELECT DATE_TRUNC(order_date, WEEK(MONDAY)) AS week, city, title, SUM(qty) AS sold
FROM `bookshop.warehouse.sales`
WHERE order_date BETWEEN '2026-09-28' AND '2026-10-11'
GROUP BY week, city, title
QUALIFY RANK() OVER (PARTITION BY DATE_TRUNC(order_date, WEEK(MONDAY)), city ORDER BY SUM(qty) DESC) = 1
ORDER BY week, city, title;

The SQLite version is in the Lab, and its answer is the same: 9 rows.

Which one when

⚠️ The bill, partitions and dates that slip

TL;DR: BigQuery and DuckDB are both columnar. DuckDB runs inside your program with no server (pip install duckdb, version 1.5.6), reads Parquet and CSV directly (FROM 'sales.parquet'), costs nothing, and suits exploring on a laptop. BigQuery is a serverless cloud warehouse: US$6.25 per TiB read (us-central1, as of October 2026), the first 1 TiB each month free, a 10 MB minimum per table, and LIMIT doesn't lower the bill. Save bytes by naming the columns you need and filtering a partitioned table (PARTITION BY order_date, CLUSTER BY city): a dashboard on 1 billion rows drops from US$3,682.68 to US$20.84 a month. The dialects differ: the week's Monday is date(order_date, 'weekday 0', '-6 days') in SQLite, date_trunc('week', order_date) in DuckDB, DATE_TRUNC(order_date, WEEK(MONDAY)) in BigQuery; DuckDB and BigQuery have QUALIFY, SQLite needs a CTE.

⚡ DuckDB: no server

Meaning: DuckDB runs inside your own program (pip install duckdb), reads Parquet and CSV directly, and costs nothing. It suits exploring data on a developer's laptop. BigQuery is a cloud warehouse with no server for you to run, and you pay per byte read.

⚡ A file read like a table

Write:

SELECT date_trunc('week', order_date)::DATE AS week, city, title, SUM(qty) AS sold
FROM 'sales.parquet'
WHERE order_date BETWEEN '2026-09-28' AND '2026-10-11'
GROUP BY week, city, title
QUALIFY RANK() OVER (PARTITION BY week, city ORDER BY sold DESC) = 1
ORDER BY week, city, title

Meaning: A file name is written like a table name. QUALIFY filters a window function's result in the same query, with no CTE. The result is 9 rows, as in the SQL lesson.

⚡ LIMIT doesn't cut the bill

Guess first: in BigQuery, SELECT * FROM sales LIMIT 10 on a 1 billion row table. Is the bill for 10 rows or the whole table?

The whole table: every column in every row, 69.95 GiB, about US$0.43 per query. To peek, use the free Preview tab.

Meaning: BigQuery bills per column the query names. Name only the columns you need.

⚡ Partitions by date

Write:

PARTITION BY order_date
CLUSTER BY city;

Meaning: A query with WHERE order_date BETWEEN '2026-09-28' AND '2026-10-11' reads only 14 partitions, not 1,096. Without a filter on the partition column, every partition is still read.

What you will learn