Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine salaries greater than the average salary

Tags:

sql

Assume I have the following table

id  name city   salary  dept 

and I want to select all salaries which are greater than the average salary

like image 206
slafik Avatar asked Jun 19 '11 17:06

slafik


People also ask

How do you display employee name and his salary whose salary is greater than highest average of department number?

How to display employee name and his salary whose salary is greater than highest average of department number? SELECT ename,deptno,sal FROM emp WHERE sal> (SELECT MAX(AVG(sal)) FROM emp GROUP BY deptno ); Share the knowledge!

How do you find higher than average in SQL?

Find values that are well above the average The simplest way is to use a fixed margin, for example 1000: SELECT name, surname, salary FROM employee WHERE (salary - 1000) > ( SELECT avg(salary) FROM employee ) ; But maybe you don't want a fixed value, you want something smarter.

How do you calculate average salary?

To calculate salary average, you'll add up all the salaries in your chosen group and divide by the people in that group.


2 Answers

Try something like this:

SELECT salary WHERE salary > (SELECT AVG(salary) FROM *)
like image 175
John Zwinck Avatar answered Sep 19 '22 09:09

John Zwinck


Assuming it's mysql, only the below two work. (I used a temp table so the names are different from yours)

select * from b  where ref > (select avg(ref) from b);
select * from b  having ref > (select avg(ref) from b);

This doesn't - select * from b having ref > avg(ref);

Some queries I tried -

mysql> select * from b;
+------+------------+------+
| id   | d2         | ref  |
+------+------------+------+
|  300 | 2010-12-12 |    3 |
|  300 | 2011-12-12 |    2 |
|  300 | 2012-12-12 |    1 |
|  400 | 2011-12-12 |    1 |
+------+------------+------+
4 rows in set (0.00 sec)

mysql> select * from b  having ref > avg(ref);
+------+------------+------+
| id   | d2         | ref  |
+------+------------+------+
|  300 | 2010-12-12 |    3 |
+------+------------+------+
1 row in set (0.00 sec)

mysql> select * from b  having ref > (select avg(ref) from b);
+------+------------+------+
| id   | d2         | ref  |
+------+------------+------+
|  300 | 2010-12-12 |    3 |
|  300 | 2011-12-12 |    2 |
+------+------------+------+
2 rows in set (0.02 sec)

mysql> select * from b  where ref > (select avg(ref) from b);
+------+------------+------+
| id   | d2         | ref  |
+------+------------+------+
|  300 | 2010-12-12 |    3 |
|  300 | 2011-12-12 |    2 |
+------+------------+------+
2 rows in set (0.00 sec)

mysql> select *,avg(ref) from b  having ref > avg(ref);
+------+------------+------+----------+
| id   | d2         | ref  | avg(ref) |
+------+------------+------+----------+
|  300 | 2010-12-12 |    3 |   1.7500 |
+------+------------+------+----------+
1 row in set (0.00 sec)
like image 21
Praveen Lobo Avatar answered Sep 20 '22 09:09

Praveen Lobo