New to SQL, having trouble with table with two IDs - sql

I have a DB of scores over the course of a season and I am having trouble with a query to return the season results for a given team. Here is a snapshot of the table involved:
Teams (tid, team_name)
Games (home_team_id, road_team_id, game_date, home_score, road_score)
What would the SQL look like to return all games where home_team_id or road_team_id is 1 and include the team_name of the opponent?

Not clearly stated in question, but I guess your main confusion is on how to get team_name from both home_team_id and road_team_id. You can do twice INNER JOIN for that purpose, for example :
select
g.*
, home.team_name as home_team_name
, road.team_name as road_team_name
from Games g
inner join Teams home on home.tid = g.home_team_id
inner join Teams road on road.tid = g.road_team_id
where g.home_team_id = 1
or g.road_team_id = 1

Related

How to count the difference between two values in SQL?

Sorry if my title is not detailed
I have two tables
Game table:
homeTeam int
awayTeam int
homePoints int
awayPoints int
Team
tid int
name varchar(20)
I am trying to find the number of games won at home by a specific team, lets say 'cops', with Team.tid = Game.homeTeam and wins are counted if homePoints > awayPoints
I want to end up with
Team HomeWins
-----------------
Cops 20
How do I go about that?
EDIT: #
I Managed to get my answer using
SELECT t.name, count(CASE WHEN homePoints > awayPoints then 1 ELSE NULL END) as "Home Wins"
from Team t
JOIN Game g
ON t.tid = g.homeTeam
where t.name = 'Patriots'
GROUP BY t.name
some of the other answers were giving me the following errors
Column 'team.name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
You should join the tables to be able to get both name and homePoints and use COUNT by homePoints and group by using team id to see a result for each team.
SELECT
T.name, COUNT(G.homePoints)
FROM
team T
INNER JOIN
game G ON G.homeTeam = T.tid
WHERE
G.homePoints > G.awayPoints
GROUP BY T.tid, T.name;
OR if you need the result for a specific team by providing it's id, you may drop the GROUP BY and add condition in the WHERE, e.g.
SELECT
T.name, COUNT(T.tid)
FROM
team T
INNER JOIN
game G ON G.homeTeam = T.tid
WHERE
G.homePoints > G.awayPoints and T.tid = :request_team_id;
First SELECT the columns that we want to display and use AS to specify they headings.
SELECT
team.name as 'Team',
COUNT(game.homePoints) AS 'HomeWins'
FROM dbo.team
Then we use INNER JOIN to only include the entries in game table, which match the Team ID in the homeTeam column vs what we selected from the team table.
INNER JOIN game on team.tid = game.homeTeam
Then we add a WHERE clause to limit it to only the team we ask for team.name = 'cops' and only include wins by that team
WHERE
team.name = 'cops'
AND game.homePoints > game.awayPoints
So all together your script should look like this;
SELECT
team.name as 'Team',
COUNT(game.homePoints) AS 'HomeWins'
FROM dbo.team
INNER JOIN game on team.tid = game.homeTeam
WHERE
team.name = 'cops'
AND game.homePoints > game.awayPoints
GROUP BY team.name

SQL UNION syntax

