---
title: Connect from the DuckDB CLI
description: ATTACH a dataset in the DuckDB CLI and aggregate statistics by prefecture
sidebar:
  label: DuckDB CLI
---

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`.

```sql
INSTALL ducklake;
LOAD ducklake;

ATTACH 'ducklake:https://data.queria.io/e_stat/metadata.db' 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.

```sql
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.

```sql
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.

```sql
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.

```sql
ATTACH 'ducklake:https://data.queria.io/reinfolib/metadata.db' AS reinfolib (READ_ONLY);
ATTACH 'ducklake:https://data.queria.io/zipcode/metadata.db' AS zipcode (READ_ONLY);
```

## Read an earlier version

Add `?v=<version ID>` to the `metadata.db` URL to read that version. `?v=` also takes the name of a tag the publisher put on a version. A dataset's tags are the entries starting with `refs/tags/` in `https://data.queria.io/datasets/<handle>/<dataset>/refs.json`. Find version IDs in the "History" tab of the dataset page, or with `queria dataset versions <dataset>`.

```sql
ATTACH 'ducklake:https://data.queria.io/calendar/metadata.db?v=01a1b2b4-954f-7d0b-ad59-ae26765e29a6' AS calendar_old (READ_ONLY);
```

A URL without `?v=` always reads the latest version.

## 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`.

```sql
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`.

```sql
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.

```sql
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.

```sql
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.

```bash
duckdb -c "
INSTALL ducklake;
LOAD ducklake;
ATTACH 'ducklake:https://data.queria.io/e_stat/metadata.db' 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.

:::note
With the [Queria CLI](/en/cli), you can find datasets, run SQL and save results to files without writing `ATTACH`.
:::
