DuckDB 2 cheatsheet

DuckDB 2: SQL features

9 common DuckDB 2 tasks.

Create a table from a file

Load a CSV into a table in one statement.

CREATE TABLE sales AS SELECT * FROM read_csv('sales.csv');

Inspect a table

Show column names and types.

DESCRIBE sales;

Summarize every column

Get min, max, quartiles, unique counts and null percentages for each column.

SUMMARIZE sales;

Group by all non-aggregated columns

Skip listing the grouping columns by hand.

SELECT region, product, sum(amount) AS total FROM sales GROUP BY ALL;

Filter on a window function

QUALIFY filters rows after window functions are calculated, without a subquery.

SELECT * FROM sales QUALIFY row_number() OVER (PARTITION BY region ORDER BY amount DESC) = 1;

Select all columns except some

Leave out columns without listing the rest.

SELECT * EXCLUDE (internal_id) FROM sales;

Pivot rows into columns

Turn category values into columns.

PIVOT sales ON region USING sum(amount) GROUP BY product;

Expand a list into rows

Turn each list element into its own row.

SELECT unnest([1, 2, 3]) AS n;

Take a random sample

Return about 10% of the rows.

SELECT * FROM sales USING SAMPLE 10%;

Was this page helpful?