I am trying to figure out what the correct syntax for UNION is. My schema looks like is the following:
Players (playerNum, playerName, team, position, birthYear)
Teams = (teamID, teamName, home, leagueName)
Games = (gameID, homeTeamNum, guestTeamNum, date)
I need to print all teamIDs where the team played against the X team but not against the Y team.
So my first idea was to check for the hometeamNum and then do a check for the guesteamNum, but I am not sure home to do the proper syntax.
SELECT DISTINCT hometeamNum
FROM games
WHERE
guestteamNum IN
(SELECT teamid FROM teams WHERE teamname = 'X') AND
guestteamNum NOT IN
(SELECT teamid FROM teams WHERE teamname = 'Y')
UNION DISTINCT
If you just need the home teams, this should suffice:
SELECT DISTINCT hometeamnum
FROM games
WHERE guestteamnum NOT IN (SELECT teamid FROM teams WHERE teamname = 'Y')
If you need both home teams and guest teams:
Select all teams that are not 'y' that didn't play agains 'y' as home team and didn't play against 'y' as guest team, and played against 'x' as guest team or played against 'x' as home team.
SELECT DISTINCT teamid
FROM teams
WHERE teamname != 'y' AND teamid NOT IN
(SELECT hometeamnum
FROM games INNER JOIN teams ON games.guestteamnum = teams.teamid
WHERE teamname = 'y'
UNION
SELECT guestteamnum
FROM games INNER JOIN teams ON games.hometeamnum = teams.teamid
WHERE teamname = 'y')
AND teamid IN
(SELECT guestteamnum
FROM games INNER JOIN teams on games.hometeamnum = teams.teamid
WHERE teamname = 'x'
UNION
SELECT hometeamnum
FROM games INNER JOIN teams on games.guestteamnum = teams.teamid
WHERE teamname = 'x');
Hopefully this is what you were after. There may be a more concise query out there but it's too late in the night for me to think of one :)
SELECT City, Country FROM Customers
WHERE Country='Germany'
UNION ALL
SELECT City, Country FROM Suppliers
WHERE Country='Germany'
ORDER BY City;
Using NOT EXISTS allows you to locate rows that don't exist. That is , you want teams that have played against 'X' which are rows that do exist and these can be located by using a simple join and where clause**. Then from those rows you need to find any that do not exist against the team 'Y'.
SELECT DISTINCT
hometeamnum
FROM games
INNER JOIN teams AS guests ON games.guestTeamNum = guests.teamID
WHERE guests.teamname = 'X'
AND NOT EXISTS (
SELECT 1
FROM games AS games2
INNER JOIN teams AS guests2 ON games2.guestTeamNum = guests2.teamID
WHERE games.hometeamnum = games2.hometeamnum
AND guests2.teamname = 'Y'
)
Notes.
EXISTS/NOT EXISTS does not actually need to return any data so it is possible to use select 1 or select null or select *. I have used select 1 here simply because it may be easier to understand - however I would personally prefer `select null' which stresses that no data is being returned by the exists subquery.
EXISTS/NOT EXISTS are both reasonably efficient and can perform better than IN (...)
** for performance, and where it does not alter the result, use a join in preference to IN ( subquery )

SQL MS Access join help average not correct

I am trying to create a query that displays the average attendance by conference when at least one team was in a game.
Relationships
this is very close to what im looking for
SELECT
Conference.ConferenceName,
AVG(Game.Attendance) AS AVG_ATT
FROM
(
Conference
INNER JOIN School ON Conference.[ConferenceID] = School.[ConferenceID]
)
INNER JOIN Game ON
(
School.[SchoolID] = Game.[Team1]
OR
School.[SchoolID] = Game.[Team2]
)
GROUP BY
Conference.ConferenceName;
the problem is if a game has 2 teams from the same conference it adds the attendance twice, and should only do it once.
consider 2 games
game1
Team1- Wisconsin
Conference - BIG10
Team2 - Michigan
Conference - BIG10
Attendance - 100,000
game2
Team1- Wisconsin
Conference - BIG10
Team2 - USC
Conference - PAC12
Attendance - 65,000
Results
BIG10-correct 82,500
PAC12 65,000
BIG10-Actual 88,333
Get a distinct list of the games by conference in a derived query, then do your average.
SELECT
ConferenceName,
AVG(Attendance) AS AVG_ATT
FROM
(
SELECT DISTINCT
GameID,
Conference.ConferenceName,
Game.Attendance
FROM
(
Conference
INNER JOIN School ON Conference.[ConferenceID] = School.[ConferenceID]
)
INNER JOIN Game ON
(
School.[SchoolID] = Game.[Team1]
OR
School.[SchoolID] = Game.[Team2]
)
) DerivedDistinctGamesAndConferences
GROUP BY
ConferenceName;
USE UNION to discard duplicated first before doing the average
SELECT
Game.GameID
Game.Attendance
Conference.ConferenceName
FROM
(
Conference
INNER JOIN School ON Conference.[ConferenceID] = School.[ConferenceID]
)
INNER JOIN Game ON
(
School.[SchoolID] = Game.[Team1] -- TEAM 1
)
UNION
SELECT
Game.GameID
Game.Attendance
Conference.ConferenceName
FROM
(
Conference
INNER JOIN School ON Conference.[ConferenceID] = School.[ConferenceID]
)
INNER JOIN Game ON
(
School.[SchoolID] = Game.[Team2] -- TEAM 2
)
So you will get query1 union query2
GameID Attendance Conference
1 100,000 BIG10 < one row will disapear
2 80,000 BIG10
1 100,000 BIG10 < after union
2 80,000 PAC12
Then you calculate the avergage over this result
I am not exactly sure in terms of coding, but conceptually, the fact that you need to aggregate totals "when at least one" team was- as you then say, only ONCE- at a conference, makes me intuit you may need to use some "if...then" clause statements within VBA script to get the specific calculation you want. Right now, the SQL "or" statement isn't enough.

three tables relations SQL select

I'm learning SQL, so help is greatly appreciated.
I have three tables: league, player and team
league and player is many to many (as a player can be in more than one league)
team and player is many to many (as a player can be on a team in multiple leagues)
league and team is one to many.
I have the following table ids:
league.id ----. league_has_player (league_id, player_id) .------ player.id
team.id ----. team_has_player (team_id, player_id) .----- player.id
league ----. team.id (team.league_id)
I want to get all the players in the league who are not on a team in the league.
Here's my broken attempt.
Select *
from player p,
join team t on t.league_id = l.id
join league l on league.id = 2
where p.id = league_has_player.player_id and
not in (select team_has_player.player_id from team_has_player)
I think you're trying more for something like this. As a couple comments have pointed out, your table schema isn't clear. But this is the impression I get from your attempt.
DECALRE #leagueId...
SELECT *
FROM player
WHERE player.id IN (SELECT player_id
FROM league_has_player
WHERE league_id = #leagueId) AND
player.id NOT IN (SELECT player_id
FROM team_has_player
INNER JOIN team ON team.Id = team_has_player.team_id
WHERE team.league_id = #leagueId)
Get all the players registered for the league, where they don't belong to a team in the league.
SELECT p.*, lp.league_id
FROM player p
JOIN league_has_player lp ON lp.player_id = p.id
WHERE NOT EXISTS (select 1
FROM team_has_player tp
JOIN team t ON t.id = tp.team_id
AND tp.player_id = p.player_id
AND t.league_id = lp.league_id)

Trouble With GROUP BY Learning Derby SQL

I have formed this query to produce the pitcher from each team who has the most wins. My trouble comes in that I need to group them a certain way and I keep having scoping issues when trying to do so. W is the number of wins for each pitcher. Here is my pre-grouped statement...
SELECT
(SELECT p1.nameFirst FROM Players p1 Where (one.playerID = p1.playerID)),
(SELECT p1.nameLast FROM Players p1 Where (one.playerID = p1.playerID)),
one.W, (SELECT t1.name FROM Teams t1 Where(one.teamID = t1.teamID))
FROM Pitching one
Where (one.W >= ALL
(SELECT two.W
FROM Pitching two
Where (two.teamID = one.teamID)));
I need to group the tuples by league and within the leagues group by division. League (lgID) and division (divID) exist in the Teams table. Can someone point me in the right direction? Thank you.
This is top six rows of what is currently output...
Zach Britton 11 Baltimore Orioles
Mark Buehrle 13 Chicago White Sox
Madison Bumgarner 13 San Francisco Giants
Jhoulys Chacin 11 Colorado Rockies
Bruce Chen 12 Kansas City Royals
Kevin Correia 12 Pittsburgh Pirates
My desired output is to have these teams sorted by league (NL/AL) and within the leagues have them sorted by division.
Based on your updated comment. I think this is what you want.
SELECT
pl.nameFirst
, pl.nameLast
, p.W
, t.name
FROM
(
SELECT
MAX(p1.W) AS W
, p1.teamId
FROM
Pitching p1
GROUP BY
p1.teamId
) t1
JOIN
Pitching p
ON t1.W = p.W
AND t1.teamId = p.teamId
JOIN
Players pl
ON p.playerID = pl.playerID
JOIN
Teams t
ON p.teamID = t.teamID
ORDER BY
t.lgID
, t.divID
I do agree with swasheck, there are some opportunities for improvement in your schema. As swascheck said, teamId should be in Players. Not in Pitching.
This may be an issue of structure. Using what I believe to be your structure we could probably narrow it down like this:
select Players.nameFirst, Players.nameLast, TopPitcher.Winner,
Teams.name, League.Name, Division.Name
from (select playerID, max(Wins) as Winner
from (select playerID, teamID, count(W) as Wins
from Pitching
group by playerID, teamID ) PitchingWins) TopPitcher
join Players
on TopPitcher.playerID = Players.playerID
join Teams
on Teams.teamID = Players.teamID
join League
on Teams.leagueID = League.leagueID
join Division
on League.divisionID = Division.divisionID
order by League.Name, Division.Name
Now. Having said that, this is only for the structure you've given (with some other interpolation). Your overall structure is faulty as I would probably relate Player to Teams and not Pitching to Teams since you might get some sort of data errors regarding team wins vs. pitcher wins.