All rules

Style

postgresql/prefer-using-over-on

Prefer `JOIN ... USING (col)` over `JOIN ... ON a.col = b.col`.

  • Type suggestion
  • Recommended off
  • Fixable yes

Why this matters

When an `ON` condition matches a column to the same-named column on the other side (`a.col = b.col`, or `schema.table.col`), `USING` names the column once and merges the pair into a single output column. When every condition is such a pair, the rule rewrites the `ON` to `USING`. When there are other conditions too, it also moves them to the start of `WHERE`, but only for an inner join that no outer join encloses, where they keep the same rows, and not inside an aliased parenthesized join, which hides its tables from `WHERE`. The rewrite is offered as a suggestion instead of a fix where it can change the result: under a bare `SELECT *` / `RETURNING *` or an aliased parenthesized join, where merging the pair changes the output columns, and when a moved condition has an unqualified column that another `FROM` item or the `UPDATE` / `DELETE` target could also have. A join whose key compares differently named columns (`i.order_id = o.id`) keeps `ON`.

Examples

Incorrect

Incorrect
SELECT o.id FROM orders o JOIN customers c ON c.customer_id = o.customer_id;
Incorrect
SELECT o.id FROM orders o JOIN customers c ON c.customer_id = o.customer_id AND c.active;

Correct

Correct
SELECT o.id FROM orders o JOIN customers c USING (customer_id);
Correct
SELECT o.id FROM orders o JOIN customers c USING (customer_id) WHERE c.active;
Correct
SELECT o.id FROM orders o JOIN customers c ON c.id = o.customer_id; -- different names: not in scope

Configure it

// eslint.config.js
import postgresql from "eslint-plugin-postgresql";

export default [
  {
    files: ["**/*.sql"],
    languageOptions: {
      parser: postgresql.configs.recommended.languageOptions.parser,
    },
    plugins: { postgresql },
    rules: {
      "postgresql/prefer-using-over-on": [
        "warn",
        {
          checkJoinedSides: false,
        },
      ],
    },
  },
];

Options

checkJoinedSides boolean default: false
Also report joins where a side is itself a join, such as the second `JOIN` of a chain. `USING (col)` fails when two tables on one side both have `col`, and the rule can't see the schema, so these are off by default and only get a suggestion.
Try this rule

Edit the SQL — only prefer-using-over-on is enabled.

Pre-filled with the first incorrect example. Toggle off in the rule shelf to see how the diagnostic disappears.

0 errors 0 warnings parse 0ms · rules 0ms
Diagnostics

No issues found.

2 rules enabled.

Rule under test prefer-using-over-on — plus no-syntax-error as a safety net.
eslint-plugin-postgresql

An ESLint plugin that lints .sql files with real PostgreSQL grammar and a curated set of best-practice rules.

© 2026 eslint-plugin-postgresql contributors Built on libpg-query · PostgreSQL 17