0

I have problems with the following SQL Query:

SELECT job
FROM (SELECT job, COUNT(*) AS cnt
FROM Employee
GROUP BY job)
WHERE cnt=1

As Result it should only shows all jobs where cnt (count of jobs) equals 1.
When I test the select query above on Fiddle, I get following error :

   Incorrect syntax near the keyword 'WHERE'.

SQLFiddle: http://sqlfiddle.com/#!6/d812a/7

6
  • sqlfiddle.com/#!6/d812a/15 Commented Apr 1, 2016 at 7:05
  • Is the expected result Baker and Gardener ? Commented Apr 1, 2016 at 7:06
  • You could also use HAVING, which would make the outer query redundant Commented Apr 1, 2016 at 7:06
  • @Loufylouf - Yes Baker, Gardener would be the correct result. Commented Apr 1, 2016 at 7:07
  • Strawberry, could u give me an example with HAVING-Clausel? Commented Apr 1, 2016 at 7:08

5 Answers 5

1

No need to increase complexity by using sub-query when it is not require

SELECT job, count(job)
FROM Employee
GROUP BY job
having count(job)=1;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks - that looks really simple. :)
1

You need to provide alias name to the nested query

SELECT A.job
FROM (SELECT job, COUNT(*) AS cnt
FROM Employee
GROUP BY job)A
WHERE A.cnt=1

Comments

0

You forget to add the alias name. Please change the query like this

SELECT job
FROM (SELECT job, COUNT(*) AS cnt
FROM Employee
GROUP BY job) As MyTable
WHERE cnt=1

You should have to give the alias name for the inner query when you are using the select and where clauses outside.

3 Comments

Didn't know that. Thanks for mentioned it - but I guess I will use HAVING-Clausel for it.
That one is fine and it is differnet than what you asked. I corrected your Query. and if it is worked and if it is the direct answer your question then upvote and accept. Check the Question that have asked , Not the alternative solution right?
You are right, I would upvote, when I have enough reputation points.
0

You should use the HAVING syntax :

SELECT job, COUNT(*) AS cnt
FROM Employee
GROUP BY job
HAVING cnt = 1;

2 Comments

Doesn't work, it returns an error. The aggregate function should be used directly in the HAVING clause.
It depends of your SQL mode. If you have disabled ONLY_FULL_GROUP_BY, it should work.
0

You should use the HAVING clause which is done for that kind of thing. Your request will be simply :

SELECT job FROM Employee GROUP BY job
HAVING COUNT(id)=1

The documentation states that

The SQL standard requires that HAVING must reference only columns in the GROUP BY clause or columns used in aggregate functions. However, MySQL supports an extension to this behavior, and permits HAVING to refer to columns in the SELECT list and columns in outer subqueries as well.

The important thing to note is that contrary to the WHERE clause, you can use aggregate funcitons (like count, max, min ...) in the HAVING clause.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.