{x}
blog image

Team Scores in Football Tournament

Solution Explanation: Team Scores in Football Tournament

This problem requires calculating the total points earned by each team in a football tournament based on the results of matches. The solution involves joining data from two tables (Teams and Matches), calculating points based on match outcomes, and finally sorting the results.

Approach:

The most efficient approach uses SQL's capabilities for data aggregation and conditional logic. The steps are:

  1. Join Tables: Perform a LEFT JOIN between the Teams and Matches tables. A LEFT JOIN ensures that all teams are included in the result, even if they didn't participate in any matches (in which case their points will be 0). The join condition should include both scenarios where a team is the host or the guest team: team_id = host_team OR team_id = guest_team.

  2. Calculate Points: Use a CASE expression within a SUM aggregate function to determine the points for each match based on the outcome:

    • If the team won (more goals than the opponent), add 3 points.
    • If the match was a draw (equal goals), add 1 point.
    • Otherwise, add 0 points (loss).
  3. Group and Sort: Group the results by team_id to sum the points for each team. Finally, sort the results first by num_points in descending order (teams with more points first) and then by team_id in ascending order (to handle ties in points).

Time Complexity Analysis:

The time complexity of this SQL query is dominated by the JOIN operation and the GROUP BY operation. In a well-optimized database system, these operations typically have a complexity that is close to linear or slightly better than linear in the number of rows (O(N log N) or even O(N) in some cases, depending on indexing and query optimization techniques), where N is the total number of rows in both Teams and Matches tables combined. The CASE expression and SUM operations have linear time complexity relative to the number of matches each team participated in. The final ORDER BY clause is typically O(N log N) for sorting.

Therefore, the overall time complexity is approximately O(N log N), dominated by the sorting step and potentially the join and group by operations in the worst case. However, with efficient indexing on team_id, host_team, and guest_team, the actual performance in practice could be closer to linear time.

Code Implementation (MySQL):

SELECT
    team_id,
    team_name,
    SUM(
        CASE
            WHEN team_id = host_team AND host_goals > guest_goals THEN 3
            WHEN team_id = guest_team AND guest_goals > host_goals THEN 3
            WHEN host_goals = guest_goals THEN 1
            ELSE 0
        END
    ) AS num_points
FROM
    Teams
LEFT JOIN Matches ON team_id = host_team OR team_id = guest_team
GROUP BY team_id, team_name
ORDER BY num_points DESC, team_id ASC;
 

This SQL query directly implements the approach described above. It's concise and efficient for solving the problem. Other SQL dialects (PostgreSQL, SQL Server, etc.) would have very similar implementations with minor syntax adjustments.