…
…
Build the fixture list, the high-scoring weeks and the league table from faculty team results with UNION ALL and CTEs.
In the university's inter-faculty soccer league, 8 teams play a 14-week double round robin. 10 weeks are done; the matches of the last 4 weeks have not been played yet, so their scores are empty. The league committee wants you to prepare the table and a few statistics for the website with SQL.
Data: team (id, name) and game (id, week, home_id, away_id, home_goals, away_goals). Both teams of a game refer to the team table. Goals are NULL in games that have not been played. A win is worth 3 points, a draw 1, a loss 0. All values are fictional sample data.
Lessons used: CASE (8), grouping and HAVING (11–13), multiple joins and self joins (17), subqueries and NOT EXISTS (20–21), UNION ALL and CTEs (22–23).
The hardest part is the league table: every game concerns two teams. You will first turn the games into rows "from the team's point of view", then group them.
What you'll do
league.sqlDownload file
Reading file…
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
The game table has no team names, only team numbers. To show the names of the home and away teams, you need to join the team table twice, with two different aliases.
Show the games of week 1 with the home team's name (home_team), the away team's name (away_team), home_goals and away_goals. Sort by game id.
This step's code cell is for members.Sign up free
The committee wants to know which weeks the crowd enjoyed most. Among the games already played, you will calculate the total goals per week and keep the weeks with at least 10 goals. Goals of unplayed games are NULL; leave them out from the start.
For played games, calculate each week's total goals (goals) and the average goals per game (average, two decimals). Show only the weeks whose total is at least 10, in week order.
This step's code cell is for members.
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
To find the highest-scoring game, first find the highest total with a subquery, then list the games whose total equals it. This way, if there is a tie, all tied games show up.
Show week, home team (home_team), away team (away_team), home_goals and away_goals for the games whose total goals (home + away) equal the season's highest total. Sort by game id.
This step's code cell is for members.Sign up free
The league table is calculated per team, but every game row holds two teams. The solution is to turn each game into two rows: from the home team's point of view (scored = home_goals, conceded = away_goals) and from the away team's point of view (scored = away_goals, conceded = home_goals). You stack the two SELECTs with UNION ALL; UNION would remove repeated rows, so UNION ALL is needed here.
Combine the two parts in a CTE named team_games (columns: team_id, scored, conceded; played games only). Then show each team's name, the number of games it played (played) and the goals it scored (scored). The team with the most goals comes first; break ties by team name.
This step's code cell is for members.Sign up free
Now every team has one row per game. You will count wins, draws and losses with CASE and calculate the points.
Using the same team_games CTE, show for each team: name, played (p), wins (w), draws (d), losses (l), goals scored (gf), goals conceded (ga), goal difference (gd = gf − ga) and points (pts). Order by points, then goal difference, then goals scored (high to low), and finally team name.
This step's code cell is for members.Sign up free
The last statistic: teams that have not won a single away game yet. The question "there is no such game" is asked with NOT EXISTS: if the team has even one away win, it is eliminated.
Show the names of the teams that have no game they won away (away_goals > home_goals), sorted by team id.
This step's code cell is for members.Sign up free