---
name: sql-join-helper
description: "Picks the right SQL JOIN type for two tables and writes the query for your database engine, with a plain explanation of which rows each side keeps. Use when a developer is unsure whether two tables need an inner, left, right or full join and wants the query written for their database. Do not use for tuning a slow query or designing table structure. Related topics: sql, data, coding."
---

# SQL JOIN Helper

Picks the right SQL JOIN type for two tables and writes the query for your database engine, with a plain explanation of which rows each side keeps.

## When to use

Use this skill when a developer is unsure whether two tables need an inner, left, right or full join and wants the query written for their database. Do not use it for tuning a slow query or designing table structure.

## Workflow

1. Ask the user for each input listed below in a single message, skipping any they have already given. Where options are listed, offer them, but accept other answers too.
2. Map the rows-to-keep choice to the join type and confirm the shared key is the join condition.
3. Write the query with aliases and explicit columns for the chosen engine.
4. Decide whether each filter belongs in ON or WHERE so outer-join rows are not silently dropped.
5. Explain which rows appear and where NULLs show up, noting any engine workaround for unsupported joins.

## Inputs

- **Table 1 name** (free text, e.g. "users")
- **Table 2 name** (free text, e.g. "orders")
- **Shared key** (free text, e.g. "users.id = orders.user_id")
- **Rows to keep** (choose one: All rows from both (full outer), Only matching rows (inner), All rows from Table 1 (left), All rows from Table 2 (right))
- **Extra filtering** (longer free text)
- **Database engine** (choose one: PostgreSQL, MySQL, SQL Server)

## Output

A SQL query in a code block for the chosen engine, followed by a short explanation of which rows are kept, where NULLs appear and why the filter is placed as it is.

## Prompt template

Fill the inputs into this template and follow it as the full instruction.

### SQL JOIN Picker

Choose the correct JOIN for two tables and write the query.

- Table 1 name: {{Table 1 name}}
- Table 2 name: {{Table 2 name}}
- Shared key: {{Shared key}}
- Rows to keep: {{Rows to keep}}
- Extra filtering: {{Extra filtering}}
- Database engine: {{Database engine}}

Write the query with the join type that matches the rows to keep, using explicit aliases and listing columns rather than selecting everything. Place filters carefully: a condition on the outer table in WHERE turns a left join into an inner join, so put it in the ON clause when the unmatched rows must survive. If the engine lacks the join type (MySQL has no FULL OUTER JOIN), give the equivalent UNION of left and right joins.

Return the query in a code block, then explain in a few sentences which rows appear, which columns come back as NULL, and why the filter sits where it does.

## Pitfalls

- Putting a filter on the outer table in WHERE, which quietly converts a left join to an inner join.
- Using FULL OUTER JOIN on MySQL, where it does not exist.
- Selecting every column and returning duplicate key names that confuse the application.
