Scrivio Interview library
DebuggingFoundations6 min read · updated 12 November 2026

MySQL 8.4 rejects SELECT customer_id, email, COUNT(*) FROM orders JOIN customers USING (customer_id) GROUP BY customer_id with an ONLY_FULL_GROUP_BY error, but an old MySQL 5.6 server accepted it. Explain why and what correct options exist.

With ONLY_FULL_GROUP_BY, every column in the SELECT list must be grouped, aggregated, or provably determined by the GROUP BY columns, and the server cannot prove that email has a single value per group as the query is written. MySQL 5.6 did not enforce this by default and silently returned an arbitrary email from each group, which is a correctness risk. The correct options are to group by the customers table's primary key and email, to use ANY_VALUE(email) when we knowingly accept any value, or to aggregate orders first and then join customers; turning ONLY_FULL_GROUP_BY off is not a fix.

Why this question is asked

Upgrading an old MySQL server is when many teams meet this error for the first time: queries that worked for years suddenly fail with an ONLY_FULL_GROUP_BY message. The tempting response is to remove the mode from sql_mode and move on. Interviewers ask about it because the right response requires understanding what the rule protects us from. In this answer, we will recap what GROUP BY allows in the SELECT list, see what MySQL 5.6 was really doing, understand functional dependency, and go through the three correct fixes and why disabling the check is a mistake.

1A Quick Recap of What GROUP BY Allows

After GROUP BY, the query produces one row per group. For each column in the SELECT list, the database must know which single value to show for the group. A grouped column has one value by definition. An aggregate such as COUNT(*) or MAX(email) computes one value from the group. Any other column is a problem: if a group has 10 rows, which of their 10 values should it show?

The SQL standard allows a third case: a column that is functionally dependent on the grouping columns. Functional dependency means that the grouping value determines the column's value. If we group by customers.customer_id, the primary key, then email is determined by it, since each customer row has exactly one email, so showing it is safe.

2What MySQL 5.6 Was Doing

MySQL 5.6 did not include ONLY_FULL_GROUP_BY in its default sql_mode. It accepted any column in the SELECT list and returned a value from some row of the group, with no guarantee which. When email really is the same for every row in the group, the arbitrary choice happens to be harmless. When it is not, the query silently returns wrong data.

SQL
-- MySQL 5.6 default mode: accepted
SELECT o.customer_id, o.status, COUNT(*)
FROM orders o
GROUP BY o.customer_id;
-- 42 | PAID | 12 <- status of one random order out of 12; another run or plan may show SHIPPED

In this example the arbitrary value is obviously meaningless, because a customer's 12 orders have different statuses. MySQL 5.7.5 made ONLY_FULL_GROUP_BY the default and added functional dependency detection, so the server refuses such queries instead of guessing. MySQL 8.4 keeps that default.

3Why the Original Query Is Rejected

The original query groups by customer_id, which comes from the USING join between orders and customers, and selects email from customers. For the server to accept it, it must prove from its rules that the grouping column determines customers.email. PostgreSQL's rule is narrow: it recognises the dependency only when we group by the primary key columns of the table that holds the selected column. Grouping by orders.customer_id, a foreign key that is not unique in orders, does not qualify, even though the join makes the values equal.

MySQL's detection is somewhat broader and can follow equalities from inner join conditions, so whether a particular spelling is accepted depends on how the grouping column resolves in the join and on the version; check your version. The error in the question shows that, as written, the server could not prove it. The robust approach, which both engines accept, is to make the dependency obvious by grouping by the primary key of customers.

Columns in the SELECT list and whether they are allowed
ColumnGrouping by orders.customer_idGrouping by c.customer_id (PK)
customer_idAllowed: groupedAllowed: grouped
COUNT(*)Allowed: aggregateAllowed: aggregate
c.emailNot provable: errorAllowed: depends on the PK
o.statusErrorError: not determined by the customer

4Fix 1: Group by the Primary Key, and Email If Needed

The clearest fix is to group by the customers primary key. Both PostgreSQL and MySQL then accept email as functionally dependent. Adding email to the GROUP BY as well is harmless, makes the query portable to engines without dependency detection, and states the intent to the reader.

SQL
SELECT c.customer_id, c.email, COUNT(*) AS orders
FROM customers c
JOIN orders o ON o.customer_id = c.customer_id
GROUP BY c.customer_id, c.email;
-- 42 | ana@example.com | 12
-- 7 | ben@example.com | 5

