This error occurs when you mix aggregate functions (MIN, MAX, COUNT) with non-aggregated columns in SELECT without a GROUP BY clause. Fix it by adding GROUP BY for all non-aggregated columns, using ANY_VALUE(), or ensuring functional dependency on a primary key.
MySQL Error 1140 is raised by the strict ONLY_FULL_GROUP_BY SQL mode (enabled by default since MySQL 5.7) to prevent queries that would return non-deterministic results. When you use an aggregate function like COUNT(), SUM(), MIN(), or MAX() in your SELECT list but also select non-aggregated columns, MySQL cannot determine which single value to return for those non-aggregated columns when multiple rows are involved. The error prevents ambiguous queries that could produce unpredictable or incorrect results.
Review your SELECT statement to locate which columns are aggregate functions and which are plain columns. The error message will tell you the expression number and column name causing the issue.
-- WRONG: Mixing aggregate (COUNT) with non-aggregate (name)
SELECT name, COUNT(*) FROM users;
-- This fails because MySQL cannot determine which user name to returnThe most straightforward fix is to add a GROUP BY clause that includes all non-aggregated columns in your SELECT list. This makes the query deterministic by explicitly telling MySQL how to group the data.
-- CORRECT: GROUP BY includes the non-aggregated column
SELECT name, COUNT(*) FROM users GROUP BY name;
-- Now MySQL knows to count occurrences of each distinct nameIf you select multiple non-aggregated columns, include all of them in GROUP BY:
SELECT department, job_title, COUNT(*)
FROM employees
GROUP BY department, job_title;If you need to select a non-aggregated column but its specific value doesn't matter (because all values in the group are the same), wrap it in ANY_VALUE(). This tells MySQL: "I know this column isn't grouped, but I accept any value from the group".
-- Using ANY_VALUE() when you don't care which specific value is returned
SELECT department, ANY_VALUE(office_location), COUNT(*)
FROM employees
GROUP BY department;
-- Valid because all employees in a department share the same office_locationUse ANY_VALUE() carefully—only when values are guaranteed to be the same within each group.
If a non-aggregated column is functionally dependent on a GROUP BY column (typically because it's a primary key or unique key), MySQL recognizes this as valid without requiring GROUP BY.
-- CORRECT: user_id is the primary key, so all columns from the same user_id are deterministic
SELECT u.id, u.name, u.email, COUNT(o.id) as order_count
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
GROUP BY u.id;
-- Valid because u.name and u.email are functionally dependent on u.id (the primary key)While you can disable the ONLY_FULL_GROUP_BY SQL mode, this is not recommended for production environments:
-- NOT RECOMMENDED - only for temporary testing
SET @@global.sql_mode := REPLACE(@@global.sql_mode, 'ONLY_FULL_GROUP_BY', '');Disabling this mode allows queries to run but produces non-deterministic results—the server arbitrarily chooses which value to return for non-grouped columns. This can cause:
- Data inconsistencies
- Unpredictable application behavior
- Hidden bugs that are difficult to debug
- Compatibility issues with modern MySQL standards
Instead, fix the query to be compliant with the strict mode.
Window functions are an alternative solution for complex scenarios. If you need to display aggregate values alongside individual row data, use window functions like ROW_NUMBER() or RANK():
SELECT id, name, salary, AVG(salary) OVER (PARTITION BY department) as dept_avg
FROM employees;This returns each individual employee row with the department average, without requiring GROUP BY.
Functional dependency rules are strict: a column must be a PRIMARY KEY, UNIQUE NOT NULL, or deterministically derived from a primary/unique key. Regular foreign keys or non-unique columns do not qualify for functional dependency.
The ONLY_FULL_GROUP_BY mode is part of MySQL's "strict" SQL compliance. Understanding GROUP BY semantics is crucial for writing portable, correct SQL across different database systems.
ERROR 1064: You have an error in your SQL syntax
How to fix "ERROR 1064: You have an error in your SQL syntax" in MySQL
ERROR 1054: Unknown column in field list
Unknown column in field list
ER_WINDOW_RANGE_FRAME_NUMERIC_TYPE (3589): RANGE frame requires numeric ORDER BY expression
RANGE frame requires numeric ORDER BY expression in MySQL window functions
CR_ALREADY_CONNECTED (2058): Handle already connected
How to fix "CR_ALREADY_CONNECTED (2058): Handle already connected" in MySQL
ER_WINDOW_DUPLICATE_NAME (3591): Duplicate window name
How to fix ER_WINDOW_DUPLICATE_NAME (3591) in MySQL window functions