4

I have two columns (buying prince and sale price) and I want to calculate the difference between them. After that I want to order the result so I can see all the profit margins.

Can I do it with just one SELECT statement?

Thanks!

user927495
  • 73
  • 1
  • 1
  • 4

2 Answers2

17
  SELECT (sale_price - buy_price) AS profit
    FROM table_name
ORDER BY profit DESC
Joe
  • 15,669
  • 4
  • 48
  • 83
6

Joe has it, but I think you might be looking for something slightly different for the ordering.

Profit margin is defined as net income / revenue.. so the profit margin of each product would be (sale_price minus buy price) divided by sale_price.

SELECT (sale_price - buy_price) AS profit FROM table_name 
ORDER BY ((sale_price - buy_price) / sale_price) DESC

Good luck.

psarid
  • 450
  • 7
  • 19