1841. League Statistics


Problem Explanation

In this problem, we are given information about football teams and their performance in various matches. We need to generate a statistics table containing information about the teams' performance, such as the number of matches played, points earned, goals scored, goals conceded, and goal difference.

We have two tables:

  1. Teams: This table contains the team_id and the respective team_name.
  2. Matches: This table contains information about the matches, such as the home_team_id, away_team_id, home_team_goals, and away_team_goals.

Our goal is to write an SQL query to generate a statistics table with team_name, matches_played, points, goal_for, goal_against, and goal_diff columns.

Let's walk through an example:

Example

Teams:

team_idteam_name
1Ajax
4Dortmund
6Arsenal

Matches:

home_team_idaway_team_idhome_team_goalsaway_team_goals
1401
1633
4152
6100

Result:

team_namematches_playedpointsgoal_forgoal_againstgoal_diff
Dortmund26624
Arsenal22330
Ajax4259-4

Approach

To generate the required statistics table, we can use the following approach:

  1. Calculate matches played, goals scored, and goals conceded for each team in the home and away matches.
  2. Combine the home and away statistics for each team.
  3. Calculate total points and goal difference for each team.
  4. Order the teams by total points, goal difference, and team_name.

Solution

SQL

WITH home_stats AS (
  SELECT
    home_team_id AS team_id,
    COUNT(*) AS matches_played,
    SUM(home_team_goals) AS goal_for,
    SUM(away_team_goals) AS goal_against,
    SUM(CASE
      WHEN home_team_goals > away_team_goals THEN 3
      WHEN home_team_goals = away_team_goals THEN 1
      ELSE 0
    END) AS points
  FROM
    Matches
  GROUP BY
    home_team_id
),
away_stats AS (
  SELECT
    away_team_id AS team_id,
    COUNT(*) AS matches_played,
    SUM(away_team_goals) AS goal_for,
    SUM(home_team_goals) AS goal_against,
    SUM(CASE
      WHEN away_team_goals > home_team_goals THEN 3
      WHEN away_team_goals = home_team_goals THEN 1
      ELSE 0
    END) AS points
  FROM
    Matches
  GROUP BY
    away_team_id
),
combined_stats AS (
  SELECT
    T.team_id,
    T.team_name,
    COALESCE(H.matches_played, 0) + COALESCE(A.matches_played, 0) AS matches_played,
    COALESCE(H.goal_for, 0) + COALESCE(A.goal_for, 0) AS goal_for,
    COALESCE(H.goal_against, 0) + COALESCE(A.goal_against, 0) AS goal_against,
    COALESCE(H.points, 0) + COALESCE(A.points, 0) AS points
  FROM
    Teams T
    LEFT JOIN home_stats H ON T.team_id = H.team_id
    LEFT JOIN away_stats A ON T.team_id = A.team_id
)
SELECT
  team_name,
  matches_played,
  points,
  goal_for,
  goal_against,
  goal_for - goal_against AS goal_diff
FROM
  combined_stats
ORDER BY
  points DESC,
  goal_diff DESC,
  team_name ASC;

Ready to land your dream job?

Unlock your dream job with a 2-minute evaluator for a personalized learning plan!

Start Evaluator
Discover Your Strengths and Weaknesses: Take Our 2-Minute Quiz to Tailor Your Study Plan:
Question 1 out of 10

What does the following code do?

1def f(arr1, arr2):
2  i, j = 0, 0
3  new_arr = []
4  while i < len(arr1) and j < len(arr2):
5      if arr1[i] < arr2[j]:
6          new_arr.append(arr1[i])
7          i += 1
8      else:
9          new_arr.append(arr2[j])
10          j += 1
11  new_arr.extend(arr1[i:])
12  new_arr.extend(arr2[j:])
13  return new_arr
14
1public static List<Integer> f(int[] arr1, int[] arr2) {
2  int i = 0, j = 0;
3  List<Integer> newArr = new ArrayList<>();
4
5  while (i < arr1.length && j < arr2.length) {
6      if (arr1[i] < arr2[j]) {
7          newArr.add(arr1[i]);
8          i++;
9      } else {
10          newArr.add(arr2[j]);
11          j++;
12      }
13  }
14
15  while (i < arr1.length) {
16      newArr.add(arr1[i]);
17      i++;
18  }
19
20  while (j < arr2.length) {
21      newArr.add(arr2[j]);
22      j++;
23  }
24
25  return newArr;
26}
27
1function f(arr1, arr2) {
2  let i = 0, j = 0;
3  let newArr = [];
4  
5  while (i < arr1.length && j < arr2.length) {
6      if (arr1[i] < arr2[j]) {
7          newArr.push(arr1[i]);
8          i++;
9      } else {
10          newArr.push(arr2[j]);
11          j++;
12      }
13  }
14  
15  while (i < arr1.length) {
16      newArr.push(arr1[i]);
17      i++;
18  }
19  
20  while (j < arr2.length) {
21      newArr.push(arr2[j]);
22      j++;
23  }
24  
25  return newArr;
26}
27

Recommended Readings

Want a Structured Path to Master System Design Too? Don’t Miss This!