ORA-00937: not a single-group group function

ORA-00937: not a single-group group function

Oracle PL/SQL error message: ORA-00937: not a single-group group function.

Cause:

A SELECT list cannot include both a group function, such as AVG, COUNT, MAX, MIN, SUM, STDDEV, or VARIANCE, and an individual column expression, unless the individual column expression is included in a GROUP BY clause.

Solution:

Drop either the group function or the individual column expression from the SELECT list or add a GROUP BY clause that includes all individual column expressions listed.

Students table

STUDENT_ID FIRST_NAME LAST_NAME
1 Daniel SCOTT
2 Anthony SIMMONS
3 Sophia THOMPSON
4 Emily PETERSON
5 David DAWSON
6 Gabriel LEWIS

Orders table

ORDER_ID COURSE_ID STUDENT_ID
1 1 6
3 4 5
21 5 6
22 1 4

Example:

select s.student_id, s.first_name, 
s.last_name, count(o.order_id) No
from students s, orders o 
where s.student_id = o.student_id
order by s.student_id;

Output:

ORA-00937: not a single-group group function

Correct:

select s.student_id, s.first_name, 
s.last_name, count(o.order_id) No
from students s, orders o 
where s.student_id = o.student_id
group by s.student_id, s.first_name, 
s.last_name, o.order_id
order by s.student_id;

Output:

STUDENT_ID FIRST_NAME LAST_NAME NO
4 Emily PETERSON 1
5 David DAWSON 1
6 Gabriel LEWIS 1
6 Gabriel LEWIS 1