실제 DB 검증Oracle AI Database 26ai Free 23.26.3.0.0에서 재현·해결 확인 · 2026-11-30 · 컨테이너 이미지 gvenzl/oracle-free:23-slim
ORA-00934는 집계 함수(SUM, AVG, MAX, COUNT 등)를 오라클이 아직 한 행씩 처리하는 자리에 썼을 때 나요. WHERE는 GROUP BY 전에 행을 거르기 때문에 그 시점엔 그룹 합계가 없어요. UPDATE ... SET도 한 행씩 처리하니 마찬가지예요.
에러 출력의 *가 잘못된 자리에 있는 집계 함수를 가리켜요.
01그룹 합계 조건을 WHERE에 SUM으로 씀
합계가 700 이상인 고객만 보려고 WHERE SUM(amount) >= 700을 썼어요. WHERE는 그룹을 묶기 전에 실행되니 SUM에서 에러가 났어요.
조건 없이 보면 합계는 800, 700, 100이에요. 조건을 HAVING SUM(amount) >= 700으로 옮기니 고객 1(800)과 2(700)만 나왔어요.
사전 준비
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.행 하나에 대한 조건은 WHERE, 그룹 결과에 대한 조건은 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.02WHERE에서 각 행을 AVG와 비교
WHERE amount > AVG(amount)는 각 주문을 전체 평균과 같은 단계에서 비교하려는 거라 안 돼요. *가 AVG를 가리켰어요.
평균은 400이에요. 이걸 서브쿼리로 빼서 WHERE amount > (SELECT AVG(amount) FROM orders)로 쓰니 주문 1001(500)과 1003(700)이 나왔어요.
사전 준비
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.서브쿼리가 먼저 계산돼 값 하나가 되니, 바깥 WHERE가 각 행과 비교할 수 있어요.
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.03UPDATE SET에 MAX를 바로 씀
UPDATE customers SET last_amount = MAX(amount)는 MAX를 계산할 그룹이 없고, amount는 CUSTOMERS의 컬럼도 아니에요. 에러가 MAX를 가리켰어요.
고객별 최대 주문은 고객 1이 500, 고객 2가 700이에요. 상관 서브쿼리 (SELECT MAX(o.amount) FROM orders o WHERE o.cust_id = c.cust_id)로 바꾸니 2행이 딱 그 값으로 바뀌었어요.
사전 준비
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.주문이 없는 고객은 이 서브쿼리 결과가 NULL이 돼요. 검증에서는 결과 확인 뒤 ROLLBACK 했어요.
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.04UPDATE SET에 윈도우 함수 SUM() OVER를 바로 씀
부서 합계를 행마다 저장하려고 SET dept_total = SUM(amount) OVER (PARTITION BY dept)를 썼어요. UPDATE SET에는 윈도우 함수도 못 써서 같은 ORA-00934가 나요(같은 자리에 ROW_NUMBER()를 쓰면 ORA-30483).
사전 준비
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.윈도우 SUM은 서브쿼리에서 계산하고 MERGE로 넣었어요. A 부서 1·2번 행은 400, B 부서 3번 행은 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.자주 묻는 질문
오라클 WHERE와 HAVING 차이가 뭐예요?
WHERE는 GROUP BY 전에 행을 거르고, HAVING은 GROUP BY 뒤에 그룹을 걸러요. 그래서 SUM 같은 집계 함수는 HAVING에서만 쓸 수 있고, WHERE에 쓰면 ORA-00934가 나요.
WHERE 절에서 집계 함수를 쓰려면 어떻게 해요?
서브쿼리 안에 넣으면 돼요. 검증에서 WHERE amount > (SELECT AVG(amount) FROM orders)는 정상 실행됐어요.
다른 테이블의 MAX 값으로 UPDATE 하려면요?
SET col = (SELECT MAX(...) FROM 다른테이블 o WHERE o.키 = t.키)처럼 상관 서브쿼리를 써요. SET에 MAX를 바로 쓰면 ORA-00934가 나요.
ORA-00934와 ORA-00937은 뭐가 달라요?
ORA-00934는 집계 함수를 쓸 수 없는 자리(WHERE, SET)에 쓴 경우예요. ORA-00937은 SELECT 목록에 집계 함수와 일반 컬럼을 같이 쓰고 GROUP BY를 빠뜨린 경우예요.
검증 방법
시나리오마다 새 DB 사용자를 만들어 시작해요. 스크립트가 사전 준비 → 에러 재현(에러 코드 일치 확인) → 해결 → 원래 문장 재실행 순서로 실행하고, 화면의 출력은 DB가 실제로 낸 결과를 스크립트가 그대로 저장한 거예요.
lab_a, lab_b는 테스트용 일반 사용자, system은 DBA 계정이에요. Oracle AI Database 26ai Free Release 23.26.3.0.0 - Develop, Learn, and Run for Free 23.26.3.0.0