All rules

Style

postgresql/prefer-coalesce-over-case

Flag `CASE WHEN x IS NULL THEN y ELSE x END` and recommend `COALESCE(x, y)`.

  • Type suggestion
  • Recommended warn
  • Fixable no

Why this matters

`COALESCE` is shorter, evaluates `x` once, and is the form every PostgreSQL planner optimizes directly. Also catches the mirrored `IS NOT NULL` form. Multi-arm CASEs that go beyond a single null-fallback are not flagged.

Examples

Incorrect

Incorrect
SELECT CASE WHEN nickname IS NULL THEN full_name ELSE nickname END FROM users;
Incorrect
SELECT CASE WHEN nickname IS NOT NULL THEN nickname ELSE full_name END FROM users;

Correct

Correct
SELECT COALESCE(nickname, full_name) FROM users;

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-coalesce-over-case": "warn",
    },
  },
];

Options

This rule has no options.

Try this rule

Edit the SQL — only prefer-coalesce-over-case 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-coalesce-over-case — 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