Performing Calculations on SQL Columns
While the percent change column is already calculated and available in the dataset on the right, it’s useful to understand how to calculate data from SQL columns in a new column.
SELECT City, 2022 estimate, 2020 estimate, 100 * (2022 estimate - 2020 estimate ) / (2020 estimate)
FROM database_table
Consider the first row of data: “100 * (2022 estimate - 2020 estimate ) / (2020 estimate) “ will take 8,335,897, subtract 8,804,190 which gives -468,293. Then it takes -468,293 and divides it by 8,804,190 to give -0.05. The phrase “100 ” indicates that it will multiply this calculation by 100 to return 5%.
Remember “ * “ in SQL and Python represents multiplication, so (10 * 5) in SQL and Python returns 50
And “ / ” in SQL and Python represents division, so 10/5 returns 2.