MySQL 8.4Lab-verified · 2026-11-29

MySQL ERROR 1248 (42000): Every derived table must have its own alias

A subquery in FROM or JOIN (a derived table) has no alias.

MySQL 8.4.13
1248 (42000): Every derived table must have its own alias

Jump to a cause

  1. 01SELECT ... FROM (subquery) with no alias
  2. 02JOIN to a subquery with no alias
  3. 03UPDATE ... WHERE IN (SELECT FROM (subquery)) — the ERROR 1093 workaround

Lab-verifiedReproduced and fixed on MySQL 8.4.13 · 2026-11-29 · container image mysql:8.4

In MySQL, a subquery used as a table — in FROM or JOIN — is called a derived table, and it must have a name. Without AS <alias> after the closing parenthesis, the statement fails with ERROR 1248 before it runs.

All three runs below were fixed by adding an alias. Nothing else in the query had to change, apart from using the alias to qualify columns in the JOIN case.

Running the subquery on its own worked; only the alias was missing.

01SELECT ... FROM (subquery) with no alias

SELECT COUNT(*) FROM (SELECT customer_id FROM orders GROUP BY customer_id) failed with ERROR 1248.

Adding AS t after the parenthesis made it return 2 customers.

Setup
Setuprun as lab_a
CREATE TABLE orders (id INT PRIMARY KEY, customer_id INT, amount DECIMAL(10,2));
INSERT INTO orders VALUES (1, 1, 100.00), (2, 1, 50.00), (3, 2, 70.00);
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0
Reproducerun as lab_a
SELECT COUNT(*) FROM (SELECT customer_id FROM orders GROUP BY customer_id);
✕ ERROR
ERROR 1248 (42000): Every derived table must have its own alias
Checkrun as lab_a
SELECT customer_id FROM orders GROUP BY customer_id;
OUTPUT
+-------------+
| customer_id |
+-------------+
|           1 |
|           2 |
+-------------+
2 rows in set (0.00 sec)
After the fixrun as lab_a
SELECT COUNT(*) AS customers FROM (SELECT customer_id FROM orders GROUP BY customer_id) AS t;
✓ SUCCESS
+-----------+
| customers |
+-----------+
|         2 |
+-----------+
1 row in set (0.00 sec)

02JOIN to a subquery with no alias

Joining customers to (SELECT customer_id, SUM(amount) AS total FROM orders GROUP BY customer_id) without a name failed with ERROR 1248.

With AS s and the columns written as s.customer_id and s.total, the query returned Kim 150.00 and Lee 70.00.

Setup
Setuprun as lab_a
CREATE TABLE customers (id INT PRIMARY KEY, name VARCHAR(50));
CREATE TABLE orders (id INT PRIMARY KEY, customer_id INT, amount DECIMAL(10,2));
INSERT INTO customers VALUES (1, 'Kim'), (2, 'Lee');
INSERT INTO orders VALUES (1, 1, 100.00), (2, 1, 50.00), (3, 2, 70.00);
OUTPUT
Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0
Reproducerun as lab_a
SELECT c.name, total
FROM customers c
JOIN (SELECT customer_id, SUM(amount) AS total FROM orders GROUP BY customer_id) ON customer_id = c.id;
✕ ERROR
ERROR 1248 (42000): Every derived table must have its own alias
Checkrun as lab_a
SELECT customer_id, SUM(amount) AS total FROM orders GROUP BY customer_id;
OUTPUT
+-------------+--------+
| customer_id | total  |
+-------------+--------+
|           1 | 150.00 |
|           2 |  70.00 |
+-------------+--------+
2 rows in set (0.00 sec)
After the fixrun as lab_a
SELECT c.name, s.total
FROM customers c
JOIN (SELECT customer_id, SUM(amount) AS total FROM orders GROUP BY customer_id) AS s ON s.customer_id = c.id
ORDER BY c.name;
✓ SUCCESS
+------+--------+
| name | total  |
+------+--------+
| Kim  | 150.00 |
| Lee  |  70.00 |
+------+--------+
2 rows in set (0.00 sec)

03UPDATE ... WHERE IN (SELECT FROM (subquery)) — the ERROR 1093 workaround

To update a table based on itself, the inner query is wrapped in another subquery (the usual workaround for ERROR 1093). That wrapper is a derived table, and without an alias the UPDATE failed with ERROR 1248.

With ... HAVING SUM(amount) >= 100) AS big), the UPDATE changed 2 rows: customer 1's orders became vip, and customer 2 stayed new.

Setup
Setuprun as lab_a
CREATE TABLE orders (id INT PRIMARY KEY, customer_id INT, amount DECIMAL(10,2), status VARCHAR(10));
INSERT INTO orders VALUES (1, 1, 100.00, 'new'), (2, 1, 50.00, 'new'), (3, 2, 70.00, 'new');
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0
Reproducerun as lab_a
UPDATE orders SET status = 'vip'
WHERE customer_id IN (SELECT customer_id FROM (SELECT customer_id FROM orders GROUP BY customer_id HAVING SUM(amount) >= 100));
✕ ERROR
ERROR 1248 (42000): Every derived table must have its own alias
Checkrun as lab_a
SELECT customer_id FROM orders GROUP BY customer_id HAVING SUM(amount) >= 100;
OUTPUT
+-------------+
| customer_id |
+-------------+
|           1 |
+-------------+
1 row in set (0.00 sec)
After the fixrun as lab_a
UPDATE orders SET status = 'vip'
WHERE customer_id IN (SELECT customer_id FROM (SELECT customer_id FROM orders GROUP BY customer_id HAVING SUM(amount) >= 100) AS big);
SELECT id, customer_id, status FROM orders ORDER BY id;
✓ SUCCESS
Query OK, 2 rows affected (0.01 sec)
Rows matched: 2  Changed: 2  Warnings: 0

+----+-------------+--------+
| id | customer_id | status |
+----+-------------+--------+
|  1 |           1 | vip    |
|  2 |           1 | vip    |
|  3 |           2 | new    |
+----+-------------+--------+
3 rows in set (0.00 sec)

FAQ

How do I fix Every derived table must have its own alias?

Put an alias after the subquery's closing parenthesis, such as ) AS t. All three cases above were fixed that way.

What is a derived table in MySQL?

A subquery used in FROM or JOIN as if it were a table. Subqueries in WHERE do not need an alias.

Is the AS keyword required?

The runs above used AS t; the alias itself is what MySQL requires.

I wrapped a subquery to avoid ERROR 1093 and now get 1248

The wrapper (SELECT ... FROM (SELECT ...)) needs an alias on the inner subquery, as in the third case (AS big).

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, each with full rights on a database of the same name (its default database); root is the administrator. MySQL 8.4.13