Grand Slam Titles - Problem

You are given information about tennis players and their Grand Slam tournament wins. The Players table contains player information, and the Championships table contains the winners of each Grand Slam tournament by year.

Table: Players

  • player_id (int): Primary key, unique identifier for each player
  • player_name (varchar): Name of the tennis player

Table: Championships

  • year (int): Primary key, tournament year
  • Wimbledon (int): Player ID who won Wimbledon
  • Fr_open (int): Player ID who won French Open
  • US_open (int): Player ID who won US Open
  • Au_open (int): Player ID who won Australian Open

Write a SQL query to report the number of Grand Slam tournaments won by each player. Do not include players who did not win any tournament. Return the result in any order.

Table Schema

Players
Column Name Type Description
player_id PK int Primary key, unique identifier for each player
player_name varchar Name of the tennis player
Primary Key: player_id
Championships
Column Name Type Description
year PK int Primary key, tournament year
Wimbledon int Player ID who won Wimbledon that year
Fr_open int Player ID who won French Open that year
US_open int Player ID who won US Open that year
Au_open int Player ID who won Australian Open that year
Primary Key: year

Input & Output

Example 1 — Multiple Tournament Winners
Input Tables:
Players
player_id player_name
1 Novak Djokovic
2 Rafael Nadal
3 Roger Federer
Championships
year Wimbledon Fr_open US_open Au_open
2018 1 2 1 3
2019 1 2 2 1
Output:
player_name grand_slams_count
Novak Djokovic 3
Rafael Nadal 3
Roger Federer 1
💡 Note:

Novak Djokovic (ID=1) won Wimbledon 2018, US Open 2018, and Australian Open 2019 = 3 titles. Rafael Nadal (ID=2) won French Open 2018, French Open 2019, and US Open 2019 = 3 titles. Roger Federer (ID=3) won Australian Open 2018 = 1 title.

Example 2 — Single Tournament Winner
Input Tables:
Players
player_id player_name
1 Serena Williams
2 Simona Halep
Championships
year Wimbledon Fr_open US_open Au_open
2020 2 2 2 2
Output:
player_name grand_slams_count
Simona Halep 4
💡 Note:

Simona Halep (ID=2) won all four Grand Slam tournaments in 2020, giving her 4 titles. Serena Williams (ID=1) did not win any tournaments, so she is excluded from the result.

Constraints

  • 1 ≤ player_id ≤ 1000
  • 1980 ≤ year ≤ 2030
  • player_name consists of English letters and spaces only
  • Each tournament winner player_id exists in the Players table

Visualization

Tap to expand
Grand Slam Titles: Column to Row TransformationPlayersidname1Novak2RafaelChampionshipsyearWimFrUSAu2018121220191221UNION ALL→ GROUP BYWins Per Playerplayer_nametitlesNovak3Rafael3SQL Process1. UNION ALL combines: Wimbledon, Fr_open, US_open, Au_open2. Creates one row per tournament win3. GROUP BY player_id and COUNT wins4. JOIN with Players table for names
Understanding the Visualization
1
Input Tables
Players and Championships with tournament columns
2
UNION ALL
Combine tournament columns into single column
3
GROUP BY
Count wins per player and join with names
Key Takeaway
🎯 Key Insight: Use UNION ALL to transform columns into rows when you need to count across multiple columns
Asked in
Amazon 12 Microsoft 8 Google 6
23.4K Views
Medium Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen