Last answered:

20 Nov 2021

Posted on:

06 Sept 2021

0

Why can't I do this code instead?

SELECT
    first_name, COUNT(first_name) AS names_count
FROM
    employees
GROUP BY first_name
HAVING COUNT(first_name) < 200
    AND hire_date > '1999-01-01'
ORDER BY first_name DESC;

3 answers ( 0 marked as helpful)
Instructor
Posted on:

12 Sept 2021

0

Hi Marcus!

Thanks for reaching out.

By executing the query you suggest, don't you obtain an error message saying that the 'hire_date' in the HAVING clause is unknown? If not, what output do you obtain on execution of the given query? Thank you.

Looking forward to your answer.
Best,
Martin

Posted on:

28 Oct 2021

0

Yes, we get this error.

Instructor
Posted on:

20 Nov 2021

0

Hi Bhushan!

Thanks for reaching out!
Thank you for your answer and please excuse me for the late reply.

The error message is most probably obtained because the hire_date we are referring to has not been mentioned in the field list. Please retry with a query beginning by:
SELECT
    first_name, COUNT(first_name) AS names_count, hire_date
FROM
    ...
However, you might obtain an empty output. This is because in the condition you are looking for a number of records per hire date to be smaller than 200. To obtain an output containing records from the table, you need to change the condition to, e.g., > 200.

Hope this helps.
Best,
Martin

Submit an answer