The 365 Data Science team is proud to invite you to our own community forum. A very well built system to support your queries, questions and give the chance to show your knowledge and help others in their path of becoming Data Science specialists.
Ask
Anybody can ask a question
Answer
Anybody can answer
Vote
The best answers are voted up and moderated by our team

why do we need group by command at the end of this query??

why do we need group by command at the end of this query??

0
Votes
1
Answer

select t1.emp_no, t1.first_name, t1.last_name, max(t2.salary)- min(t2.salary) as salar_diff,
case
when max(t2.salary) – min(t2.salary) > 30000 then ‘salary raised by more than 30000’
else ‘raised less than that amount’
end as salary_gap
from employees t1 join salaries t2 on t1.emp_no = t2.emp_no group by t2.emp_no;

1 Answer

365 Team
0
Votes

Hi Kane!
Thanks for reaching out!
In this query, the GROUP BY clause allows us to obtain output per employee number. In other words, if we don’t include it, then MySQL will only display an output for the first employee number as obtained by the SQL optimiser and will disregard the rest. That’s how MySQL will behave by default.
Be adding a GROUP BY clause, we designate that we want the output to be separated by employee number.
Hope this helps.
Best,
Martin

thanks

7 months

You are welcome!

7 months
×
LAST CHANCE
Ready to Learn Data Science?
50% OFF