Grouping by both columns does not change the result, because email has one value per customer anyway, so the groups are exactly the same.

5Fix 2: ANY_VALUE When We Knowingly Accept Any Value

MySQL provides ANY_VALUE(col), which tells the server: I know this column may not be provably single-valued, return a value from any row of the group. It is the explicit version of what MySQL 5.6 did silently.

SQL
SELECT o.customer_id, ANY_VALUE(c.email) AS email, COUNT(*) AS orders
FROM orders o
JOIN customers c ON c.customer_id = o.customer_id
GROUP BY o.customer_id;

The difference from the old behaviour is that the choice is now visible in the code. A reviewer can see it and ask whether any value is really acceptable. Here it is, because email does not vary within a customer, but I would still prefer Fix 1 when it is available. PostgreSQL 16 and later also offer an any_value() aggregate; in older versions MIN(email) or MAX(email) does the same job.

6Fix 3: Aggregate First, Then Join

Often the best shape is to aggregate the big table on its own and join the small one afterwards. The aggregation then works only with orders, and the join attaches one customer row to each already-aggregated row.

SQL
SELECT c.customer_id, c.email, o.orders
FROM (
SELECT customer_id, COUNT(*) AS orders
FROM orders
GROUP BY customer_id
) o
JOIN customers c ON c.customer_id = o.customer_id;

This form has no dependency question at all, since the inner query selects only grouped and aggregated columns. It is also often faster on large tables, because the join processes one row per customer instead of one row per order.

7Why Not Simply Disable ONLY_FULL_GROUP_BY

Removing ONLY_FULL_GROUP_BY from sql_mode makes the error go away, but it brings back the silent arbitrary choice for every query in the application, including the ones where the value really does vary. Those queries will return plausible but wrong data, and nothing will tell us. It also makes the application depend on a non-default server setting, so a new environment or a managed database with defaults will fail differently. The error is the database pointing at a real ambiguity, so the right move is to fix the queries, which usually takes one of the three small changes above.

8Follow-up questions

Does PostgreSQL accept the original query?

No. PostgreSQL reports that email must appear in the GROUP BY clause or be used in an aggregate function, unless we group by the primary key of customers. It has never had MySQL's old permissive mode, so applications that move from MySQL 5.6 to PostgreSQL meet the same set of queries to fix.

How do you find all affected queries before an upgrade?

Turn on ONLY_FULL_GROUP_BY in a test environment with the old server, or run the test suite against MySQL 8.4, and collect the errors. The general query log or performance_schema statement digests from production help find queries the tests do not cover.

9Conclusion

In this answer, we saw that ONLY_FULL_GROUP_BY requires every selected column to be grouped, aggregated or functionally dependent on the GROUP BY columns. MySQL 5.6 silently returned an arbitrary value from each group, while MySQL 5.7 and later refuse queries where they cannot prove a single value. The correct fixes are grouping by the customers primary key and email, using ANY_VALUE knowingly, or aggregating orders first and joining afterwards, and disabling the mode only hides real ambiguity.

What to say in the interviewAbout 80 seconds aloud

Open with

The rule behind the error is that with GROUP BY, each column in the SELECT list must be grouped, aggregated, or functionally dependent on the grouping columns, meaning the grouping value determines it.

Then, if they want more

MySQL 5.6 didn't enforce that by default and simply returned a value from an arbitrary row of each group, which is a correctness risk whenever the values differ. Since 5.7, ONLY_FULL_GROUP_BY is on by default, and the server rejects the query if it can't prove email has one value per group.

PostgreSQL recognizes the dependency only when you group by the primary key of the table the column comes from, and grouping by the orders foreign key doesn't prove it; MySQL's detection is a bit broader, but as written it couldn't prove it either. I'd fix it by grouping by c.customer_id, the primary key, and email, which both engines accept.

If I genuinely don't care which value appears, ANY_VALUE of email makes that choice explicit. Often the best option is to aggregate orders first in a subquery and then join customers, which is also faster. I would not remove ONLY_FULL_GROUP_BY from sql_mode, because that brings back silent wrong results everywhere.

About this answer

Last updated
12 November 2026
Versions it names
MySQL 5.6, MySQL 5.7, MySQL 8.4, PostgreSQL 16
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "MySQL 8.4 rejects SELECT customer_id, email, COUNT(*) FROM orders JOIN customers USING...": https://scrivio-interview-library.netlify.app/databases/sql-fundamentals/a38eee825f/