Rank rows within each team
Use a CTE and a window function to return one latest ticket per team.
Return the latest ticket for each team
Use the four tickets from the setup exercise. North has two tickets on April 2. The reporting rule selects the larger ticket ID when dates tie. Keep each ticket intact while computing its position within the team.
WITH ranked AS (
SELECT id, team_id, opened,
ROW_NUMBER() OVER (
PARTITION BY team_id ORDER BY opened DESC, id DESC
) AS position
FROM ticket
)
SELECT team_id, id, opened
FROM ranked
WHERE position = 1
ORDER BY team_id;
The output contains (1, 13, 2026-04-02) and (2, 14, 2026-04-03). There is no row to rank for team 3. To include team 3, left join from team to the ranked rows, with the position condition in the join.
Choose how ties behave
To retain every ticket on a team’s newest date, use RANK() OVER (PARTITION BY team_id ORDER BY opened DESC) and keep rank one. North then contributes tickets 12 and 13. Keep a deterministic order in the outer query when displaying tied rows. The CTE names an intermediate query for this statement and makes its window result available to the outer WHERE.
Rank rows within each team quiz
Answer 3 questions. Check each answer for an explanation.