PostgreSQL Error 42712: duplicate alias
PostgreSQL error code 42712 (duplicate_alias) occurs when a SQL query contains two or more tables, subqueries, or CTEs assigned the same alias within the same query scope. The PostgreSQL parser catches this at parse time — before execution — making it straightforward to identify and fix. This error is especially common in complex multi-join queries and large CTE chains.
Top 3 Causes
1. Duplicate Table Aliases in FROM / JOIN Clauses
The most frequent cause is accidentally assigning the same alias to two different tables or subqueries in a JOIN.
-- ERROR: Both tables assigned alias 'o'
SELECT o.id, o.product_name
FROM orders o
JOIN order_items o ON orders.id = o.order_id;
-- ERROR: table name "o" specified more than once
Fix: Use distinct, meaningful aliases for each table.
-- FIXED: Unique aliases for each table
SELECT o.id, oi.product_name
FROM orders o
JOIN order_items oi ON o.id = oi.order_id;
2. Duplicate CTE Names in WITH Clauses
Defining two CTEs with the same name in a single WITH block triggers this error immediately.
-- ERROR: CTE name 'sales_data' used twice
WITH sales_data AS (
SELECT region, SUM(amount) AS total FROM sales WHERE year = 2023 GROUP BY region
),
sales_data AS (
SELECT region, SUM(amount) AS total FROM sales WHERE year = 2024 GROUP BY region
)
SELECT * FROM sales_data;
-- ERROR: WITH query name "sales_data" specified more than once
Fix: Give each CTE a unique, descriptive name.
-- FIXED: Unique CTE names
WITH sales_2023 AS (
SELECT region, SUM(amount) AS total FROM sales WHERE year = 2023 GROUP BY region
),
sales_2024 AS (
SELECT region, SUM(amount) AS total FROM sales WHERE year = 2024 GROUP BY region
)
SELECT
s23.region,
s23.total AS total_2023,
s24.total AS total_2024
FROM sales_2023 s23
JOIN sales_2024 s24 ON s23.region = s24.region;
3. Conflicting Aliases Between Inline Subqueries and Tables
When an inline subquery (derived table) is given the same alias as another table already referenced in the same FROM clause, 42712 is raised.
-- ERROR: Both 'users' table and subquery use alias 'u'
SELECT u.name, u.order_count
FROM users u
JOIN (
SELECT user_id, COUNT(*) AS order_count
FROM orders
GROUP BY user_id
) u ON u.id = u.user_id;
-- ERROR: table name "u" specified more than once
Fix: Assign a descriptive, unique alias to the subquery.
-- FIXED: Subquery gets a clear, unique alias
SELECT u.name, ord_summary.order_count
FROM users u
JOIN (
SELECT user_id, COUNT(*) AS order_count
FROM orders
GROUP BY user_id
) ord_summary ON u.id = ord_summary.user_id;
Quick Fix Checklist
- Scan every alias in your
FROM,JOIN, andWITHclauses and ensure no two share the same name within the same query scope. - Replace single-letter or generic aliases (
t,a,x) with table-based abbreviations (usr,ord,pay) to make duplicates visually obvious. - When refactoring large queries, use
EXPLAINto parse the query — it will surface 42712 instantly without running the full execution.
-- Use EXPLAIN to catch alias errors without executing the query
EXPLAIN
SELECT u.name, ord_summary.order_count
FROM users u
JOIN (
SELECT user_id, COUNT(*) AS order_count FROM orders GROUP BY user_id
) ord_summary ON u.id = ord_summary.user_id;
Prevention Tips
Adopt a team-wide alias naming convention. Define standard abbreviations for your most-used tables (
users → usr,orders → ord,payments → pay) and enforce them in code reviews. This makes duplicate aliases immediately visible.Break complex queries into validated CTE steps. Build your query incrementally — write and test each CTE independently before combining them. Use descriptive CTE names that reflect their purpose (
active_users,monthly_revenue) rather than generic ones, which naturally reduces the chance of name collisions.
Related Errors
-
42701
duplicate_column— Similar naming conflict, but for column names inSELECTlists or table definitions. -
42P01
undefined_table— Can appear right after fixing 42712 if a renamed alias is not updated everywhere in the query. -
42703
undefined_column— Often follows alias renaming when column references still use the old alias name.
📖 Want a more detailed guide?
Check out the full in-depth version (Korean) on oraerror.com — includes detailed analysis, additional SQL examples, and prevention tips.
Top comments (0)