Search code examples
sqlsql-serverinner-join

SQL Server inner join returns the same row repeatedly in all the column


I am writing this query to do an inner join:

select firstname 
from employees  
inner join orders on Employees.employeeid = orders.employeeid

The result is the same row in all the columns.

What is wrong?


Solution

  • Presumably, you have more orders than you have employees. Hence, one employee is on many orders.

    When you run:

    select e.firstname 
    from employees e inner join
         orders o
         on e.employeeid = o.employeeid;
    

    Then you are getting a list of all first names -- so one employee name is going to be repeated, once per each order. If you want just a list of the distinct values, then you can use select distinct instead.