Skip to content
Advertisement

Creating new column from existing column with condition SQL

I need help with one table name as Employee. Table employee consist of four columns “ID” “salary”, “position” and “cumulative_salary” . Position consist of senior and junior status and there salary are in different range. I want to add another column from salary column named as “salary_range”. Now condition for my salary range column is if someone salary lies between 0 – 10K there salary range will be 0-10 in the salary_range column. same conditions for if someone salary is in between 11K-20K there range is 11K-20K like this.

i want a query that generates a separate column that shows in which bucket the salary range falls, and name it salary bucket. The salary buckets can be created in 10K steps, first one will range from 0-10,000 and so on.

something like this

Select salary as Salary range 
from employee 
where salary < 10k 
salary range is 0-10k || salary > 10k && < 20k ,
salary range = 11k -20k

Advertisement

Answer

Here is the the code:

Select salary,
    (Case 
         When salary < 10000 Then '0-10k'
         When salary >= 10000 and salary < 20000 Then '11k -20k'
         else
             '30k'
     end) as salary_bucket                                            
 from employee

Output will look like this

enter image description here

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement