SQL Query Help - Multiple JOINS - sql

having trouble with this SQL query. Here's the situation:
I have three tables structured as follows:
Events -> fields ID and Name,
Players -> fields ID and Name,
Matches -> fields ID, EventID, Player1ID, Player2ID
I would like to perform a query that shows me the Matches Table, but replaces the EventID with the Event.Name, the Player1ID with the Players.Name and the Player2ID with the Players.Name.
It is easy for me to get one Player and the Event using this:
SELECT
Players.Name as Player1, Events.Name
FROM
(Matches
INNER JOIN
Events ON (Matches.EventID=Events.ID))
INNER JOIN
Players ON (Matches.Player1ID = Player.ID) ;
But, how do I get the Player2's name?

Add a second JOIN to the Players table:
SELECT
Players.Name as Player1, Events.Name,
p2.Name as Player2
FROM
Matches
INNER JOIN
Events ON Matches.EventID = Events.ID
INNER JOIN
Players ON Matches.Player1ID = Player.ID
INNER JOIN
Players p2 ON Matches.Player2ID = p2.ID;

You can do this by joining the tables together. The trick is that you have to include the Players table twice. This is a case where you need table aliases to distinguish between these two references to the table in the from clause:
select m.matchid, e.name as event_name, p1.name as player1_name, p2.name as player2_name
from matches m join
events e
on m.eventid = e.id join
players p1
on m.player1 = p1.id join
players p2
on m.player2 = p2.id;
I also added table aliases for the other tables, which makes the query easier to read.

SELECT p1.Name, p2.Name, Events.Name
FROM Matches
INNER JOIN Events ON (Matches.EventID=Events.ID))
INNER JOIN Players p1 ON (Matches.Player1ID = p1.ID)
INNER JOIN Players p2 ON (Matches.Player2ID = p2.ID)

You need to join the query again with the Players table on the Player2ID field. So change your query to:
SELECT one.Name as Player1, two.Name as Player2, Events.Name
FROM
Matches INNER JOIN
Events ON Matches.EventID=Events.ID INNER JOIN
Players one ON Matches.Player1ID = one.ID INNER JOIN
Players two ON Matches.Player1ID = two.ID

Related

How to combine Inner Join and Left Join in SQL Query statement?

Given this Diagram:
What is the right way to use join that will includes all necessary fields from different tables (person name, employee number, status of contracts, job name, department name, pcn/item name, country, and religion)? BUT I want also to include all persons that choose not to put either thier country or religion. I have this query:
SELECT persons.fullname, religions.religion_name, countries.country_name
FROM countries INNER JOIN (religions INNER JOIN (persons INNER JOIN
((jobs INNER JOIN (departments INNER JOIN pcns ON departments.ID = pcns.department_id) ON jobs.ID = pcns.job_id)
INNER JOIN (employees INNER JOIN contracts ON employees.ID = contracts.employee_id) ON pcns.ID = contracts.pcn_id)
ON persons.ID = employees.person_id) ON religions.ID = persons.religion) ON countries.ID = persons.country_id;
I used LEFT JOIN but I got an error. Right now, I only query all my tables except for countries and religions then I made a function that whenever a user clicks particular person this function will return its religion and/or country. I am just curious how to achieve it in a SELECT query.

need help writing a sql query with multiple join conditions

i have a sql query currently with a multiple inner join
select game_id, start_time_utc, t.name from games g
inner join teams t
on g.home_team_id = t.team_id
or g.away_team_id = t.team_id
the problem is i want t.name for both the home_team_id and the away_team_id however it is just giving the first instance with his home_team_id
how do i modify the sql query so i get back both the home team name and the away team name
You need to join the table twice.
select game_id, start_time_utc, t.name as home_team_name, t2.name as away_team_name
from games g
inner join teams t on g.home_team_id = t.team_id
inner join teams t2 on g.away_team_id = t2.team_id
You could select each team name using a correlated subquery:
select g.game_id, g.start_time_utc,
(select name from teams t where t.team_id=g.home_team_id) HomeTeam,
(select name from teams t where t.team_id=g.away_team_id) AwayTeam
from games g;

Joining tables and finding values that do not exist

