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

A subquery in FROM wasn’t given an alias.

Seen on: SQL

Meaning

MySQL (and PostgreSQL before 16) require an alias for every derived table: FROM (SELECT …) AS t.

Common causes

  • Subquery in FROM without AS alias
  • Query ported from PostgreSQL 16+/SQL Server that allows omitting it

⚡ Quick fix

  1. Add an alias after the closing parenthesis
  2. Alias every derived table even when not referenced

Detailed fix by platform

SQL

  1. SELECT t.user_id, t.cnt FROM (SELECT user_id, COUNT(*) cnt FROM orders GROUP BY user_id) AS t;

How to diagnose

  1. Subquery — Alias present?
  2. Recent change — Did it start after a deploy, upgrade, or configuration change? Compare with the last working version.

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.