motherduck-query

Execute DuckDB SQL queries against MotherDuck databases for analytics and transformations.

Updated Oct 15, 2019
One-click install
npx skills add https://github.com/kkkaoru/dotfiles --skill motherduck-query-kkkaoru
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: motherduck-query
Source: https://github.com/kkkaoru/dotfiles/tree/main/.agents/skills-stroage/motherduck-query
Command: npx skills add https://github.com/kkkaoru/dotfiles --skill motherduck-query-kkkaoru

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Writing correct, performant SQL against MotherDuck requires DuckDB-specific syntax and patterns that differ from PostgreSQL, and mistakes like unqualified table names or misused window functions lead to wrong results or slow queries. This Skill guides the AI to write idiomatic DuckDB SQL with proper grain, filtering, and aggregation discipline. ## Core Features & Use Cases - DuckDB-Native Query Patterns: Enforces CTEs, GROUP BY ALL, QUALIFY, arg_max, FILTER, PIVOT/UNPIVOT, and fully qualified database.schema.table names. - Performance Optimization: Covers filter-early/aggregate-early strategy, EXPLAIN plan review, predicate pushdown, and pre-aggregated serving tables for repeated reads. - Safe Write Handling: Treats DDL, DML, ATTACH, and SHUTDOWN lifecycle commands as writes requiring explicit user confirmation via MCP query_rw. - Use Case: Ask for the top 5 products per category by revenue, and receive a QUALIFY-based window function query against your fully qualified MotherDuck tables, ready to run. ## Quick Start Ask the AI to write a DuckDB query that computes monthly revenue per region from your MotherDuck analytics database using fully qualified table names.

Frequently Asked Questions about motherduck-query

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write SQL queries against MotherDuck databases?

Write DuckDB SQL, not PostgreSQL SQL, and always use fully qualified database.schema.table names in double quotes. Structure queries with CTEs, filter and aggregate early, and use DuckDB features like GROUP BY ALL and QUALIFY for cleaner logic.

How to get the top N rows per group in DuckDB?

Use QUALIFY with a window function such as ROW_NUMBER() or RANK() partitioned by the group column and ordered by the ranking metric. For example, QUALIFY ROW_NUMBER() OVER (PARTITION BY category ORDER BY revenue DESC) <= 5 returns the top 5 per category.

Can I use PostgreSQL syntax with MotherDuck?

No, MotherDuck runs DuckDB SQL even when connecting through the PostgreSQL endpoint. PostgreSQL-specific syntax will fail or behave differently, so use DuckDB functions and patterns like arg_max, FILTER, and list comprehensions instead.

How do I filter window function results in DuckDB?

Use the QUALIFY clause instead of WHERE, since WHERE cannot reference window function results. QUALIFY evaluates after window functions, making it the correct tool for deduplication and top-N queries.

Why is my MotherDuck query slow on large tables?

Common causes include missing early filters, SELECT * in production queries, functions applied to filtered columns blocking pushdown, and repeated raw-table rescans. Use EXPLAIN to inspect the plan and materialize repeated queries into pre-aggregated serving tables.

When should I use SHUTDOWN on a MotherDuck Duckling?

Use SHUTDOWN only for explicit operational control, such as stopping a Duckling after batch or CI work completes. SHUTDOWN TERMINATE interrupts running queries and both commands require query_rw access with explicit user confirmation.