FB

ORA-00979 ErrorHave you tried to run a query and got the “ORA-00979: not a group by expression” error? Learn what it is and how to resolve the error in this article.

ORA-00979: not a group by expression

The ORA-00979 error happens when you have at least one column in your SELECT clause that is not in your GROUP BY expression when you are using an aggregate function. Common aggregate functions include SUM, AVG, MIN, MAX, and COUNT. Any column or expression in your SELECT clause must also be listed in the GROUP BY clause.

Here’s an example of a query that will generate the error:

SELECT first_name, last_name, COUNT(*)
FROM student
GROUP BY first_name;

Result:

ORA-00979: not a GROUP BY expression

 

Why Do I Get The ORA-00979 Error?

This error happens because you’re using an aggregate function, and there is at least one column in the SELECT clause that is not in the GROUP BY clause.

Using the example query above:

SELECT first_name, last_name, COUNT(*)
FROM student
GROUP BY first_name;

Because I use an aggregate function (COUNT), I need to define all of the columns in a GROUP BY clause that are in the SELECT clause.

In this example, I have specified the first_name and last_name column in the SELECT clause, but the last_name column is not in the GROUP BY clause.

Even if I have some fields in the GROUP BY clause, if I don’t specify all of the fields from the SELECT clause, I’ll still get an error.

For example:

SELECT first_name, last_name, address_state, COUNT(*)
FROM student
GROUP BY first_name, last_name

Result:

ORA-00979: not a GROUP BY expression

As you can see, this will still give me an error.

Why does Oracle give an error?

Because, if you don’t have a GROUP BY but you want to SELECT the column, Oracle doesn’t know what value to show when using this aggregate function. Should it show the first value? The last value? A random value?

 

How To Resolve the ORA-00979 Error

To resolve the ORA-00979: not a group by expression error, simply ensure that all of the GROUP BY columns match the SELECT clause.

You can do this by adding columns to the GROUP BY.

So, using the example above:

SELECT first_name, last_name, COUNT(*)
FROM student
GROUP BY first_name, last_name;

Result:

FIRST_NAME LAST_NAME COUNT(*)
John Smith 2
Mark Anderson 1
Michael Condor 1
Brendan Jefferson 1
Peter Stark 1
Sally Lincoln 1
Michelle Brumby 1
Amy Ford 1
Rose Minson 1
Tina Mitchell 1

Or, using the second example:

SELECT first_name, last_name, address_state, COUNT(*)
FROM student
GROUP BY address_state, first_name, last_name

Result:

FIRST_NAME LAST_NAME ADDRESS_STATE COUNT(*)
John Smith California 1
Mark Anderson Texas 1
Michael Condor Florida 1
Brendan Jefferson Florida 1
Peter Stark Illinois 1
Sally Lincoln California 1
Michelle Brumby Texas 1
Amy Ford Texas 1
Rose Minson California 1
Tina Mitchell Illinois 1
John Smith Texas 1

The columns don’t need to be in the same order to correct the error. They just need to be present.

One thing to remember with a GROUP BY clause, is if you give your columns aliases, you need to specify the original column or expression.

So, if you have a query like this:

SELECT first_name + ' ' + last_name AS full_name, COUNT(*)
FROM student
GROUP BY full_name;

Result:

ORA-00904: “FULL_NAME”: invalid identifier

You get an error, because you can’t refer to a column alias within the GROUP BY clause. You’ll need to use a query like this:

SELECT first_name + ' ' + last_name AS full_name, COUNT(*)
FROM student
GROUP BY first_name + ' ' last_name;
FULL_NAME COUNT(*)
John Smith 2
Mark Anderson 1
Michael Condor 1
Brendan Jefferson 1
Peter Stark 1
Sally Lincoln 1
Michelle Brumby 1
Amy Ford 1
Rose Minson 1
Tina Mitchell 1

 

Conclusion

So, in conclusion, the ORA-00979: not a group by expression error happens because the columns in the SELECT clause don’t match the columns in the GROUP BY clause. To resolve the error, make sure the columns match.

Lastly, if you enjoy the information and career advice I’ve been providing, sign up to my newsletter below to stay up-to-date on my articles. You’ll also receive a fantastic bonus. Thanks!

Get Your FREE PDF: "5 Ways to Avoid SQL Errors"

Get Your FREE PDF: "5 Ways to Avoid SQL Errors"