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:
Teams
: This table contains the team_id and the respective team_name.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_id | team_name |
---|---|
1 | Ajax |
4 | Dortmund |
6 | Arsenal |
Matches:
home_team_id | away_team_id | home_team_goals | away_team_goals |
---|---|---|---|
1 | 4 | 0 | 1 |
1 | 6 | 3 | 3 |
4 | 1 | 5 | 2 |
6 | 1 | 0 | 0 |
Result:
team_name | matches_played | points | goal_for | goal_against | goal_diff |
---|---|---|---|---|---|
Dortmund | 2 | 6 | 6 | 2 | 4 |
Arsenal | 2 | 2 | 3 | 3 | 0 |
Ajax | 4 | 2 | 5 | 9 | -4 |
Approach
To generate the required statistics table, we can use the following approach:
- Calculate matches played, goals scored, and goals conceded for each team in the home and away matches.
- Combine the home and away statistics for each team.
- Calculate total points and goal difference for each team.
- 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 EvaluatorWhat 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
LeetCode Patterns Your Personal Dijkstra's Algorithm to Landing Your Dream Job The goal of AlgoMonster is to help you get a job in the shortest amount of time possible in a data driven way We compiled datasets of tech interview problems and broke them down by patterns This way we
Recursion Recursion is one of the most important concepts in computer science Simply speaking recursion is the process of a function calling itself Using a real life analogy imagine a scenario where you invite your friends to lunch https algomonster s3 us east 2 amazonaws com recursion jpg You first
Runtime Overview When learning about algorithms and data structures you'll frequently encounter the term time complexity This concept is fundamental in computer science and offers insights into how long an algorithm takes to complete given a certain input size What is Time Complexity Time complexity represents the amount of time
Want a Structured Path to Master System Design Too? Don’t Miss This!