Skip to content
Advertisement

How to define INNER JOIN in SQL

I have a problem how to define INNER JOIN in SQL. The problem is that I have a table Names with columns ID and Name. The other table has column PersonID.

Sometimes value in column PersonID is 0, and in Names table there is no row with a column value of 0.

I have created a view, and I am using an INNER JOIN. Problem is that SELECT statement in the view only works when there is value greater than zero in column PersonID.

How can I define an INNER JOIN when value is 0, so that column is NULL, and when is not zero then value should be Name from Names?

What I mean is:

Thank you in advance for any help.

Advertisement

Answer

It sounds like you want LEFT JOIN:

Note that this also changes the table aliases to meaningful abbreviations for the tables. And uses COALESCE() instead of CASE.

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