假设我有下表
id name city salary dept
我想选择所有大于平均工资的工资
答案 0 :(得分:13)
尝试这样的事情:
SELECT salary WHERE salary > (SELECT AVG(salary) FROM *)
答案 1 :(得分:6)
假设它是mysql,只有以下两个工作。 (我使用临时表,因此名称与你的不同)
select * from b where ref > (select avg(ref) from b);
select * from b having ref > (select avg(ref) from b);
这不是 - select * from b having ref > avg(ref);
我尝试了一些查询 -
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)
答案 2 :(得分:1)
如果支持窗口聚合函数:
SELECT Salary
FROM (
SELECT
Salary,
AVG(Salary) OVER () AS AvgSalary
FROM atable
) s
WHERE Salary > AvgSalary
答案 3 :(得分:1)
它非常简单,只需按照下面的简短命令使用
SELECT *FROM table_name WHERE salary > avg(select salary from table_name)
希望你知道: - )
答案 4 :(得分:0)
select empno,e.deptno,sal
from emp e, ( select deptno,avg(sal) avsal
from emp
group by deptno
) a
where e.sal > a.avsal
and e.deptno = a.deptno;
答案 5 :(得分:0)
如果表格的名称是员工(身份证,姓名,城市,工资)
select salary from Employee where salary > (select ava(salary) from employee)
答案 6 :(得分:0)
假设emp是表的名称,其部门ID为dept_id
(按部门分组)
select e1.* from emp e1 inner join (select avg(sal) avg_sal,dept_id from emp group by
dept_id) as e2 on e1.dept_id=e2.dept_id and e1.sal>e2.avg_sal
查询结果显示所有员工的工资大于平均工资的详细信息。
select * from emp where sal > (select avg(sal) from emp)
答案 7 :(得分:-1)
遵循将为您工作。
SELECT salary FROM table_name WHERE salary > (SELECT AVG(salary) FROM table_name);