Question

How to do an inner join

  • 21 August 2023
  • 15 replies
  • 139 views

Badge +2

Hello, I would like to join two tables with the id as a key using the inner join, but when I try to do so, it doesn’t return any results even though it should return something. I woule like to know what is the correct syntaxis for this, I am using

 

SELECT * FROM "Table1" INNER JOIN "Table2" ON 'Table1.Table2_id' = 'Table2.id';

Thank you!


15 replies

Badge +4

Try this without enclosing the table.


SELECT *
FROM Table1
INNER JOIN Table2 ON Table1.Table2_id = Table2.id;

 

What type of database are you using? Is it SQL Studio?


 

Badge +2

Hi thanks for your answer.

I am using PostgreSQL as database and the query doesn’t work if I don’t use the quotation marks, it doesn’t find the column names, and when I use it in quotes it retrieves the empty data table when it should have information inside of it.

Badge +4

Try this 
SELECT *
FROM "Table1"
INNER JOIN "Table2" ON "Table1"."Table2_id" = "Table2"."id";
 

 

Badge +4

Did it work?

Badge +2

Nope, it says that  "Table1"."Table2_id" does not exist, but the tables aren’t empty 

Badge +4

Can you message me your actual query?

Badge +2

SELECT *

FROM "People"

INNER JOIN "MasterPeople" ON “People”.”MasterPeople”_”idPerson” = “MasterPeople”.”idPerson”;

Badge +4

SELECT *
FROM "People"
INNER JOIN "MasterPeople" ON "People"."MasterPeople_idPerson" = "MasterPeople"."idPerson";
 

Badge +4

Try that

 

Badge +2

It is still the same :( The error says: “ERROR: column People.MasterPeople_idPerson does not exist”

Badge +4

Postgres is case-sensitive. Is the field names and table names identical?
SELECT *
FROM "People"
INNER JOIN "MasterPeople" ON "People"."MasterPeople_idPerson" = "MasterPeople"."idPerson";

Badge +2

Yes, the names are identical. As an example when I use just the select * and the name of the tables it works perfectly, the issue is with this inner join 

Badge +4

what about the field names?

Badge +2

Yes, everything is identical. I made sure of it several times

Badge +4

Can you give me a Select query with the fields of the table individually with *?

Bit challenging to know the issue without seeing the tables.

Reply