Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everybody,

I have two unrelated tables without any primary key or relationship as following:
SQL
Employee Table: ID, Dispaly Name, Name, Department
Participants Table: User Name, Name


This is a bad database design but because I imported both of them from Excel sheet to SQL Server Management Studio. I know there are many matches between [Display Name] and [User Name] columns in both of them, so I want to show the department column for the matches. How to do that?

I came up with this query, but it did not show me any results and I don't know why:
SQL
SELECT dbo.[Participants].[User Name], dbo.Employee.Department
    FROM 
    Employee, ['Participants']
     WHERE   Employee.[Display name] = ['Participants'].[User Name]
Posted

Try this:

SQL
SELECT P.[User Name], E.Department
FROM [Employee] AS E
INNER JOIN Participants AS P
ON E.[Display name] = P.[User Name]
 
Share this answer
 
['Participants'] is not the correct way to write the table name.
Instead write like [Participants] or only Participants.

So, the query will look like...
SQL
SELECT
    dbo.[Participants].[User Name],
    dbo.Employee.Department
FROM
    Employee, [Participants]
WHERE
    Employee.[Display name] = [Participants].[User Name]

Or if you will use INNER JOIN, then query will be like...
SQL
SELECT 
    Part.[User Name], 
    emp.Department
FROM 
    Employee AS emp 
INNER JOIN 
    [Participants] AS Part
ON   
    emp.[Display name] = Part.[User Name]


Thanks...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900