effective-dbt-sql
できること
正しく、読みやすいdbtモデルのSQLを書くためのルールを示します。dbtのモデルや指標を書いたり変えたりするアナリティクスエンジニア向けです。
使う場面
- ✓dbtモデルの中にSQLを書こうとしているとき。
- ✓指標のSQLの定義を変えたいとき。
- ✓サブクエリを追加するか、既存のフィールドを再利用するかを決めているとき。
スキルのリンク
https://github.com/lightdash/lightdash/blob/main/skills/effective-dbt-sql/SKILL.mdコピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。
使い方
いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。
貼り付け用メッセージ
Claude Code、Codex、Cursorの場合:プロジェクトのフォルダで実行するとスキルがインストールされます。
SKILL.mdをダウンロードして、エージェントのスキル用フォルダに入れてください。
AIへの頼み方の例
“subscriptionsのdbtモデルに解約率の指標を追加してください。同じロジックを繰り返さず、既存のフィールドを再利用してください。”
毎週5つの新しいスキル。1分で読めます。
仕組み
スキルは、可能な限り既存のフィールドを再利用し、外側の各行に依存するサブクエリを避け、複雑なロジックにはCTEを使うようエージェントに指示します。また、リポジトリで既に使われている書き方に合わせるよう求めます。
技術的な内容(元のSKILL.md)
作者による元のテキスト(英語)をGitHubからそのまま表示しています。
Effective dbt SQL
Write dbt model SQL that is correct, readable, and idiomatic. This skill is about the semantics of the SQL you write — not naming or formatting.
Scope
Use this when you are about to write or change SQL inside a dbt model (.sql), or a metric/dimension whose value is defined by SQL.
This skill covers semantics only. It does not prescribe naming schemes, file layout, or indentation. For those, follow the rule below.
Match the target repo first. These are fallback defaults, not mandates. Before applying any rule, read the models around the one you are changing and match the repo's established conventions where they differ. Apply these rules to new or changed SQL only — do not reformat, rename, or restructure existing code that already works.
The rules
Reuse before you re-derive. If the value you are asked for is already expressed by an existing dimension or metric — or by two of them combined — compose those fields instead of writing new SQL to recompute it. Read the model's existing dimensions/metrics before adding one. See reuse-over-subqueries.
No correlated subqueries. Never compute a value with a subquery that references the outer query's rows (a per-row correlated subquery in
SELECTorWHERE). Reuse an existing field, or join to an aggregated CTE instead. This is the single most common mistake. See cte-pipelines.CTEs over nested subqueries. Structure a query as a pipeline of named CTEs, each referenced by the next, ending in a final
select. Prefer this over subqueries nested insidefrom/where. See cte-pipelines.Explicit joins. Always state the join type (
inner/left/…) and theoncondition. Never comma-join. Know the grain: a join that fans out rows silently changes every downstream aggregate.Explicit columns. Select named columns rather than
select *when adding or changing what a model emits, so a change to an upstream model can't silently alter this model's output. (A pass-throughselect * from finalat the very end of a CTE pipeline is fine — the columns are already pinned by the CTEs.)Don't break references. Before editing a model's SQL, read the
source()/ref()models it selects from. Do not rename or remove a column that downstream models orschema.ymlreference without checking the impact first.
Not in scope
- Emitted-type / casting safety (e.g. boolean-vs-numeric column types): follow the warehouse skill the system prompt points you to (
/home/user/.lightdash-skills/). This skill does not restate casting rules. - Naming and formatting conventions: match the target repo (see the rule above).
よくある質問
名前や書式も変えますか?
いいえ。スキルは名前や体裁ではなく、SQLの意味を扱います。
動いているコードを書き直しますか?
いいえ。スキルは新規または変更されたSQLにだけ適用し、動いているコードは書き直しません。
相関サブクエリとは何ですか?
外側のクエリの行を参照するサブクエリで、スキルは避けるよう定めています。
詳細
作者による元の説明
Use when writing or modifying dbt model SQL — deciding whether to add a subquery or reuse an existing dimension/metric, structuring a query, joining models, or refactoring a metric's SQL. Encodes SQL semantic-correctness rules: reuse existing fields, prefer CTEs over correlated subqueries, and make joins and column lists explicit.
repo: lightdash/lightdash
人気
- GitHubのスター
- 6.2k
- ランキング
- #14405
- ライセンス
- —
- 更新日
- 2026/11/2
コメント
- …
このリポジトリの他のスキル
breakup-pr
6.2k大きなプルリクエストを、一つずつレビューしてマージできる、小さく積み重ねたスライスに分割するのを支援します。
lightdash-agent-slack-messaging
6.2k証拠に基づいて、Lightdashの管理者向けに、わかりやすく親しみのある保守とアクティビティの報告を書くのを支援します。
developing-in-lightdash
6.2kLightdashのダッシュボードとグラフを、JSONとして読み、作成し、編集するのを支援します。各グラフの種類の設定を含みます。
filter-expressions
6.2kLightdashの指標クエリに、正しいフィルター式を書くのを支援します。カスタム指標へのフィルターも含みます。
似ているスキル
discrawl
394k保存済みのローカルなDiscordメッセージのアーカイブを検索して要約し、その新しさを確認し、ダイレクトメッセージを読めるようエージェントを支援します。
clickhouse-io
280kエージェントがClickHouseデータベースのクエリとスキーマを最適化できるようにします。高性能な分析処理向けのベストプラクティスを適用します。分析クエリが遅すぎるときに使います。
scientific-db-pubmed-database
280kエージェントが生物医学分野の科学論文を検索するのに役立ちます。特定の医療情報が必要な研究に便利です。