Exercise 12:1194.Tournament Winners

1.Description

Table: Players

+-------------+-------+
| Column Name | Type  |
+-------------+-------+
| player_id   | int   |
| group_id    | int   |
+-------------+-------+
player_id is the primary key of this table.
Each row of this table indicates the group of each player

Table: Matches

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| match_id      | int     |
| first_player  | int     |
| second_player | int     | 
| first_score   | int     |
| second_score  | int     |
+---------------+---------+
- match_id is the primary key of this table.
- Each row is a record of a match, first_player and second_player 
contain the player_id of each match.
- first_score and second_score contain the number of points of the first_player 
and second_player respectively.
- You may assume that, in each match, players belongs to the same group.

The winner in each group is the player who scored the maximum total points within the group. In the case of a tie, the lowest player_id wins.

Write an SQL query to find the winner in each group.

The query result format is in the following example:

2.Create Table and insert into values

This Problem test how to use UNION and Window Function, pay attention to the type of UNION.

Last updated