> For the complete documentation index, see [llms.txt](https://jaywin.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jaywin.gitbook.io/leetcode/solutions/1212-team-scores-in-football-tournament.md).

# 1212. Team Scores in Football Tournament

<https://leetcode.com/problems/team-scores-in-football-tournament>

## Description

Table: `Teams`

```
+---------------+----------+
| Column Name   | Type     |
+---------------+----------+
| team\_id       | int      |
| team\_name     | varchar  |
+---------------+----------+
team\_id is the primary key 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 primary key 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.
```

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

* A team receives three points if they win a match (Score strictly more goals than the opponent team).
* A team receives one point if they draw a match (Same number of goals as the opponent team).
* A team receives no points if they lose a match (Score less goals than the opponent team).

Write an SQL query that selects the **team\_id**, **team\_name** and **num\_points** of each team in the tournament after all described matches. Result table should be ordered by **num\_points** (decreasing order). In case of a tie, order the records by **team\_id** (increasing order).

The query result format is in the following example:

```
Teams table:
+-----------+--------------+
| team\_id   | team\_name    |
+-----------+--------------+
| 10        | Leetcode FC  |
| 20        | NewYork FC   |
| 30        | Atlanta FC   |
| 40        | Chicago FC   |
| 50        | Toronto FC   |
+-----------+--------------+
Matches table:
+------------+--------------+---------------+-------------+--------------+
| match\_id   | host\_team    | guest\_team    | host\_goals  | guest\_goals  |
+------------+--------------+---------------+-------------+--------------+
| 1          | 10           | 20            | 3           | 0            |
| 2          | 30           | 10            | 2           | 2            |
| 3          | 10           | 50            | 5           | 1            |
| 4          | 20           | 30            | 1           | 0            |
| 5          | 50           | 30            | 1           | 0            |
+------------+--------------+---------------+-------------+--------------+
Result table:
+------------+--------------+---------------+
| team\_id    | team\_name    | num\_points    |
+------------+--------------+---------------+
| 10         | Leetcode FC  | 7             |
| 20         | NewYork FC   | 3             |
| 50         | Toronto FC   | 3             |
| 30         | Atlanta FC   | 1             |
| 40         | Chicago FC   | 0             |
+------------+--------------+---------------+
```

## ac

```java
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://jaywin.gitbook.io/leetcode/solutions/1212-team-scores-in-football-tournament.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
