Skip to main content
Question

How to do an inner join

  • August 20, 2023
  • 15 replies
  • 284 views

Forum|alt.badge.img+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

Forum|alt.badge.img+4
  • Navigator | Tier 3
  • August 21, 2023

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?


 


Forum|alt.badge.img+2
  • Author
  • Cadet | Tier 2
  • August 21, 2023

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.


Forum|alt.badge.img+4
  • Navigator | Tier 3
  • August 21, 2023

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

 


Forum|alt.badge.img+4
  • Navigator | Tier 3
  • August 21, 2023

Did it work?


Forum|alt.badge.img+2
  • Author
  • Cadet | Tier 2
  • August 21, 2023

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


Forum|alt.badge.img+4
  • Navigator | Tier 3
  • August 21, 2023

Can you message me your actual query?


Forum|alt.badge.img+2
  • Author
  • Cadet | Tier 2
  • August 21, 2023

SELECT *

FROM "People"

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


Forum|alt.badge.img+4
  • Navigator | Tier 3
  • August 21, 2023

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


Forum|alt.badge.img+4
  • Navigator | Tier 3
  • August 21, 2023

Try that

 


Forum|alt.badge.img+2
  • Author
  • Cadet | Tier 2
  • August 21, 2023

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


Forum|alt.badge.img+4
  • Navigator | Tier 3
  • August 21, 2023

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


Forum|alt.badge.img+2
  • Author
  • Cadet | Tier 2
  • August 21, 2023

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 


Forum|alt.badge.img+4
  • Navigator | Tier 3
  • August 21, 2023

what about the field names?


Forum|alt.badge.img+2
  • Author
  • Cadet | Tier 2
  • August 21, 2023

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


Forum|alt.badge.img+4
  • Navigator | Tier 3
  • August 21, 2023

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.