SQL Server Error 329: Fix GROUP BY Expressions
In this article
Error 329 in SQL Server means your GROUP BY clause contains an expression that is not tied to a valid grouping: the usual message reads "Each GROUP BY expression must contain at least one column that is not an outer reference." You fix it by editing the query so every item in GROUP BY is a plain column reference (or a column inside a function), not a constant, an expression built only from outer-query columns, or a bare literal. SQL Server physically cannot group rows by something that has the same value for every row, so it rejects the statement. The fix is almost always a few seconds of editing once you find the offending item.
What You'll Need
- Access to the database with a login that can run SELECT queries (a read-only account is enough for fixing a reporting query).
- SQL Server Management Studio (SSMS), Azure Data Studio, or any client that shows the full error message and lets you edit and re-run the query.
- The text of the failing query, ideally saved so you can compare working and broken versions.
- Knowledge of the table columns involved, or permission to run a
SELECT TOP 10 *against the table to inspect them.
Step-by-Step
- Read the full error, not just the number. Error 329 is raised at parse or compile time and names the problem in one sentence. In SSMS the message pane shows the exact wording; double-click it if a line number is highlighted. Confirm the message mentions "outer reference" or "at least one column", because other GROUP BY complaints (such as column not contained in an aggregate) have different numbers and different fixes.
- Isolate the failing statement. Comment out everything after the SELECT that ends in GROUP BY, or copy the query into a new window and keep only the SELECT list, FROM, WHERE and GROUP BY clauses. If the query has several statements, run them one at a time so you know exactly which one throws 329.
- Look at the GROUP BY clause item by item. Write the list out and check each entry against three questions: is it a column from a table in the FROM clause, is it wrapped in a function such as
YEAR(OrderDate), or is it something else such as'All',1,NULL, a CASE expression referencing only outer-query columns, or a parameter like@Region. The last category is what triggers 329. - Remove or replace constants and parameters in GROUP BY. A literal like
GROUP BY 'Total'is meaningless because every row groups to the same value. Delete it. If the parameter is genuinely needed to filter results, it belongs in WHERE, not GROUP BY. If the query is dynamic SQL and the parameter is being concatenated into the group list, fix the string building so only column names are inserted. - Handle the correlated-subquery case. The classic cause is a subquery in the SELECT list that groups by a column from the outer query, for example
SELECT (SELECT COUNT(*) FROM b GROUP BY a.id) FROM a. The outer columna.idis an outer reference inside the subquery, so it cannot be a grouping key there. Rewrite it as a correlated aggregate without GROUP BY, such asSELECT (SELECT COUNT(*) FROM b WHERE b.aid = a.id) FROM a, or move the aggregation into a derived table and join it back. - Keep the SELECT list legal too. Once GROUP BY is valid, every non-aggregated column in SELECT must appear in GROUP BY. Replace loose columns with
MIN(),MAX()or another aggregate, or add them to GROUP BY. If two columns are functionally dependent (for example a primary key plus another column from the same table), SQL Server may still require both, so list both. - Re-run and compare row counts. Run the fixed query, then sanity-check totals against an ungrouped
COUNT(*)with the same WHERE clause. A query that compiles but returns the wrong number of groups usually means you added a column to GROUP BY that split rows you intended to combine. - Save the corrected version. Store it in source control or replace the old view, stored procedure or report definition so the broken statement does not resurface. If the query lives in a view, use
ALTER VIEW; for a procedure useALTER PROCEDURE, then refresh dependent code and any cached execution plans.
Common Mistakes
- Adding the offending item to SELECT instead of fixing it. Error 329 is about GROUP BY validity; copying the constant into the SELECT list just creates a second problem.
- Assuming the error is about the aggregate function. It is not. COUNT, SUM and AVG are fine; the grouping key is what SQL Server is objecting to.
- Confusing 329 with "Column is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause" (error 8120). That one is about the SELECT list. Fixing 8120 by adding columns to GROUP BY can sometimes expose 329 if one added item is a constant.
- Wrapping a constant in a function to satisfy the parser.
GROUP BY CAST('x' AS varchar(10))is still a constant expression with no grouping column and will still fail or produce one meaningless group. - Relying on SELECT aliases in GROUP BY. SQL Server does not accept column aliases in GROUP BY, so repeating an alias instead of the underlying expression can produce confusing errors. Use the real expression.
- Patching dynamic SQL with string concatenation of user input. Even when it compiles, this opens injection risk; use parameterised WHERE clauses and never splice user text into the GROUP BY list.
Tips
- Test the query against a small table or a filtered subset first. Parse errors appear instantly, but the logic error in a rewritten query can be far more expensive to spot on millions of rows.
- Format the GROUP BY list one item per line while debugging. Most 329 cases are a single stray literal that is invisible on a long single-line query.
- If you are generating the grouping from a reporting tool, check whether it emits a dummy constant to keep the GROUP BY clause non-empty when the user selects no groups. That pattern is a frequent source of 329, and the fix belongs in the tool's query template.
- Use
SET SHOWPLAN_ONLYor the estimated execution plan to compile the query without running it, which confirms the fix without scanning data. - Keep both original and fixed definitions of views and procedures so you can roll back quickly if the rewrite changes reporting numbers.
Troubleshooting
The error persists after removing the obvious constant. Look for a second GROUP BY clause in a subquery or a UNION branch. Error 329 is raised for the first failing scope, but after you fix one, another can surface.
The message mentions an outer reference specifically. Check every subquery in the SELECT list, HAVING and ORDER BY. Any grouped subquery that references an outer-table column is suspect; convert it to a scalar correlated subquery without GROUP BY or to a joined derived table.
A view fails but the same query text works. The view may be bound to schema (SCHEMABINDING) or was created before a column change. Refresh dependencies with sp_refreshview or sp_refreshsqlmodule, then re-run.
It only fails in the application, not in SSMS. The app may build the statement with a parameterised GROUP BY or send a different batch. Capture the exact SQL with an Extended Events session or Profiler and inspect the grouping clause as actually sent.
It fails after an upgrade or migration. Compare the old and new query text, and check compatibility level if behaviour seems inconsistent; note that sp_refreshsqlmodule only refreshes stored definitions and does not change query logic.
Frequently Asked Questions
What exactly does SQL Server error 329 mean?
It means a GROUP BY expression has no real column to group by — for example a literal, a parameter, or a reference to the outer query inside a subquery — so SQL Server cannot form groups from it.
Can I group by a computed expression like YEAR(OrderDate)?
Yes. A function applied to a column from a table in the FROM clause is a valid grouping expression because it resolves to actual row data.
Is error 329 the same as error 8120?
No. 329 is about the GROUP BY list; 8120 is about a non-aggregated column in the SELECT list. Both often appear together in a badly written aggregate query.
Does GROUP BY 1 work as an ordinal position shortcut?
SQL Server does not support ordinal positions in GROUP BY, and a numeric literal there is treated as a constant, which is exactly the pattern that raises 329. Use the column name instead.
Will this error occur in Azure SQL Database and Managed Instance too?
Yes. The same engine rule applies across SQL Server, Azure SQL Database, Azure SQL Managed Instance and SQL Server on Linux, so the fix is identical.