Why this matters
`ORDER BY 1, 2` silently changes meaning when the SELECT list is reordered or a column is inserted. Worse, the intent is invisible at any callsite that consumes the query via a view or CTE. Reference columns by name or by an alias.
Examples
Incorrect
SELECT id, name FROM users ORDER BY 1;SELECT id, name, email FROM users ORDER BY 1, 2 DESC;Correct
SELECT id, name FROM users ORDER BY name;SELECT id, name AS display_name FROM users ORDER BY display_name;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/no-order-by-ordinal": "warn",
},
},
]; Options
Edit the SQL — only no-order-by-ordinal is enabled.
Pre-filled with the first incorrect example. Toggle off in the rule shelf to see how the diagnostic disappears.
Diagnostics
No issues found.
2 rules enabled.
Rule under test
no-order-by-ordinal — plus no-syntax-error as a safety net.