Fix: ‘Column is invalid in the select list’

The error “Column is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause” occurs in SQL when a SELECT statement includes columns that aren’t in an aggregate function or the GROUP BY clause.

Error “Column is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause”

This error means that if you’re using GROUP BY to summarize data, any non-aggregated columns must be in the GROUP BY list. If they aren’t, SQL Server will return an error because it doesn’t know which value to show for those fields.

This usually happens when you select columns not part of the aggregation in a GROUP BY query. It can also occur due to incorrect grouping or by failing to include all non-aggregated fields in the GROUP BY clause.

Two key terms, “Group by” and “aggregate function,” are central to solving SQL errors. Here’s how to use them:

GROUP BY Clause:

The “GROUP BY” clause helps summarize or aggregate data, such as calculating total profits, sales, or counting students in a department. For instance, it can be used to sum daily sales figures.

GROUP BY Split-Apply-Combine Strategy:

GROUP BY” employs a “split-apply-combine” strategy:

  • Split: Divides data into groups.
  • Apply: Uses aggregate functions (e.g., SUM, AVG) on each group.
  • Combine: Merges these results into one output.
SPLIT_APPLY_COMBINE strategy sample
“SPLIT_APPLY_COMBINE” strategy sample

In the figure above, column C1 is divided into groups, aggregate functions are applied, and a single value is assigned to each group.

Here’s an example using a database named “appuals.”

Database creation
Database creation

Example:

Create a table called “employee” using this code:

USE [appuals]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[employee](
    [e_id] [int] NOT NULL,
    [e_ename] [varchar](50) NULL,
    [dep_id] [int] NULL,
    [salary] [int] NULL,
    CONSTRAINT [PK_employee] PRIMARY KEY CLUSTERED
    (
        [e_id] ASC
    )
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
Employee table creation
Employee table creation

Insert data into the table:

Insert into employee(e_id, e_ename, dep_id, salary)
values(101, 'Sadia', 1, 6000),
(102, 'Saba', 1, 5000),
(103, 'Sana', 2, 4000),
(104, 'Hammad', 2, 3000),
(105, 'Umer', 3, 4000),
(106, 'Kanwal', 3, 2000)

Result:

Data insertion into 'employee' table
Data insertion into table “employee”

Next, view all data:

select * from employee

Output:

The output from the employee table
The output from the employee table.

Group by department:

select dep_id, salary from employee group by dep_id

Error: Column ’employee.salary’ is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

This error occurs because the “employee.salary” column is neither in the GROUP BY clause nor in an aggregate function.

Error: Column 'employee.salary' is invalid in the select list
Error: Column ’employee.salary’ is invalid in the select list

Solution:

Use GROUP BY and an aggregate function to calculate the average salary for each department:

select dep_id, avg(salary) as average_salary from employee group by dep_id
Find the average salary of employees in each department
Find the average salary of employees in each department

Depicted via split-apply-combine:

SPLIT-APPLY-COMBINE strategy applied on employee table
SPLIT-APPLY-COMBINE strategy applied on employee table

Aggregate Functions:

  • Sum(): Total of each group.
  • Count(): Number of rows in each group.
  • Avg(): Mean of each group.
  • Min(): Minimum value of each group.
  • Max(): Maximum value of each group.

Using GROUP BY and Aggregate Functions:

Now, a practical example:

Create a table “people“:

USE [appuals]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[people](
    [id] [bigint] IDENTITY(1,1) NOT NULL,
    [name] [varchar](500) NULL,
    [city] [varchar](500) NULL,
    [state] [varchar](500) NULL,
    [age] [int] NULL
) ON [PRIMARY]
GO
Table creation
Table creation

Insert data:

insert into people(name, city, state, age)
values
('Meggs', 'MONTEREY','CA',20),
('Staton','HAYWARD', 'CA',22),
('Irons', 'IRVINE' ,'CA',25),
('Krank', 'PLEASANT', 'IA',23),
('Davidson' ,'WEST BURLINGTON', 'IA',40),
('Pepewachtel' ,'FAIRFIELD' ,'IA',35),
('Schmid', 'HILLSBORO', 'OR',23),
('Davidson', 'CLACKAMAS', 'OR',40),
('Condy','GRESHAM','OR',35)

Output:

Data insertion into 'people' table
Data insertion into “people” table

To find the number of residents and their average age by state, use:

select state, avg(age) as Age, count(*) as no_of_residents from people group by state

Error: Column ‘people.age’ is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

This error arises when the “people.age” column is not part of the GROUP BY clause nor included in an aggregate function.

Grouping by state causes an error

Logical Solution:

To fix the query, include the specific aggregation for the “age” column, like the average age:

select state, avg(age) as Age, count(*) as no_of_residents from people group by state

This executes correctly and provides:

Result: number of residents and average age by state.
Result: number of residents and average age by state.

Consider these points when using GROUP BY to avoid errors:

  • The GROUP BY clause comes after the WHERE clause and before the ORDER BY clause.
  • Use the WHERE clause to filter rows before applying “GROUP BY”.
  • If a column contains NULL values, they are treated as one group.

GROUP BY and NULL Values:

Insert a row with NULL in the “state” column:

insert into people(name, city, state, age) values ('Kanwal', 'GRESHAM', '', 35)
Adding NULL/empty value to the column for GROUP BY
Adding NULL/empty value to the column for GROUP BY

Now, execute:

select state, avg(age) as Age, count(*) as no_of_residents from people group by state

Output:

Empty value in the column is considered a single group
Empty value in the column is considered a single group

Add more NULLs:

insert into people(name, city, state, age)
values ('Kanwal', 'IRVINE', 'NULL', 35), ('Krank', 'PLEASANT', 'NULL', 23)
Inserting NULL value in the column for GROUP BY
Inserting NULL value in the column for GROUP BY.

Re-execute the query:

'NULL' in the column is considered a single group
“NULL” in the column is considered a single group

In summary, empty or NULL entries are treated as separate groups within the “GROUP BY” operation.

ABOUT THE AUTHOR

Kevin Arrows


Kevin Arrows is a highly experienced and knowledgeable technology specialist with over a decade of industry experience. He holds a Microsoft Certified Technology Specialist (MCTS) certification and has a deep passion for staying up-to-date on the latest tech developments. Kevin has written extensively on a wide range of tech-related topics, showcasing his expertise and knowledge in areas such as software development, cybersecurity, and cloud computing. His contributions to the tech field have been widely recognized and respected by his peers, and he is highly regarded for his ability to explain complex technical concepts in a clear and concise manner.