How to Fix MySQL Error 1057 (ER_WRONG_SUM_SELECT)
You get mysql error 1057 when a query mixes sum functions and plain columns without grouping. You can fix it by adding a group clause or removing the ungrouped columns. The database engine throws the symbol ER_WRONG_SUM_SELECT and returns SQLSTATE 42000 for this syntax problem. The server returns a clear message that the statement has sum functions and columns together. Your query fails right away before the engine reads any table rows. Why mysql error 1057 happens in the database engine A s
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
You get mysql error 1057 when a query mixes sum functions and plain columns without grouping. You can fix it by adding a group clause or removing the ungrouped columns.
The database engine throws the symbol ER_WRONG_SUM_SELECT and returns SQLSTATE 42000 for this syntax problem. The server returns a clear message that the statement has sum functions and columns together. Your query fails right away before the engine reads any table rows.
Why mysql error 1057 happens in the database engine
A sum function takes many rows and turns them into one single value. A plain column tries to show one value for every single row in the table. When you put both in one query without grouping, the database gets confused. It does not know which single row value to pair with the total sum.
Strict SQL rules block this mix to stop bad data from reaching your app. Without this check, the server would just guess and pick a random row value. That guess leads to wrong reports and broken finance numbers in your app. The parser spots this mismatch early and stops the query with code 1057.
This issue often shows up when developers write quick report queries by hand. It also happens when an app builds queries with missing grouping parts. Sometimes a database upgrade turns on stricter SQL rules that catch old broken queries.
How to find the broken query in production
First, check your app error logs to find the exact query that failed. Look for the ER_WRONG_SUM_SELECT symbol or the 42000 state code in the log entries. Copy the broken query text into a safe test tool so you can read it.
Read the list of items you ask the database to return in that query. Mark every item that uses a math function like a sum, count, or average. Next, mark every plain table column that sits outside of those math functions. Look at the end of the query to see if a grouping clause exists.
MySQL Access Governance + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
If the grouping clause is missing, you have found the root cause of the crash. If a grouping clause is there, check if it lists every plain column. Any plain column left out of the grouping list will trigger this error.
How to fix the query without turning off safety checks
Do not fix this problem by turning off strict SQL rules on your server. Weakening server rules hides the error but gives you random, wrong data in return. Never grant admin rights to developers just so they can change server modes.
Instead, fix the query text itself using one of three safe paths. The first path is to add a grouping clause at the end of your query. List every plain column from your select list inside that new grouping clause. Now the database will calculate a separate sum for each unique group of columns.
The second path works when you only want one grand total for the whole table. Simply delete the plain columns from the query and keep only the sum functions. The query will run fast and return one single row with your totals.
The third path is to wrap the plain columns inside a summary function too. For example, you can ask for the minimum or maximum value of that column. You can also use a window function if you need both row details and totals. Test your fixed query on a staging database before you deploy it to production. When teams run ad-hoc fixes against live data, data masking can rewrite matched fields like emails, phone numbers, card numbers, or national IDs in results.
Check MySQL queries and results on the wire
MySQL grants control who logs in. They do not check single queries or mask sensitive columns once a session starts. With hoop.dev, you run an open-source proxy, a sidecar for your MySQL, inline in the MySQL wire protocol. On every query an AI agent or human runs against production MySQL, RDS, or Aurora, hoop.dev decides to pass, mask, block, or hold it for review.
Multi-statement batches and prepared queries are split and checked one by one by hoop.dev guardrails, which refuse banned queries with a native MySQL error or hold risky ones for approval. On the response path, hoop.dev data masking rewrites string values in returned rows in memory, and every decision is logged. See the MIT-licensed project on GitHub.
Frequently asked questions
What does the ER_WRONG_SUM_SELECT message mean in plain words?
It means your query asks for both a single summary total and full row details. The database cannot fit both shapes of data into one result grid without grouping.
Can I fix code 1057 by changing the server SQL mode?
You should never change the server SQL mode to hide this syntax error. Doing so forces the engine to pick random row values, which corrupts your report numbers.
Why did my query work in an older database version but fail now?
Older database setups often allowed loose grouping rules by default and guessed the missing values. Modern setups turn on strict checks by default and reject queries with ungrouped columns.
Related guides
In this post