Last answered:

10 Apr 2023

Posted on:

31 Mar 2023

0

Retrieve diff columns while not included wthin Group by

I see some student write this code on the Q&A section : I'm wondering how this code will work without using the column hire_date as well within "Group By", how you can group data based on one column while the results we want to retrieve include another column that probably contains diff values for each row "first_name" (I don't think all group of 'first_name' have the same hire_date) 

SELECT 
    COUNT(first_name), first_name, hire_date
FROM
    employees
WHERE
    hire_date > '1999-01-01'
GROUP BY first_name
HAVING COUNT(first_name) < 200; 


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

03 Apr 2023

0

Hi Elmehdi!
Thanks for reaching out.


Actually, it is not compulsory to use the hire_date column in the GROUP BY clause. You use this column which you would like to group the result set by.


Hope this helps.
Best,
Tsvetelin

Posted on:

04 Apr 2023

0

No, I mean we can't retrieve in the same time a column with multiple records hire_date and a column that return only a single value COUNT(first_name) without grouping the records by this column (hire_date)

Actually, I see the answer to my question farther in the course, here:

https://learn.365datascience.com/courses/sql/using-aggregate-functions-with-joins/

We can't retrieve by SELECT an aggregate function and another column without grouping by this column in other versions of SQL.

Thanks.

Instructor
Posted on:

10 Apr 2023

0

Hi Elmehdi!
Thanks for reaching out.


Yes, we have to use this column in the GROUP BY clause.

I am glad that you have found the lecture which describes this concept.


Best,
Tsvetelin

Submit an answer