effective-dbt-sql
What it does
This skill gives rules for writing dbt model SQL that is correct and easy to read. It is for analytics engineers who write or change dbt models and metrics.
When to use it
- ✓When you are about to write SQL inside a dbt model.
- ✓When you want to change a metric's SQL definition.
- ✓When you are deciding whether to add a subquery or reuse an existing field.
Skill link
https://github.com/lightdash/lightdash/blob/main/skills/effective-dbt-sql/SKILL.mdCopy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.
How to use
The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.
Ready-to-paste message
For Claude Code, Codex or Cursor: run it in your project folder and the skill gets installed.
Download the SKILL.md and drop it into your agent’s skills folder.
Example of what to ask your AI
“Add a churn rate metric to the subscriptions dbt model, reusing the fields we already have instead of repeating the logic.”
5 new skills a week. 1 minute to read.
How it works
The skill tells the agent to reuse existing fields when possible, avoid subqueries that depend on each outer row, and prefer CTEs for complex logic. It also asks the agent to match the style already used in the repository.
Technical content (original SKILL.md)
Original text by the author, straight from 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).
FAQ
Does it change naming or formatting?
No, the skill covers the meaning of the SQL, not names or layout.
Will it rewrite existing code?
No, the skill applies to new or changed SQL only and does not rewrite working code.
What is a correlated subquery?
It is a subquery that refers to the rows of the outer query, and the skill says to avoid it.
Details
Original description by the author
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
Popularity
- GitHub stars
- 6.2k
- Ranking position
- #14405
- License
- —
- Updated
- 11/2/2026
Comments
- …
More skills from this repository
breakup-pr
6.2kHelps your agent split a large pull request into smaller, stacked slices that can be reviewed and merged one at a time.
lightdash-agent-slack-messaging
6.2kHelps your agent write clear, friendly maintenance and activity reports for Lightdash administrators, based only on the evidence it has.
developing-in-lightdash
6.2kHelps your agent read, create and edit Lightdash dashboards and charts as JSON, including each chart type's settings.
filter-expressions
6.2kHelps your agent write correct filter expressions for Lightdash metric queries, including filters on custom metrics.
Similar skills
discrawl
394kLets your agent search and summarize a saved local archive of Discord messages, check how fresh it is, and read direct messages.
clickhouse-io
280kHelps your agent optimize queries and schemas in ClickHouse databases. It applies best practices for high-performance analytical workloads. Use when analytical queries are too slow.
scientific-db-pubmed-database
280kHelps your agent search for scientific articles in the biomedical field. It's useful for research needing specific medical information.