Last answered:

28 Nov 2022

Posted on:

20 Nov 2022

0

why i don't obtain the same result when i use where and having with same condition ?

Dear sir,

When i write the query with where,i obtain the same resultat in the lecture.But when i execute the query with having i obtain another result with a huge difference.
Query :
SELECT
    YEAR(d.from_date) AS calendar_year,
    e.gender,
    COUNT(e.emp_no) AS num_of_employees
FROM
    employees e
JOIN dept_emp d
ON e.emp_no = d.emp_no
WHERE
YEAR(e.hire_date) >= 1990

GROUP BY calendar_year,e.gender
ORDER BY calendar_year;
Resultat:
image.png
Query 2 Having :
SELECT
    YEAR(d.from_date) AS calendar_year,
    e.gender,
    COUNT(e.emp_no) AS num_of_employees
FROM
    employees e
JOIN dept_emp d
ON e.emp_no = d.emp_no
GROUP BY calendar_year,e.gender
HAVING calendar_year >= 1990
ORDER BY calendar_year ;
Result :
image.png
Best Regards
Anouar

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

28 Nov 2022

0

Hi Anouar!
Thanks for reaching out.

Actually, you should use the shortened versions of these tables - t_employees and t_dept_emp.
Please, refer to the following lecture.
https://learn.365datascience.com/courses/sql-tableau/loading-the-database/

Hope this helps.
Best,
Tsvetelin

Submit an answer