✏️ Explanatory Question

Predict the output — GROUP BY with a non-aggregated column in the SELECT (the ONLY_FULL_GROUP_BY trap)

👁 5 Views
📘 Detailed Answer
🟢 Easy
💡

Answer with Explanation

84

What does a GROUP BY with a non-aggregated column return? (the ONLY_FULL_GROUP_BY trap)

Level: Hard — Tests whether you understand aggregation grain and why MySQL changed its default behaviour.

Scenario: A query that worked fine on the old MySQL 5.6 server throws an error on the new MySQL 8.0 server: "Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column...". The dev insists "it worked before!" — what changed, and what was the query silently doing wrong?

Sample Data — employees table

emp_id name dept_id salary
1Rumman1090000
2Krushna1060000
3Swetha1075000
4Ritesh2050000
5Manjula2080000

The cause: When you GROUP BY dept_id, each group collapses many rows into one. But if you also SELECT name (a non-aggregated column), MySQL has 3 names for dept 10 and must pick just one. Older MySQL silently returned an arbitrary/indeterminate value; MySQL 8.0 enables ONLY_FULL_GROUP_BY by default and rejects the ambiguous query.

Why interviewers ask this: The old behaviour produced silently wrong reports (e.g., pairing the wrong name with a MAX salary). They want to see that you know the value is not guaranteed and how to write the query correctly.

Predict the Output

SELECT dept_id, name, MAX(salary) AS top_salary
FROM employees
GROUP BY dept_id;
MySQL Version Result
5.6 / 5.7 (default OFF) Runs, but wrong: returns a random name (e.g., 'Rumman' or 'Krushna') NOT necessarily the top earner
8.0 (ONLY_FULL_GROUP_BY ON) ERROR 1055 — nonaggregated column 'name' not in GROUP BY
The dangerous part: On old MySQL, the query might return dept 10 → 'Krushna' → 90000 — pairing Krushna's name with Rumman's salary. The name and the MAX come from different rows! This silently corrupts reports.

The Correct Way to Get "Top Earner Per Dept"

-- WRONG: name is ambiguous, may not match the MAX salary
SELECT dept_id, name, MAX(salary)
FROM employees GROUP BY dept_id;

-- CORRECT (MySQL 8.0+): window function ranks within each dept
SELECT dept_id, name, salary
FROM (
    SELECT dept_id, name, salary,
           ROW_NUMBER() OVER (PARTITION BY dept_id ORDER BY salary DESC) AS rn
    FROM employees
) t
WHERE rn = 1;

-- CORRECT (any version): correlated subquery matching the real top row
SELECT e.dept_id, e.name, e.salary
FROM employees e
WHERE e.salary = (
    SELECT MAX(e2.salary) FROM employees e2 WHERE e2.dept_id = e.dept_id
);
dept_idnamesalary
10Rumman90000
20Manjula80000

Now the name correctly matches the top salary in each department.

Legit exception — functional dependency: If you GROUP BY the primary key (e.g., GROUP BY emp_id), MySQL 8.0 allows selecting other columns, because they are functionally dependent on the PK and therefore unambiguous.
Interviewer follow-up: "How do you disable the error to make legacy code run?" → You can remove ONLY_FULL_GROUP_BY from sql_mode, but the correct answer is: don't — fix the queries instead, because the mode is protecting you from silently wrong results.