I am having some issues with joining tables to get null values, and I can't find what I am doing wrong.
The case: I am trying to make a cinema system, where I have made entities that match the cinema.
I have a Hall, Row and Seat table, and a Show table that holds the value for movies and what hall it will be played in. To bond everything together, I have made a Reservation table that is keeping track of what seats to that specific show is taken.
My entities look like this:
My problem: I am trying to fetch all free seats for the show, I can get all seats for the show, but when I try to add the Reservation to get the free ones I get no records.
My query that is able to fetch all seats:
SELECT show.id AS ShowID,
seat.id AS SeatID,
seat.rowid AS RowID,
show.hallid AS HallId,
reservation.seatid AS Expr1
FROM show
INNER JOIN hall
ON show.hallid = hall.id
FULL OUTER JOIN seat
ON hall.id = seat.hallid
LEFT OUTER JOIN reservation
ON reservation.showid = show.id
WHERE ( show.id = 1 )
AND ( reservation.seatid IS NULL )
ORDER BY reservation.showid,
rowid
You need INNER joins between Show, Hall, Row and Seat and a LEFT join to Reservation, so you can filter out the matched rows:
SELECT s.Id AS ShowID, t.Id AS SeatID, t.RowId AS RowID, s.HallId
FROM Show s
INNER JOIN Hall h ON h.Id = s.HallId
INNER JOIN Seat t ON t.HallId = h.Id
INNER JOIN Row w ON w.HallId = h.Id AND w.Id = t.RowId
LEFT JOIN Reservation r ON r.ShowId = s.Id AND r.HallId = h.Id AND r.SeatId = t.Id AND r.RowId = w.Id
WHERE (s.Id = 1) AND (r.SeatId IS NULL)
Replace:
INNER JOIN Hall ON Show.Id = Hall.Id FULL OUTER JOIN
With:
INNER JOIN Hall ON Show.HallId = Hall.Id FULL OUTER JOIN
While it might not be the full answer to your question, i think this might cause issues for you too.

How to select SQL results based on multiple tables

I need to select results from one table based on certain matching values in a couple of other tables. I have the following tables:
person: id, firstname, lastname
team: id, teamname
player: id, person_id(FK), team_id(FK)
coach: id, person_id(FK), team_id(FK)
I need to return all the coaches and players names for each team. I've only ever used inner joins, and it doesn't seem like I can use those here, so any idea how to do this?
This will give you the coach:
SELECT team.Teamname, person.Firstname, person.Lastname
FROM person
JOIN coach ON person.id = coach.person_id
JOIN team ON coach.team_id = team.id
And this will give you the players:
SELECT team.Teamname, person.Firstname, person.Lastname
FROM person
JOIN player ON person.id = player.person_id
JOIN team ON player.team_id = team.id
So, the non-elegant, simple answer is to just toss it all together with UNION.
Just use an OR in the join to Team
SELECT
P.firstname,
P.lastname,
T.teamname
FROM
person p id
LEFT JOIN player pl
ON p.id = pl.person_id
LEFT JOIN coach c
ON p.id = c.person_id
LEFT JOIN team t
ON pl.team_id = t.id
or.c.team_id = t.id
Or if you perfer if and your database has COALESCE
LEFT JOIN team t
ON COALESCE(pl.team_id,c.team_id) = t.id

SQL query on table that has 2 columns with a foreign id on the same table

I have a table let's say in the form of: match(id, hometeam_id, awayteam_id) and team(id, name). How do I build my SQL query in order to get a result table in the form of (match_id, hometeam_name, awayteam_name), since they both (hometeam_id, awayteam_id) reference the same table (team)?
Thank you
You would just join to the team table multiple times:
SELECT m.id, away.name, home.name
FROM match m
INNER JOIN team away ON away.id = m.awayteam_id
INNER JOIN team home ON home.id = m.hometeam_id
You join to the team table twice.
select matchdate, t1.teamname, t2,teamname from
match m
join team t1 on m.hometeamId = t1.teamid
join team t2 on m.awayteamid = t2.teamid
Join to the team table twice, once for the Home Team, and again for the Away Team, using aliases after the table names in the query:
select m.match_id, homeTeam.name as HomeTeamName, awayTeam.name as AwayTeamName
from
team homeTeam join
match m on m.hometeam_id = homeTeam.hometeam_id join
team awayTeam on awayTeam.hometeam_id = m.awayteam_id
select m.id, h.name as hometeam_name, a.name as awayteam_name
from match m left join team h on m.hometeam_id = h.id
left join team a on m.awayteam_id = a.id