1

I have written this query on oracle database.

Select salary from emp having salary < (SELECT MAX(SALARY) FROM EMP ));

http://sqlfiddle.com/#!4/47961/13

But it is not working .

Where am i wrong?

3
  • )); should be ); I believe.
    – Oren
    Jan 15, 2013 at 14:08
  • having is used in combination with GROUP BY to narrow down results
    – Tikkes
    Jan 15, 2013 at 14:08
  • you can apply having clause where you used group by , here there is no group by than just use where Jan 15, 2013 at 14:14

5 Answers 5

3

things to do,

  • use WHERE (use having when comparing with aggregated condition)
  • remove extra parenthesis,

eg.

Select salary 
FROM emp 
WHERE salary < (SELECT MAX(SALARY) FROM EMP );
3

Two errors:

  • Use WHERE instead of HAVING.
  • Remove the extra parenthesis.

Try this:

SELECT salary
FROM emp
WHERE salary < (SELECT MAX(salary) FROM emp);

http://sqlfiddle.com/#!4/47961/17

1
Select salary from emp where salary < (SELECT MAX(SALARY) FROM EMP ));

you can apply having clause where you used group by , here there is no group by than just use where

The HAVING Clause

The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.

0
0
Select salary from emp WHERE SALARY < (SELECT MAX(SALARY) FROM EMP );
0

Correct way using Having in Oracle:

SELECT sal FROM scott.emp
 GROUP BY sal
HAVING sal < (SELECT MAX(SAL) FROM scott.EMP)

/
SELECT deptno, COUNT(*) total_emps_by_dept
  FROM scott.emp
GROUP BY deptno
HAVING COUNT(*) > 1
/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.