실제 DB 검증MySQL 8.4.13에서 재현·해결 확인 · 2026-12-01 · 컨테이너 이미지 mysql:8.4
MySQL 8.4 기본 sql_mode에 들어 있는 ONLY_FULL_GROUP_BY가 켜져 있으면 SELECT·HAVING·ORDER BY의 컬럼은 GROUP BY에 있거나, 집계되거나, 그룹 컬럼에 종속(예: 기본 키)이어야 해요. 아니면 1055가 나고, 메시지에 어느 식(SELECT 목록 #2, ORDER BY #1 등)이 문제인지 나와요.
메시지는 lc_messages를 한국어로 해도 영어로 나와요. MySQL 8.4에서 확인한 5가지 경우예요.
01SELECT에 GROUP BY에 없는 컬럼을 추가
GROUP BY customer_id인데 SELECT에 customer_name을 넣었어요. orders의 일반 컬럼이라 고객마다 값이 하나라고 MySQL이 보장할 수 없어요.
사전 준비
CREATE TABLE orders (id INT PRIMARY KEY, customer_id INT, customer_name VARCHAR(30), amount INT);
INSERT INTO orders VALUES (1,10,'Kim',100),(2,10,'Kim',200),(3,20,'Lee',50);Query OK, 0 rows affected (0.02 sec)
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0SELECT customer_id, customer_name, SUM(amount) AS total
FROM orders GROUP BY customer_id;ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'lab_a.orders.customer_name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_bySELECT @@sql_mode;+-----------------------------------------------------------------------------------------------------------------------+
| @@sql_mode |
+-----------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
+-----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)SELECT @@sql_mode로 ONLY_FULL_GROUP_BY가 켜진 걸 확인했어요. 그룹마다 값이 하나뿐이면 GROUP BY에 넣거나(GROUP BY customer_id, customer_name) ANY_VALUE(customer_name)로 감싸세요. 둘 다 Kim 300, Lee 50이 나와요.
SELECT customer_id, customer_name, SUM(amount) AS total
FROM orders GROUP BY customer_id, customer_name;
SELECT customer_id, ANY_VALUE(customer_name) AS customer_name, SUM(amount) AS total
FROM orders GROUP BY customer_id;+-------------+---------------+-------+
| customer_id | customer_name | total |
+-------------+---------------+-------+
| 10 | Kim | 300 |
| 20 | Lee | 50 |
+-------------+---------------+-------+
2 rows in set (0.00 sec)
+-------------+---------------+-------+
| customer_id | customer_name | total |
+-------------+---------------+-------+
| 10 | Kim | 300 |
| 20 | Lee | 50 |
+-------------+---------------+-------+
2 rows in set (0.00 sec)02MAX()와 다른 컬럼으로 그룹별 최신 행을 뽑으려 함
SELECT customer_id, MAX(order_date), amount ... GROUP BY customer_id는 최근 주문 금액이 나올 것 같지만, amount가 MAX 행과 묶여 있지 않아서 거부돼요(Expression #3).
사전 준비
CREATE TABLE orders (id INT PRIMARY KEY, customer_id INT, order_date DATE, amount INT);
INSERT INTO orders VALUES (1,10,'2026-11-01',100),(2,10,'2026-11-20',200),(3,20,'2026-11-05',50),(4,20,'2026-11-03',70);Query OK, 0 rows affected (0.02 sec)
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0SELECT customer_id, MAX(order_date) AS last_date, amount
FROM orders GROUP BY customer_id;ERROR 1055 (42000): Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'lab_a.orders.amount' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_bySELECT customer_id, MAX(order_date) AS last_date FROM orders GROUP BY customer_id;+-------------+------------+
| customer_id | last_date |
+-------------+------------+
| 10 | 2026-11-20 |
| 20 | 2026-11-05 |
+-------------+------------+
2 rows in set (0.01 sec)ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date DESC)로 고객별 번호를 매기고 rn = 1만 남기세요. 고객 10은 2026-11-20·200, 고객 20은 2026-11-05·50으로 실제 최근 주문이 나와요.
SELECT customer_id, order_date AS last_date, amount FROM (
SELECT customer_id, order_date, amount,
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date DESC) AS rn
FROM orders) t
WHERE rn = 1;+-------------+------------+--------+
| customer_id | last_date | amount |
+-------------+------------+--------+
| 10 | 2026-11-20 | 200 |
| 20 | 2026-11-05 | 50 |
+-------------+------------+--------+
2 rows in set (0.00 sec)03그룹으로 묶지 않은 컬럼으로 ORDER BY
SELECT 목록은 괜찮은데 ORDER BY order_date는 고객마다 값이 여러 개인 컬럼으로 정렬해요. 에러에 Expression #1 of ORDER BY clause라고 나와요.
사전 준비
CREATE TABLE orders (id INT PRIMARY KEY, customer_id INT, order_date DATE, amount INT);
INSERT INTO orders VALUES (1,10,'2026-11-01',100),(2,10,'2026-11-20',200),(3,20,'2026-11-05',50);Query OK, 0 rows affected (0.02 sec)
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0SELECT customer_id, SUM(amount) AS total
FROM orders GROUP BY customer_id
ORDER BY order_date DESC;ERROR 1055 (42000): Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'lab_a.orders.order_date' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_bySELECT customer_id, MAX(order_date) AS last_date, SUM(amount) AS total FROM orders GROUP BY customer_id;+-------------+------------+-------+
| customer_id | last_date | total |
+-------------+------------+-------+
| 10 | 2026-11-20 | 300 |
| 20 | 2026-11-05 | 50 |
+-------------+------------+-------+
2 rows in set (0.00 sec)집계값으로 정렬하세요: ORDER BY MAX(order_date) DESC. 고객별 최근 주문일 순으로 10(300), 20(50)이 나와요.
SELECT customer_id, SUM(amount) AS total
FROM orders GROUP BY customer_id
ORDER BY MAX(order_date) DESC;+-------------+-------+
| customer_id | total |
+-------------+-------+
| 10 | 300 |
| 20 | 50 |
+-------------+-------+
2 rows in set (0.00 sec)04조인한 코드 테이블에 PK·UNIQUE가 없음
부서 코드로 묶고 부서 이름을 같이 보여 주는 흔한 쿼리예요. d.dept_code로 GROUP BY 했는데도 d.dept_name이 GROUP BY에 없다고 1055가 났어요.
MySQL은 GROUP BY 컬럼이 PK나 NOT NULL UNIQUE일 때만 그 행의 다른 컬럼이 하나로 정해진다(functionally dependent)고 봐요. depts에는 키가 없어서, 같은 코드가 두 줄 있을 수도 있다고 판단한 거예요.
사전 준비
CREATE TABLE depts (dept_code VARCHAR(10), dept_name VARCHAR(30));
INSERT INTO depts VALUES ('D10','Sales'),('D20','IT');
CREATE TABLE emps (id INT PRIMARY KEY, dept_code VARCHAR(10), salary INT);
INSERT INTO emps VALUES (1,'D10',300),(2,'D10',400),(3,'D20',500);Query OK, 0 rows affected (0.02 sec)
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
Query OK, 0 rows affected (0.02 sec)
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0SELECT d.dept_code, d.dept_name, SUM(e.salary) AS total
FROM emps e JOIN depts d ON d.dept_code = e.dept_code
GROUP BY d.dept_code;ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'lab_a.d.dept_name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_bySELECT index_name, column_name, non_unique
FROM information_schema.statistics
WHERE table_schema = DATABASE() AND table_name = 'depts';
SELECT dept_code, COUNT(*) AS cnt FROM depts GROUP BY dept_code HAVING COUNT(*) > 1;Empty set (0.00 sec)
Empty set (0.00 sec)depts에 인덱스가 하나도 없고(첫 조회 Empty set), 실제로 중복된 코드도 없어요(두 번째 Empty set). 데이터는 문제없는데 키가 없어서 MySQL이 알 수 없는 상태예요.
ALTER TABLE depts ADD PRIMARY KEY (dept_code);Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0ALTER TABLE depts ADD PRIMARY KEY (dept_code);만 하고 쿼리는 그대로 다시 실행하니 성공했어요. 코드 테이블에 키를 주는 게 GROUP BY에 이름을 덧붙이는 것보다 근본적인 해결이에요.
SELECT d.dept_code, d.dept_name, SUM(e.salary) AS total
FROM emps e JOIN depts d ON d.dept_code = e.dept_code
GROUP BY d.dept_code;+-----------+-----------+-------+
| dept_code | dept_name | total |
+-----------+-----------+-------+
| D10 | Sales | 700 |
| D20 | IT | 500 |
+-----------+-----------+-------+
2 rows in set (0.00 sec)05GROUP BY 식과 SELECT 식이 다름 (DATE() vs DATE_FORMAT())
일별 매출을 GROUP BY DATE(created_at)로 묶고, SELECT에서는 DATE_FORMAT(created_at, '%Y-%m-%d')로 보여 줬어요. 둘 다 같은 날짜처럼 보이지만 MySQL은 다른 식으로 봐서, created_at이 GROUP BY에 없다고 1055를 냈어요.
사전 준비
CREATE TABLE orders (id INT PRIMARY KEY, created_at DATETIME, amount INT);
INSERT INTO orders VALUES (1,'2026-12-01 09:10:00',100),(2,'2026-12-01 15:30:00',200),(3,'2026-12-02 11:00:00',50);Query OK, 0 rows affected (0.01 sec)
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0SELECT DATE_FORMAT(created_at, '%Y-%m-%d') AS day, SUM(amount) AS total
FROM orders
GROUP BY DATE(created_at);ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'lab_a.orders.created_at' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_bySELECT DATE(created_at) AS grouped_by, DATE_FORMAT(created_at, '%Y-%m-%d') AS selected FROM orders;+------------+------------+
| grouped_by | selected |
+------------+------------+
| 2026-12-01 | 2026-12-01 |
| 2026-12-01 | 2026-12-01 |
| 2026-12-02 | 2026-12-02 |
+------------+------------+
3 rows in set (0.00 sec)두 식을 나란히 조회하면 값은 같아요. 하지만 MySQL은 결과값이 아니라 식이 같은지를 봐요. SELECT의 별칭으로 GROUP BY day 하니 12-01은 300, 12-02는 50으로 나왔어요.
SELECT DATE_FORMAT(created_at, '%Y-%m-%d') AS day, SUM(amount) AS total
FROM orders
GROUP BY day;+------------+-------+
| day | total |
+------------+-------+
| 2026-12-01 | 300 |
| 2026-12-02 | 50 |
+------------+-------+
2 rows in set (0.00 sec)자주 묻는 질문
this is incompatible with sql_mode=only_full_group_by 해결 방법은?
컬럼을 GROUP BY에 넣거나, MAX()·ANY_VALUE() 같은 집계로 감싸거나, 특정 행이 필요하면 윈도 함수로 바꾸세요.
ONLY_FULL_GROUP_BY가 켜져 있는지 확인하려면?
SELECT @@sql_mode;를 실행하세요. MySQL 8.4 기본값은 ONLY_FULL_GROUP_BY로 시작해요.
MySQL 8에서 그룹별 최신 1건 조회는?
서브쿼리에서 ROW_NUMBER() OVER (PARTITION BY 그룹컬럼 ORDER BY 날짜 DESC)를 매기고 rn = 1로 거르세요.
Expression #2 of SELECT list는 무슨 뜻인가요?
SELECT 목록에서 문제 컬럼의 순서(1부터)예요. ORDER BY 쪽 문제는 Expression #N of ORDER BY clause로 나와요.
PK로 GROUP BY 했는데도 1055가 나요. 조인한 테이블 컬럼이에요.
조인한 쪽 테이블의 GROUP BY 컬럼에 PK나 NOT NULL UNIQUE가 있는지 보세요. 위 실행에서는 코드 테이블에 키가 없어서 1055가 났고, PRIMARY KEY를 추가하니 같은 쿼리가 성공했어요.
검증 방법
시나리오마다 새 DB 사용자를 만들어 시작해요. 스크립트가 사전 준비 → 에러 재현(에러 코드 일치 확인) → 해결 → 원래 문장 재실행 순서로 실행하고, 화면의 출력은 DB가 실제로 낸 결과를 스크립트가 그대로 저장한 거예요.
lab_a, lab_b는 테스트용 일반 사용자(같은 이름의 데이터베이스가 기본, 그 안에서 모든 권한), root는 관리자예요. MySQL 8.4.13