Skip to content
Queria Docs
Esc
↑↓navigate↵open⌘Jpreview
On this page

Connect from the DuckDB CLI

ATTACH a dataset in the DuckDB CLI and aggregate statistics by prefecture

This page connects to a dataset from the DuckDB CLI, looks at its tables and aggregates them, in three steps. The example uses the e-Stat statistics dataset (e_stat) to rank prefectures by population.

1. Connect

Load the DuckLake extension and ATTACH e_stat.

INSTALL ducklake;
LOAD ducklake;

ATTACH 'ducklake:https://data.queria.io/e_stat/ducklake.duckdb' AS e_stat (READ_ONLY);

2. Look at the tables

e_stat holds prefecture and municipality statistics by field, such as population, economy and labor. List the tables in the ssds schema.

SELECT schema, name FROM (SHOW ALL TABLES) WHERE database = 'e_stat' AND schema = 'ssds' LIMIT 20;

3. Rank prefectures by population

Rank prefectures by total population for fiscal year 2024. The item_name values (for example, A1101_総人口 — total population) join e-Stat’s classification code and indicator name with an underscore.

SELECT DISTINCT area_name, value AS population
FROM e_stat.ssds.a_pref_population
WHERE item_name = 'A1101_総人口'
  AND year = 2024
  AND area_name != '全国'
ORDER BY value DESC
LIMIT 10;

Tokyo, Kanagawa and Osaka come first. (Note: 全国 means “nationwide” and is excluded here.)

Compute a rate from two indicators

Put “labor force” and “unemployed persons” in separate columns with CASE, and compute the unemployment rate for each prefecture.

SELECT
  area_name,
  MAX(CASE WHEN item_name = 'F1101_労働力人口' THEN value END) AS labor_force,
  MAX(CASE WHEN item_name = 'F1107_完全失業者数' THEN value END) AS unemployed,
  ROUND(
    MAX(CASE WHEN item_name = 'F1107_完全失業者数' THEN value END)
    / MAX(CASE WHEN item_name = 'F1101_労働力人口' THEN value END) * 100,
    2
  ) AS unemployment_rate_pct
FROM e_stat.ssds.f_pref_labor
WHERE year = 2020 AND area_name != '全国'
GROUP BY area_name
ORDER BY unemployment_rate_pct DESC NULLS LAST
LIMIT 10;

Attach multiple datasets at once

Repeat ATTACH to use several datasets in one SQL query.

ATTACH 'ducklake:https://data.queria.io/reinfolib/ducklake.duckdb' AS reinfolib (READ_ONLY);
ATTACH 'ducklake:https://data.queria.io/zipcode/ducklake.duckdb' AS zipcode (READ_ONLY);

Find datasets and tables with SQL

Read the Queria API with read_json_auto to look up, inside DuckDB, which datasets hold which tables and columns. limit goes up to 200. When more results remain, has_more is true; raise offset to read the next page.

Here is the dataset list. Prefix catalog_url with ducklake: when you pass it to ATTACH.

SELECT name, title, 'ducklake:' || catalog_url AS attach_url, bytes, updated_at
FROM (SELECT unnest(datasets, recursive := true)
      FROM read_json_auto('https://queria.io/api/catalog/datasets?limit=200'))
ORDER BY bytes DESC;

Search tables and columns by keyword. type takes dataset, table, or column.

SELECT e.qualified_name, e.schema_name, e.table_name, e.column_name, e.title
FROM (SELECT unnest(entries) AS e
      FROM read_json_auto('https://queria.io/api/catalog/search?q=population&type=column&limit=200'));

Here are the tables in a single dataset.

SELECT t.schema, t.name, t.title, len(t.fields) AS fields
FROM (SELECT unnest(declaration.tables) AS t
      FROM read_json_auto('https://queria.io/api/catalog/datasets/queria/zipcode'))
WHERE t.published;

Here are a table’s columns, with their types and whether they allow NULL.

SELECT f.name, f.type, f.nullable, f.title
FROM (SELECT unnest(t.fields) AS f
      FROM (SELECT unnest(declaration.tables) AS t
            FROM read_json_auto('https://queria.io/api/catalog/datasets/queria/zipcode'))
      WHERE t.name = 'mart_zipcode');

Run once from the shell

Pass all the SQL to the -c option to get results without opening DuckDB’s interactive mode.

duckdb -c "
INSTALL ducklake;
LOAD ducklake;
ATTACH 'ducklake:https://data.queria.io/e_stat/ducklake.duckdb' AS e_stat (READ_ONLY);
SELECT DISTINCT area_name, value AS population
FROM e_stat.ssds.a_pref_population
WHERE item_name = 'A1101_総人口' AND year = 2024 AND area_name != '全国'
ORDER BY value DESC LIMIT 5;
"

Notes

  • With an older DuckDB, the ducklake extension is also older and may fail to read the data. Use 1.5.2 or later, for example via uvx duckdb
  • The httpfs extension that reads https:// data loads automatically the first time. Where automatic extension installation is turned off, run INSTALL httpfs; LOAD httpfs; first
  • For datasets with GEOMETRY columns (nlftp, the e_stat boundary data and so on), run INSTALL spatial; LOAD spatial;

Queria’s data is published in the standard DuckLake format. The ATTACH on this page works the same in any language or environment that runs DuckDB 1.5.2 or later.

Was this page helpful?