SQL

SQL

Made by DeepSource

Unflattened CASE statements in ELSE clause SQL-L058

Anti-pattern
Minor

Nested CASE statement in ELSE clause could be flattened.

Bad practice

In this example, the outer CASE’s ELSE is an unnecessary, nested CASE.

SELECT
  CASE
    WHEN species = 'Cat' THEN 'Meow'
    ELSE
    CASE
       WHEN species = 'Dog' THEN 'Woof'
    END
  END as sound
FROM mytable

Recommended

SELECT
  CASE
    WHEN species = 'Cat' THEN 'Meow'
    WHEN species = 'Dog' THEN 'Woof'
  END AS sound
FROM mytable