Skip to content
Advertisement

Query to schedule a match between each team

How to fetch Below Output Using SQL Query in Oracle

Scenario:

suppose that in a Table named Team contains 4 records, we need to schedule a match between each team vs opposite team:

 Team
    ----
    India
    Pakistan
    Srilanka
    Australia


    Output :
    ----
    India VS Pakistan
    India VS Srilanka
    India VS Australia
    Pakistan VS Srilanka
    Pakistan VS Australia
    Srilanka VS Australia

Advertisement

Answer

WITH teams
     AS (SELECT 'India' team
         FROM dual
         UNION ALL
         SELECT 'Pakistan' team
         FROM dual
         UNION ALL
         SELECT 'Srilanka' team
         FROM dual
         UNION ALL
         SELECT 'Australia' team
         FROM dual
         )
SELECT DISTINCT CASE
                  WHEN t1.team >= t2.team THEN t2.team
                  ELSE t1.team
                END  || ' VS '||
                CASE
                  WHEN t1.team >= t2.team THEN t1.team
                  ELSE t2.team
                END Matches
FROM teams t1
       cross join teams t2
WHERE t1.team != t2.team
ORDER BY Matches;

Output:

Australia VS India
Australia VS Pakistan
Australia VS Srilanka
India VS Pakistan
India VS Srilanka
Pakistan VS Srilanka

You may use a different combinations within the CASE block

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement