Swiss Re Data Engineer Interview SQL (Medium-Level) Problem — Solution
Not a member? You can access this article here.
This problem was asked of me in December 2023 when I attended a data engineer interview.
But later, I have seen the same kind of problem being asked in recent data engineer interviews in various companies like Tiger Analytics, Deloitte, etc. That was trending on some of the LinkedIn posts that I have recently seen.
I observed that many people are struggling to solve this problem in writing SQL queries. So, I thought it would be helpful if I could share a clear explanation to solve this problem.
Here are a few articles that will help you revise concepts that we use in solving this problem.
Problem Statement
Given a table with four columns named match_id, team1, team2, and match_result.
--table create statement
create table matches (id int primary key identity(1,1),
team1 varchar(20),
team2 varchar(20),
match_result varchar(20))
--data insert statement
insert into matches values (
'CSK', 'RCB', 'team2'), ('MI', 'SRH', 'team2'), ('KKR', 'CSK', 'team1'),
('RCB', 'LSG', 'team1'), ('GT', 'MI', 'draw'), ('SRH', 'LSG', 'team2'),
('LSG', 'GT', 'team1'), ('MI', 'SRH', 'draw'), ('PJSK', 'RCB', 'team2')
--table create statement
create table matches (id int primary key identity(1,1),
team1 varchar(20),
team2 varchar(20),
match_result varchar(20))
--data insert statement
insert into matches values (
'CSK', 'RCB', 'team2'), ('MI', 'SRH', 'team2'), ('KKR', 'CSK', 'team1'),
('RCB', 'LSG', 'team1'), ('GT', 'MI', 'draw'), ('SRH', 'LSG', 'team2'),
('LSG', 'GT', 'team1'), ('MI', 'SRH', 'draw'), ('PJSK', 'RCB', 'team2')
Question
Write a SQL query to return the total number of matches played by each team, the number of matches won, lost, and draws.
Solution:
- There are different columns mentioned in the given table.
Id → It’s a unique match ID.
Team1 -> Name of the team that plays in the first position.
Team2 -> Name of the team that plays in the second position.
Match_result -> it’s just given a team name that won the match, and also given as a draw if that was a draw.
- Now, we will create the result for each team with their positions individually, using conditional statements, and finally, we will combine them using the set operator, i.e., union all.
- Finally, we can count the total number of matches played by each team, the number of matches won, lost, and drawn by applying aggregate functions to the previous result.
Step 1: Find the team position-wise for each result, and then combine them using union all.
select team1 as team,
case when match_result = 'team1' then 1 else 0 end as won,
case when match_result = 'team2' then 1 else 0 end as loss,
case when match_result = 'draw' then 1 else 0 end as draws
from matches
union all
select team2 as team,
case when match_result = 'team1' then 1 else 0 end as won,
case when match_result = 'team2' then 1 else 0 end as loss,
case when match_result = 'draw' then 1 else 0 end as draws
from matches
select team1 as team,
case when match_result = 'team1' then 1 else 0 end as won,
case when match_result = 'team2' then 1 else 0 end as loss,
case when match_result = 'draw' then 1 else 0 end as draws
from matches
union all
select team2 as team,
case when match_result = 'team1' then 1 else 0 end as won,
case when match_result = 'team2' then 1 else 0 end as loss,
case when match_result = 'draw' then 1 else 0 end as draws
from matches
- We can use CTE to store all these result table queries.
Step 2: Now, we will apply all aggregate functions to find all. Finally, if we use group by, we will get our required result.
with cte as(
select team1 as team,
case when match_result = 'team1' then 1 else 0 end as won,
case when match_result = 'team2' then 1 else 0 end as loss,
case when match_result = 'draw' then 1 else 0 end as draws
from matches
union all
select team2 as team,
case when match_result = 'team1' then 1 else 0 end as won,
case when match_result = 'team2' then 1 else 0 end as loss,
case when match_result = 'draw' then 1 else 0 end as draws
from matches)
select team, count(*), sum(won), sum(loss), sum(draws) from cte
group by team
with cte as(
select team1 as team,
case when match_result = 'team1' then 1 else 0 end as won,
case when match_result = 'team2' then 1 else 0 end as loss,
case when match_result = 'draw' then 1 else 0 end as draws
from matches
union all
select team2 as team,
case when match_result = 'team1' then 1 else 0 end as won,
case when match_result = 'team2' then 1 else 0 end as loss,
case when match_result = 'draw' then 1 else 0 end as draws
from matches)
select team, count(*), sum(won), sum(loss), sum(draws) from cte
group by team
Here are a few articles that will help you solve a few different kinds of problems.
Resume Overloading Can Cost You the Job
Be Mindful in Adding the Right & Suitable Technologies in Your Resume
Netflix SQL Medium-Level Interview Question
A Detailed Solution with concept-wise and step-by-step
A Context-Based Medium-Level SQL Interview Question
Problem-Related To Employee Allotment To Projects In Any Organization


