Lab-verifiedReproduced and fixed on Oracle AI Database 26ai Free 23.26.3.0.0 · 2026-11-30 · container image gvenzl/oracle-free:23-slim
ORA-00934 appears when an aggregate function (SUM, AVG, MAX, COUNT...) is written where Oracle is still working on single rows. WHERE filters rows before GROUP BY, so the group total does not exist yet at that point. UPDATE ... SET is the same: it works on one row at a time.
The * under the error points to the aggregate function that is in the wrong place.
01SUM in the WHERE clause to filter group totals
The query wanted customers whose total is 700 or more, and wrote WHERE SUM(amount) >= 700. WHERE runs before the rows are grouped, so the error pointed to SUM.
Without a filter, the totals were 800, 700 and 100. Moving the condition to HAVING SUM(amount) >= 700 returned only customers 1 (800) and 2 (700).
Setup
CREATE TABLE orders (order_id NUMBER, cust_id NUMBER, amount NUMBER);
INSERT INTO orders VALUES (1001, 1, 500);
INSERT INTO orders VALUES (1002, 1, 300);
INSERT INTO orders VALUES (1003, 2, 700);
INSERT INTO orders VALUES (1004, 3, 100);
COMMIT;Table created.
1 row created.
1 row created.
1 row created.
1 row created.
Commit complete.SELECT cust_id, SUM(amount) AS total
FROM orders
WHERE SUM(amount) >= 700
GROUP BY cust_id;WHERE SUM(amount) >= 700
*
ERROR at line 3:
ORA-00934: group function is not allowed here
Help: https://docs.oracle.com/error-help/db/ora-00934/SELECT cust_id, SUM(amount) AS total
FROM orders
GROUP BY cust_id
ORDER BY cust_id; CUST_ID TOTAL
---------- ----------
1 800
2 700
3 100
3 rows selected.Conditions on single rows go in WHERE, conditions on group results go in HAVING.
SELECT cust_id, SUM(amount) AS total
FROM orders
GROUP BY cust_id
HAVING SUM(amount) >= 700
ORDER BY cust_id; CUST_ID TOTAL
---------- ----------
1 800
2 700
2 rows selected.02Comparing each row with AVG in WHERE
WHERE amount > AVG(amount) tries to compare each order with the average of all orders in the same step. The * pointed to AVG.
The average itself was 400. Putting it in a subquery, WHERE amount > (SELECT AVG(amount) FROM orders), returned orders 1001 (500) and 1003 (700).
Setup
CREATE TABLE orders (order_id NUMBER, cust_id NUMBER, amount NUMBER);
INSERT INTO orders VALUES (1001, 1, 500);
INSERT INTO orders VALUES (1002, 1, 300);
INSERT INTO orders VALUES (1003, 2, 700);
INSERT INTO orders VALUES (1004, 3, 100);
COMMIT;Table created.
1 row created.
1 row created.
1 row created.
1 row created.
Commit complete.SELECT order_id, amount
FROM orders
WHERE amount > AVG(amount);WHERE amount > AVG(amount)
*
ERROR at line 3:
ORA-00934: group function is not allowed here
Help: https://docs.oracle.com/error-help/db/ora-00934/SELECT AVG(amount) FROM orders;AVG(AMOUNT)
-----------
400
1 row selected.The subquery is calculated first and gives one value, so the outer WHERE can compare each row with it.
SELECT order_id, amount
FROM orders
WHERE amount > (SELECT AVG(amount) FROM orders)
ORDER BY order_id; ORDER_ID AMOUNT
---------- ----------
1001 500
1003 700
2 rows selected.03MAX directly in UPDATE SET
UPDATE customers SET last_amount = MAX(amount) has no group to calculate MAX from, and amount is not even a column of CUSTOMERS. The error pointed to MAX.
The largest order per customer was 500 for customer 1 and 700 for customer 2. A correlated subquery, (SELECT MAX(o.amount) FROM orders o WHERE o.cust_id = c.cust_id), updated 2 rows with exactly those values.
Setup
CREATE TABLE orders (order_id NUMBER, cust_id NUMBER, amount NUMBER);
INSERT INTO orders VALUES (1001, 1, 500);
INSERT INTO orders VALUES (1002, 1, 300);
INSERT INTO orders VALUES (1003, 2, 700);
INSERT INTO orders VALUES (1004, 3, 100);
COMMIT;
CREATE TABLE customers (cust_id NUMBER, last_amount NUMBER);
INSERT INTO customers VALUES (1, NULL);
INSERT INTO customers VALUES (2, NULL);
COMMIT;Table created.
1 row created.
1 row created.
1 row created.
1 row created.
Commit complete.
Table created.
1 row created.
1 row created.
Commit complete.UPDATE customers
SET last_amount = MAX(amount);SET last_amount = MAX(amount)
*
ERROR at line 2:
ORA-00934: group function is not allowed here
Help: https://docs.oracle.com/error-help/db/ora-00934/SELECT cust_id, MAX(amount) FROM orders GROUP BY cust_id ORDER BY cust_id; CUST_ID MAX(AMOUNT)
---------- -----------
1 500
2 700
3 100
3 rows selected.A customer with no orders gets NULL from this subquery. The test was rolled back after checking the result.
UPDATE customers c
SET last_amount = (SELECT MAX(o.amount) FROM orders o WHERE o.cust_id = c.cust_id);
SELECT * FROM customers ORDER BY cust_id;
ROLLBACK;2 rows updated.
CUST_ID LAST_AMOUNT
---------- -----------
1 500
2 700
2 rows selected.
Rollback complete.04Window function SUM() OVER used directly in UPDATE SET
To store each department total on every row, SET dept_total = SUM(amount) OVER (PARTITION BY dept) was used. A window function is not allowed in UPDATE SET either, so the same ORA-00934 is raised (ROW_NUMBER() in the same place gives ORA-30483 instead).
Setup
CREATE TABLE sales (id NUMBER PRIMARY KEY, dept VARCHAR2(10), amount NUMBER, dept_total NUMBER);
INSERT INTO sales VALUES (1, 'A', 100, NULL);
INSERT INTO sales VALUES (2, 'A', 300, NULL);
INSERT INTO sales VALUES (3, 'B', 50, NULL);
COMMIT;Table created.
1 row created.
1 row created.
1 row created.
Commit complete.UPDATE sales
SET dept_total = SUM(amount) OVER (PARTITION BY dept);SET dept_total = SUM(amount) OVER (PARTITION BY dept)
*
ERROR at line 2:
ORA-00934: group function is not allowed here
Help: https://docs.oracle.com/error-help/db/ora-00934/MERGE INTO sales t
USING (SELECT id, SUM(amount) OVER (PARTITION BY dept) AS tot FROM sales) s
ON (t.id = s.id)
WHEN MATCHED THEN UPDATE SET t.dept_total = s.tot;
COMMIT;
SELECT id, dept, amount, dept_total FROM sales ORDER BY id;3 rows merged.
Commit complete.
ID DEPT AMOUNT DEPT_TOTAL
---------- ---------- ---------- ----------
1 A 100 400
2 A 300 400
3 B 50 50
3 rows selected.Compute the window SUM in a subquery and apply it with MERGE. Rows 1 and 2 (dept A) got 400 and row 3 (dept B) got 50.
SELECT id, dept, amount, dept_total FROM sales ORDER BY id; ID DEPT AMOUNT DEPT_TOTAL
---------- ---------- ---------- ----------
1 A 100 400
2 A 300 400
3 B 50 50
3 rows selected.FAQ
What is the difference between WHERE and HAVING in Oracle?
WHERE filters single rows before GROUP BY. HAVING filters groups after GROUP BY, so aggregate functions like SUM can be used there. Using SUM in WHERE gives ORA-00934.
Can I use an aggregate function in WHERE with a subquery?
Yes. In the test, WHERE amount > (SELECT AVG(amount) FROM orders) worked. The aggregate is inside the subquery, not directly in WHERE.
How do I update a column with MAX from another table?
Use a correlated subquery in SET: SET col = (SELECT MAX(...) FROM other o WHERE o.key = t.key). Writing MAX directly in SET gives ORA-00934.
What is the difference between ORA-00934 and ORA-00937?
ORA-00934 means the aggregate function itself is in a place where it is not allowed (WHERE, SET). ORA-00937 means the aggregate is in the SELECT list, but other plain columns are missing from GROUP BY.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary users created for the test; system is the DBA account. Oracle AI Database 26ai Free Release 23.26.3.0.0 - Develop, Learn, and Run for Free 23.26.3.0.0