1

How to list all employees having percentage greater than some number, and percentage gets calculated on the basis of max salary.

Example:

if max salary is 100 and an employee salary is 50 then percentage should show 50.

This is what i tried:

select (salary/Max(salary)*100) as percentage from test.employeetable
where percentage > 75;

Table

Error that i get is:

Unknown column 'percentage' in 'where clause

tabby
  • 1,767
  • 1
  • 17
  • 34

4 Answers4

2

Try something like that;

select * from (
select (salary / (select max(salary) from test.employeetable) * 100) as percentage from test.employeetable) Records
where percentage > 75;
lucky
  • 11,548
  • 4
  • 18
  • 35
1

First get the max in a variable, then select the relevant results

select @max := max(salary) from test.employeetable;
select (salary/@max*100) as pc from test.employeetable having pc > 75;

note the having instead of where.

You could display the relevant salary (etc...) as well

select @max := max(salary) from test.employeetable;
select salary,(salary/@max*100) as pc from test.employeetable having pc > 75;

Without using a variable

select (salary/m.mx*100) as pc from test.employeetable, 
(select max(salary) as mx from test.employeetable) as m
having pc > 75;
Breaking not so bad
  • 26,037
  • 5
  • 67
  • 96
0
Select (salary/Max(salary)*100) as percentage 
from test.employeetable 
where (salary/Max(salary)*100) > 75;  

in MySql you Can not use an "as" in where clause

H. Pauwelyn
  • 11,346
  • 26
  • 71
  • 115
Dans
  • 166
  • 6
-1

Use a subquery

SELECT *
FROM (
  SELECT (salary / MAX(salary) * 100) AS percentage
  FROM test.employeetable
) x
WHERE percentage > 75