Problem Statement
A cricket tournament is played between multiple teams.
-
Winning team gets +1 point
-
Losing team gets –1 point
We need to calculate total points for each team and find the top-ranked team based on points.
CREATE TABLE Matches ( MatchID INT PRIMARY KEY, Team1 VARCHAR(50), Team2 VARCHAR(50), Winner VARCHAR(50) ); INSERT INTO Matches (MatchID, Team1, Team2, Winner) VALUES (1, 'India', 'Australia', 'India'), (2, 'India', 'England', 'England'), (3, 'Australia', 'England', 'Australia'), (4, 'India', 'Australia', 'Australia'), (5, 'England', 'India', 'India');
SQL Query to Calculate Team Points
Explanation:
-
We used a CTE to break each match into:
-
Winner row (+1)
-
Loser row (–1)
-
-
UNION ALL keeps all point records
-
SUM(Points) calculates total score per team
-
RANK() gives ranking based on total points
![]()

