Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

DuckDB 5 min read How-to mediumAdept

DuckDB Tricks – Part 1

Five DuckDB SQL tricks for interactive use, built around a small example CSV and DuckDB's friendly SQL shorthands such as FROM-first syntax. Casting to DECIMAL(15, 3) pretty-prints floats while keeping them numeric, unlike printf. CREATE TABLE ... AS FROM ... LIMIT 0 copies a schema without data, and ORDER BY hash(rowid + 42) gives a deterministic shuffle (the offset matters because hash(0) is 0). The read_csv types argument overrides auto-detected column types, and COPY can rewrite a CSV in place, which a naive Unix shell redirect cannot do.

Assumes: basic SQL

Related posts

  • Ducks in a Column

    DuckDB mediumAdept

    DuckDB stores data by column and processes it in vectors of 2,048 values, which drives its compression, projection pushdown and aggregation speed. A tour built on a duck-sightings dataset shows querying CSV, Parquet, S3 and Hive-partitioned files in place (with zonemap row-group skipping and CSV type sniffing), column-level friendly SQL (EXCLUDE, REPLACE, COLUMNS() with regex or lambdas, FROM-first, STRUCT unpacking, SUMMARIZE), and aggregation shortcuts such as GROUP BY ALL, FILTER, arg_max, PIVOT/UNPIVOT and approximate aggregates. Window functions, QUALIFY, ASOF joins for aligning mismatched time series, and list aggregation round out the examples.

  • Even Friendlier SQL with DuckDB

    DuckDB mediumAdept

    DuckDB extends SQL syntax with features that simplify everyday queries and enable dynamic analysis. Additions include reusable column aliases within a SELECT, COLUMNS() for selecting and transforming columns by regex, EXCLUDE/REPLACE or lambda, automatic JSON-to-nested-type conversion with dot notation, FROM-first queries, dot-operator function chaining, UNION/INSERT BY NAME, and dynamic PIVOT/UNPIVOT that need no hardcoded column values. Each feature is shown next to the verbose standard-SQL equivalent it replaces.

  • Gems of DuckDB 2.0.0

    DuckDB mediumAdept

    DuckDB 2.0.0 LTS includes a set of smaller features beyond its headline items. The Python client gains an appender for buffered row inserts, though INSERT INTO ... SELECT remains faster for DataFrames and Arrow tables. The CLI adds pretty-printed .schema, a .manual function reference, calculator-style expressions and queryable command history via shell_history(). New SQL features include SHOW for settings, CREATE SECURE VIEW to hide statistics and plans, regexp_extract options, setval for sequences, session variables as prepared-statement defaults, custom PARTITION_PATH and ORDER_BY in COPY, GeoJSON writing, and extension-defined window functions.

  • The Journey to Zero-Copy: How chDB Became the Fastest SQL Engine on Pandas DataFrame

    ClickHouse mediumAdept

    chDB, ClickHouse embedded as a Python library, removed the four serialization steps that made every v1 query take over 100ms by reading Pandas DataFrames in place. That change alone made v2 87x faster than v1. The engineering work included automatic variable discovery via Python(df), batching CPython calls before parallel execution to avoid GIL contention, and reimplementing Python string-to-UTF-8 encoding in C++, which cut one ClickBench query from 8.6s to 0.56s. v4.0 adds zero-copy output by mapping ClickHouse columns directly to NumPy dtypes. In the post's own benchmarks, exporting 1M rows to a DataFrame took about 24% less time than DuckDB, and chDB beat Pandas at most operations on 1M and 10M rows.

  • Text2SQL using Hugging Face Dataset Viewer API and Motherduck DuckDB-NSQL-7B

    Hugging Face mediumAdept

    DuckDB-NSQL-7B, a Llama-2-7B model fine-tuned on general SQL and then DuckDB text-to-SQL pairs, can turn plain-language questions into DuckDB SQL that runs directly against Hugging Face datasets. The pipeline fetches a dataset's auto-converted parquet URL from the Dataset Viewer API and derives a CREATE TABLE schema by loading one row into DuckDB. It then prompts the quantized GGUF model through llama.cpp with the schema and question, rewrites the table name to the parquet URL, and executes the query in DuckDB. On the world-cities-geo dataset the generated query matches the results of the Viewer's search and filter APIs.

The quest for software engineering knowledge.

Look:
Layout: