← Atlassian Interview Insights
The union step is where I almost tripped up.
First, normalize the games table by creating a union of two SELECT statements: one for each team's perspective (team as home, opponent as away) and one for the reverse. Then, use a window function like ROW_NUMBER() or RANK() partitioned by team and ordered by score DESC, date DESC, game_id ASC to assign ranks, and finally filter to the top 3 per team for the 2024 season.
Pro tip: When using window functions, be explicit about the tie-breaking order and consider whether RANK() or ROW_NUMBER() is more appropriate; ROW_NUMBER() ensures exactly 3 rows per team even with ties, while RANK() may return more than 3 if ties occur at the boundary.
Identify the columns in the games table (e.g., game_id, date, home_team, away_team, home_score, away_score) and clarify that each row represents one team's perspective. The goal is to transform it so each game appears twice, once for each team.
Write two SELECT statements: one selecting home_team as team, away_team as opponent, home_score as team_score, and another selecting away_team as team, home_team as opponent, away_score as team_score. Combine them with UNION ALL to preserve all rows.
Apply a WHERE clause to restrict the data to games played in the 2024 season, using the date column (e.g., EXTRACT(YEAR FROM date) = 2024 or date BETWEEN '2024-01-01' AND '2024-12-31').
Use ROW_NUMBER() OVER (PARTITION BY team ORDER BY team_score DESC, date DESC, game_id ASC) to assign a rank to each game per team. This ensures deterministic ordering and handles ties as specified.
Wrap the ranked query in a subquery or CTE, then filter WHERE rank <= 3. Finally, select team, opponent, team_score, and rank for the output.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.