itvba
itvba

Reputation: 235

Substract 2 columns SQL

I have a table where i have 2 columns: salary and newsalary. I want to substract newsalary with salary and store it in a new column. How can i do that?

This isn't working:

SELECT employee_id, last_name, salary, round((salary * 0.155 + salary)) AS newsal, SUBSTR(newsal, salary) AS Increase FROM employees;

This also isn't working

SELECT employee_id, last_name, salary, round((salary * 0.155 + salary)) AS newsal, sum(newsal - salary) AS Increase FROM employees;

Upvotes: 1

Views: 106

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133360

You can't use alias as column name in select you must repeat the code and simply use minus

  SELECT employee_id
  , last_name
  , salary
  , round((salary * 0.155 + salary)) AS newsal
  , ( round(salary * 0.155 + salary) -salary) AS Increase 
  FROM employees;

or you can use a subselect as table

select  employee_id
, last_name, salary
, (newsal - salary=  AS Increase
from (
SELECT employee_id
, last_name
, salary
, round((salary * 0.155 + salary)) AS newsal
FROM employees
) t 

Upvotes: 1

Related Questions