Home

CASE expressions should end with ELSE clauses

Rule description

  • CASE expressions should end with ELSE clauses

 

Non-compliant Code Example

SELECT
  carName,
  price,
  CASE          --Non compliant code (Case ends without else clause)
    WHEN price > 0 AND price  < 500000
        THEN 'Low Model'
    WHEN price >= 500000 AND price < 1200000
        THEN 'Hatchback Model'
    WHEN price >= 1200000 AND price < 1500000
        THEN 'Sedan Model'
  END car_group
FROM
  cars

Compliant Code Example

SELECT
  carName,
  price,
  CASE          --Compliant code (Case ends with else clause)
    WHEN price > 0 AND price  < 500000
        THEN 'Low Model'
    WHEN price >= 500000 AND price < 1200000
        THEN 'Hatchback Model'
    WHEN price >= 1200000 AND price < 1500000
        THEN 'Sedan Model'
    ELSE 
        'Premium Model'
  END car_group
FROM
  cars
Visual Expert 2020
 VEPLSQLRULE45