Knowledge Guide
HomeDatabasesSQL Practice Problems

Team Scores in Football Tournament

Problem

Table: Teams

+---------------+----------+
| Column Name   | Type     |
+---------------+----------+
| team_id       | int      |
| team_name     | varchar  |
+---------------+----------+
team_id is the column with unique values of this table.
Each row of this table represents a single football team.

Table: Matches

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| match_id      | int     |
| host_team     | int     |
| guest_team    | int     | 
| host_goals    | int     |
| guest_goals   | int     |
+---------------+---------+
match_id is the column of unique values of this table.
Each row is a record of a finished match between two different teams. 
Teams host_team and guest_team are represented by their IDs in the Teams table (team_id), and they scored host_goals and guest_goals goals, respectively.

Problem Definition

You have to compute the scores of all teams after all matches. Points are awarded as follows:

Write a solution that selects the team_id, team_name and num_points of each team in the tournament after all described matches.

Return the result table ordered by num_points in decreasing order. In case of a tie, order the records by team_id in increasing order.

Example

Image
Image

Output

Image
Image

Try It Yourself

java
-- TODO: Write your user queries here

Solution

To compute the scores of all teams after all matches, we need to determine the number of points each team has earned based on their performance in each match. Points are awarded as follows:

The solution involves joining the Teams and Matches tables, calculating points per match for each team, aggregating these points, and finally ordering the results as specified.

Approach Overview

SQL Query

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 (team_id = host_team OR team_id = guest_team) AND host_goals = guest_goals THEN 1 ELSE 0 END) AS num_points FROM Teams T LEFT JOIN Matches M ON T.team_id = M.host_team OR T.team_id = M.guest_team GROUP BY team_id, team_name ORDER BY num_points DESC, team_id ASC;

Step-by-Step Approach

Step 1: Join Teams with Matches

Associate each team with all the matches they have participated in, whether as the host or guest team. This allows us to evaluate each team's performance in every match.

SQL Query:

SELECT T.team_id, T.team_name, M.host_team, M.guest_team, M.host_goals, M.guest_goals FROM Teams T LEFT JOIN Matches M ON T.team_id = M.host_team OR T.team_id = M.guest_team;

Explanation:

Output After Step 1:

+---------+---------------+-----------+------------+-------------+-------------+ | team_id | team_name | host_team | guest_team | host_goals | guest_goals | +---------+---------------+-----------+------------+-------------+-------------+ | 10 | DesignGuru FC | 10 | 20 | 3 | 0 | | 10 | DesignGuru FC | 30 | 10 | 2 | 2 | | 10 | DesignGuru FC | 10 | 50 | 5 | 1 | | 20 | NewYork FC | 20 | 30 | 1 | 0 | | 20 | NewYork FC | 20 | 30 | 1 | 0 | | 30 | Atlanta FC | 30 | 10 | 2 | 2 | | 30 | Atlanta FC | 20 | 30 | 1 | 0 | | 30 | Atlanta FC | 50 | 30 | 1 | 0 | | 40 | Chicago FC | NULL | NULL | NULL | NULL | | 50 | Toranto FC | 50 | 30 | 1 | 0 | | 50 | Toranto FC | 10 | 50 | 5 | 1 | +---------+---------------+-----------+------------+-------------+-------------+

Step 2: Calculate Points per Match

Assign points to each team based on the outcome of each match they participated in. The CASE statement evaluates whether the team won, drew, or lost the match and assigns points accordingly.

SQL Query:

SELECT T.team_id, T.team_name, CASE WHEN T.team_id = M.host_team AND M.host_goals > M.guest_goals THEN 3 WHEN T.team_id = M.guest_team AND M.guest_goals > M.host_goals THEN 3 WHEN (T.team_id = M.host_team OR T.team_id = M.guest_team) AND M.host_goals = M.guest_goals THEN 1 ELSE 0 END AS points FROM Teams T LEFT JOIN Matches M ON T.team_id = M.host_team OR T.team_id = M.guest_team;

Explanation:

Output After Step 2:

+---------+---------------+--------+ | team_id | team_name | points | +---------+---------------+--------+ | 10 | DesignGuru FC | 3 | | 10 | DesignGuru FC | 1 | | 10 | DesignGuru FC | 3 | | 20 | NewYork FC | 3 | | 20 | NewYork FC | 0 | | 30 | Atlanta FC | 0 | | 30 | Atlanta FC | 0 | | 30 | Atlanta FC | 1 | | 40 | Chicago FC | 0 | | 50 | Toranto FC | 3 | | 50 | Toranto FC | 0 | +---------+---------------+--------+

Step 3: Aggregate Points per Team

Sum the points each team has earned across all their matches to determine their total score in the tournament.

SQL Query:

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 (team_id = host_team OR team_id = guest_team) AND host_goals = guest_goals THEN 1 ELSE 0 END) AS num_points FROM Teams T LEFT JOIN Matches M ON T.team_id = M.host_team OR T.team_id = M.guest_team GROUP BY team_id, team_name;

Explanation:

Output After Step 3:

+---------+---------------+------------+ | team_id | team_name | num_points | +---------+---------------+------------+ | 10 | DesignGuru FC | 7 | | 20 | NewYork FC | 3 | | 50 | Toranto FC | 3 | | 30 | Atlanta FC | 1 | | 40 | Chicago FC | 0 | +---------+---------------+------------+

Step 4: Order the Results

Sort the teams based on their total points in descending order. In case of a tie in points, sort the teams by their team_id in ascending order to maintain consistency.

SQL Query:

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 (team_id = host_team OR team_id = guest_team) AND host_goals = guest_goals THEN 1 ELSE 0 END) AS num_points FROM Teams T LEFT JOIN Matches M ON T.team_id = M.host_team OR T.team_id = M.guest_team GROUP BY team_id, team_name ORDER BY num_points DESC, team_id ASC;

Explanation:

Final Output:

+---------+---------------+------------+ | team_id | team_name | num_points | +---------+---------------+------------+ | 10 | DesignGuru FC | 7 | | 20 | NewYork FC | 3 | | 50 | Toranto FC | 3 | | 30 | Atlanta FC | 1 | | 40 | Chicago FC | 0 | +---------+---------------+------------+
🤖 Don't fully get this? Learn it with Claude

Stuck on Team Scores in Football Tournament? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Team Scores in Football Tournament** (Databases) and want to truly understand it. Explain Team Scores in Football Tournament from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Team Scores in Football Tournament** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Team Scores in Football Tournament** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Team Scores in Football Tournament** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes