Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading