Join 3 tables in postgresql on different objects

Question:

I have a dataframe with 3 tables, lets call them People, Event, Outcome. The setup for these tables would look like this:

Name has: ID, Name, Age; Outcome has: ID, EventID, EventTime and OutcomeID; Event has: EventID, EventState, EventDate, EventTemp.

I need to run a query that pulls in all the Events that "Sally" competed in and output the EventName, Event Month (extracted from the EventDate), EventTemp, and EventTime. But this issue I’m running into is I need to join Event and Outcome on the EventID and then People and Outcome on the ID.

Here is what I last tried (which isn’t working):

SELECT eventname, eventstate, EXTRACT(MONTH FROM eventdate), eventtemp
FROM event E JOIN outcome O ON E.eventid = O.eventid 
FROM name N JOIN outcome O ON N.id = O.id
WHERE name = "Sally";

This is not outputting anything because it throws an error. I am new to postgresql. Can someone help?

Asked By: data_life

||

Answers:

There can only be one FROM clause, although it can contain multiple JOINs. I’m assuming that the "name" field is inside the "name" table:

SELECT E.eventname, E.eventstate, EXTRACT(MONTH FROM E.eventdate), E.eventtemp
FROM event E JOIN outcome O ON E.eventid = O.eventid 
             JOIN name N ON N.id = O.id
WHERE N.name = 'Sally';
Answered By: Tim Roberts
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.