Search code examples
sqloraclerecursive-query

calculate salary in oracle


I have a starting salary as 100,000.

I want to calculate 10% as salary increase and 5% as bonus increase for next 5 years.

Not able to figure out the oracle query. I tried lag function and connect by dual. but did not succeed.

So something like the following:

year    salary    bonus   total_salary 
1       100,000   5000    105,000

Solution

  • One option uses a recursive query:

    with cte(year, salary, bonus) as (
        select 1, 100000, 5000 from dual
        union all
        select year + 1, salary * 1.10, bonus * 1.05 from cte where year < 20
    )
    select c.*, salary + bonus total_salary from cte c