SQL CASE query prioritization I'm doing a pokemon sql table and created a new column named capacity_difference to determine the collectability of each pokemon in the table. However, when I want to use CASE query to categorize them, the outcomes only show the ELSE condition for every log. Can someone please tell me how to fix the query?
My queries are as follow:
ALTER TABLE pokemon add capacity_difference INTEGER;

SELECT name, type_1, type_2, HP, (attack - defense) as capacity_difference from pokemon; 

SELECT name, type_1, type_2, HP, (attack - defense) as capacity_difference, 
  CASE 
    when capacity_difference &gt; 90 then &quot;collect asap&quot;
    when  capacity_difference &lt; 90 and capacity_difference &gt; 50 then &quot;good&quot;
    when capacity_difference &gt; 0 and capacity_difference &lt; 50 then &quot;okay&quot;
    when capacity_difference &gt; -10 and capacity_difference &lt; 0 then &quot;bad&quot;
  ELSE &quot;worse&quot;
  END AS Collectability
FROM pokemon;
The problem is that renaming ( (attack - defense) as capacity_difference ) is not known at the time when your case expression is evaluated. I.e. Your case expression is evaluating the column capacity_difference which you have not assigned a value. I would suggest that you get rid of that column, and do:
SELECT name
     , type_1
     , type_2
     , HP
     , CASE when capacity_difference &gt; 90 
            then &quot;collect asap&quot; 
            when capacity_difference &lt; 90 and capacity_difference &gt; 50 
            then &quot;good&quot; 
            when capacity_difference &gt; 0 and capacity_difference &lt; 50 
            then &quot;okay&quot; 
            when capacity_difference &gt; -10 and capacity_difference &lt; 0 
            then &quot;bad&quot; 
            ELSE &quot;worse&quot; 
       END AS Collectability 
FROM (
    SELECT (attack - defense) as capacity_difference
         ,  name
         , type_1
         , type_2
         , HP
    FROM pokemon
) AS t;