SoKeT SoKeT - 5 months ago 17
MySQL Question

MySQL - ERROR 1054 (42S22): Unknown column 'sal' in 'having clause'

I need to count the number of employees by department with a salary greater than the average.

My employees table:

+------+--------+-----------+------+------------+------+------+--------+
| eno | ename | job | mgr | hiredate | sal | comm | deptno |
+------+--------+-----------+------+------------+------+------+--------+
| 7369 | smith | clerk | 7902 | 1980-12-17 | 800 | NULL | 20 |
| 7499 | allen | salesman | 7698 | 1981-02-20 | 1600 | 300 | 30 |
| 7521 | ward | salesman | 7698 | 1981-02-22 | 1250 | 500 | 30 |
| 7566 | jones | manager | 7839 | 1981-04-02 | 2975 | NULL | 20 |
| 7654 | martin | salesman | 7698 | 1981-10-28 | 1250 | 1400 | 30 |
| 7698 | blake | manager | 7839 | 1981-05-01 | 2850 | NULL | 30 |
| 7782 | clark | manager | 7839 | 1981-06-09 | 2450 | NULL | 10 |
| 7788 | scott | analyst | 7566 | 1982-12-09 | 3000 | NULL | 20 |
| 7839 | king | president | NULL | 1981-11-17 | 5000 | NULL | 10 |
| 7844 | turner | salesman | 7698 | 1981-10-08 | 1500 | NULL | 30 |
| 7876 | adams | clerk | 7788 | 1983-01-12 | 1100 | NULL | 20 |
| 7900 | james | clerk | 7698 | 1981-12-03 | 950 | NULL | 30 |
| 7902 | ford | analyst | 7566 | 1981-12-03 | 3000 | NULL | 20 |
| 7934 | miller | clerk | 7782 | 1982-01-23 | 1300 | NULL | 10 |
+------+--------+-----------+------+------------+------+------+--------+


The query I'm trying to do is

select deptno, count(*) from emp group by deptno having sal > avg(sal)


And that's when I get the error.

I tried this query too:

select deptno, count(*), avg(sal) from emp group by deptno


Which is basically the same except there is no having clause, and it works as expected.

-

select deptno, count(*) from emp where sal > avg(sal) group by deptno


This query throws the following error:

ERROR 1111 (HY000): Invalid use of group function


-

select deptno, count(*) from emp group by deptno where sal > avg(sal)


And this one is using incorrect syntax:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where sal > avg(sal)' at line 1


-

Why am I getting this error? Which would be the correct way to execute this query? Thanks in advance.

Insert script:

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

CREATE TABLE IF NOT EXISTS `emp` (
`eno` int(11) NOT NULL AUTO_INCREMENT,
`ename` varchar(30) DEFAULT NULL,
`job` varchar(30) DEFAULT NULL,
`mgr` int(11) DEFAULT NULL,
`hiredate` date DEFAULT NULL,
`sal` int(11) DEFAULT NULL,
`comm` int(11) DEFAULT NULL,
`deptno` int(11) DEFAULT NULL,
PRIMARY KEY (`eno`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=10000 ;

INSERT INTO `emp` (`eno`, `ename`, `job`, `mgr`, `hiredate`, `sal`, `comm`, `deptno`) VALUES
(7369, 'smith', 'clerk', 7902, '1980-12-17', 800, NULL, 20),
(7499, 'allen', 'salesman', 7698, '1981-02-20', 1600, 300, 30),
(7521, 'ward', 'salesman', 7698, '1981-02-22', 1250, 500, 30),
(7566, 'jones', 'manager', 7839, '1981-04-02', 2975, NULL, 20),
(7654, 'martin', 'salesman', 7698, '1981-10-28', 1250, 1400, 30),
(7698, 'blake', 'manager', 7839, '1981-05-01', 2850, NULL, 30),
(7782, 'clark', 'manager', 7839, '1981-06-09', 2450, NULL, 10),
(7788, 'scott', 'analyst', 7566, '1982-12-09', 3000, NULL, 20),
(7839, 'king', 'president', NULL, '1981-11-17', 5000, NULL, 10),
(7844, 'turner', 'salesman', 7698, '1981-10-08', 1500, NULL, 30),
(7876, 'adams', 'clerk', 7788, '1983-01-12', 1100, NULL, 20),
(7900, 'james', 'clerk', 7698, '1981-12-03', 950, NULL, 30),
(7902, 'ford', 'analyst', 7566, '1981-12-03', 3000, NULL, 20),
(7934, 'miller', 'clerk', 7782, '1982-01-23', 1300, NULL, 10);

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

Answer

Try to Join the aggregate with the table

select emp.depnto, count(*) from 
(select deptno,  avg(sal) avgsal from employees group by deptno) avg
INNER JOIN employees emp on avg.deptno = avg.depnto
WHERE emp.sal > avg.avgsal

EDIT

Maybe i thoght to complicated. When the overall-average is meant it is simply

select depnto, count(*) from employees
where sal > (select avg(sal) from employees)
group by deptno