Skip to content
Advertisement

What happens if one query contains duplicate joins?

I have an application filter which can generate duplicate SQL queries to the result SQL like:

select * from articles 
inner join users on articles.users_id = users.id 
inner join users on articles.users_id = users.id 
where users.name like %xxx%

The question is if a database is able to handle these duplicates or not. What happened in the database if this query comes inside? If I should remove it from the result SQL or if I can leave it as is.

Advertisement

Answer

This is a self join. A self join is a regular join, but the table is joined with itself.

Example

SELECT
    A.Id,
    A.FullName,
    A.ManagerId,
    B.FullName as ManagerName
FROM Employees A
JOIN Employees B
ON A.ManagerId = B.Id

A and B are different table aliases for the same table.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement