If you’re getting an error that reads something like “The correlation name ‘a’ is specified multiple times in a FROM clause’” in SQL Server, it could be that you’re doing a join on two tables, but you’re assigning them the same aliases.
Each table’s alias must be unique.
To fix this issue, be sure to use unique table aliases in your query.
Example of Error
Here’s an example of code that produces the error:
SELECT
a.AuthorName,
b.Title
FROM Authors a
JOIN Books a
ON a.AuthorId = b.AuthorId
ORDER BY a.AuthorName;
Output:
Msg 1011, Level 16, State 1, Line 1
The correlation name 'a' is specified multiple times in a FROM clause.
In this case I accidentally provided the same alias for both tables. I assigned both tables an alias of a
, which caused the error.
Solution
To fix this issue, we must provide unique table aliases:
SELECT
a.AuthorName,
b.Title
FROM Authors a
JOIN Books b
ON a.AuthorId = b.AuthorId
ORDER BY a.AuthorName;
Output:
AuthorName Title
----------- ---------------
Blake Wiley Amazing Stories
Sesh Smith The Great Novel
Sesh Smith Sesh's Memoir
This time the query ran fine without error. I changed the Books
table’s alias to b
, which resulted in each table having its own unique alias.