Last answered:

25 Mar 2022

Posted on:

12 Nov 2021

0

error code 1140 : in aggregated query without group by

I followed the code exactly per the video and everything is ok until I performed the call emp_avg_salary.

I know we need to perform group by, but on the video, you did not perform group by and everything seems ok on your MySql while mine shows error, do you mind explaining what is going on?
Thank you very much 365 team



Screen Shot 2021-11-12 at 19.53.50.png

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

14 Nov 2021

1

Hi Yenni!

Thanks for reaching out.

This is a good catch! Indeed, the fact that we did not obtain an error reflects the fact that we've recorded that video on a different version of MySQL where one of the default settings regarding the use of the GROUP BY clause was different.
Although ideally we should be using the GROUP BY clause in case we are using an aggregate functions, there is a way to correct for that.

Basically, to make sure you can take some of the remaining lectures of the course without unnecessary interruption, we strongly advise you to execute the following query now.
set @@global.sql_mode := replace(@@global.sql_mode, 'ONLY_FULL_GROUP_BY', '');
If you’re interested in the reason we are asking you to do this, you can read the technical description below. Please be aware that it may sound a bit too abstract at this stage. However, you can always refer back to it once you have completed the lectures about the different types of SQL variables found in the Advanced SQL Tools section of the course.

Technical description:
The GROUP BY clause will be used in several queries by the end of the course. Different versions of the SQL language and MySQL, in particular, can set different limits on how the GROUP BY clause can be applied.
In some of the lectures in the JOINs section, we have opted for queries where multiple table columns that are not included in the GROUP BY clause will be listed in the SELECT statement. We have decided this is the correct approach to teaching the use of the GROUP BY clause at this stage. At the same time, we are aware some versions of MySQL may not allow such queries, or at least not by default.
To adjust the relevant default settings, there is a system variable, called ‘sql_mode’, which needs to be reconfigured.
In order to view the current value of this variable in your case, you have to execute the following command.
select @@global.sql_mode;
An expression containing a few values, separated by commas, will appear in the result grid. They correspond to various MySQL settings that influence the way in which MySQL will behave in different situations.
One of these values, ‘only_full_group_by’, blocks certain type of group statements and that can potentially lead to Error Code 1055. The latter signifies the problem of listing fields in the SELECT statement that are not included in the GROUP BY clause.
Naturally, we want to avoid that. Hence, to prevent it from happening, we must execute the following statement.
set @@global.sql_mode := replace(@@global.sql_mode, 'ONLY_FULL_GROUP_BY', '');
REPLACE() is the function that will remove the “only_full_group_by” value from the expression here. Thus, error 1055 will not show up in the future.
Finally, if for some reason you’d like to disallow this behavior you can always execute the following command which will do exactly the opposite: it will add the “only_full_group_by” value to the expression.
set @@global.sql_mode := concat('ONLY_FULL_GROUP_BY,', @@global.sql_mode);
That said, we must also add that there is a reason behind this functionality. If you think about it, it is not logical to allow a column value to be listed in the output alongside a value that has been included in the GROUP BY clause of the query. We just cannot be sure that the not-grouped value that has been retrieved is going to be correct. But for the sake of our exercises, and for the purpose of making them clearer, we have allowed such syntax.

Hope this helps.
Best,
Martin

Posted on:

15 Nov 2021

0

Hello there, Martin.
Yes, you are correct; it does sound a little abstract.
However, I appreciate your response.
Thank you very much.

Yeni

Posted on:

15 Nov 2021

0

I have the same issue, although it continues to persist even with this fix.  I can see that "ONLY_FULL_GROUP_BY" is NOT in the @@global.sql_mode variable any more (if it once was), but I still get error 1140. Is there another place where sql is being told to only allow full_group_by?

Instructor
Posted on:

03 Dec 2021

0

Hi Carly!

Thanks for reaching out.

Can you please execute the following query and then retry running the other SELECT statement.

set @@global.sql_mode := concat('ONLY_FULL_GROUP_BY,', @@global.sql_mode);

Does this help?

Looking forward to your answer.
Kind regards,
Martin

Posted on:

24 Mar 2022

0

I also tried to execute

set @@global.sql_mode := concat('ONLY_FULL_GROUP_BY,', @@global.sql_mode);

but still got the error from MySQL.

Posted on:

24 Mar 2022

0

Even with the suggested set clause, I could only get the code to work by adding a group by line:

CREATE DEFINER=`nativeuser`@`localhost` PROCEDURE `emp_avg_salary`(IN p_emp_no INTEGER)
BEGIN
SELECT
    e.first_name, e.last_name, AVG(s.salary)
FROM
    employees e
        JOIN
    salaries s ON e.emp_no = s.emp_no
WHERE
    e.emp_no = p_emp_no
GROUP by e.emp_no;
END

Instructor
Posted on:

25 Mar 2022

1

Hi Michael!

Thanks for reaching out!

Please ensure you have reconnected to the database after changing the SQL mode (this is essential to do after changing global variables' values). Then, retry.

Hope this helps.
Kind regards,
Martin

Submit an answer