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
SELECT o.id FROM orders o JOIN customers c ON c.customer_id = o.customer_id;SELECT o.id FROM orders o JOIN customers c ON c.customer_id = o.customer_id AND c.active;Correct
SELECT o.id FROM orders o JOIN customers c USING (customer_id);SELECT o.id FROM orders o JOIN customers c USING (customer_id) WHERE c.active;SELECT o.id FROM orders o JOIN customers c ON c.id = o.customer_id; -- different names: not in scopeConfigure 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
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.
No issues found.
2 rules enabled.
prefer-using-over-on — plus no-syntax-error as a safety net.