Five-a-Side Teams

Friends play five-a-side football and want balanced teams.

players(id, name, technical, athletic, role)      -- role: ATTACKER, DEFENDER, GOALKEEPER
matches(id, played_on)
availability(match_id, player_id)
lineups(match_id, player_id, team)                -- team: 1 or 2

Write a procedure generate_teams(p_match) that splits the players available for a match into two teams:

  • at least 10 players must be available, otherwise raise an error with a clear message;
  • within each role, players are ordered by technical * 1.2 + athletic, best first, and assigned alternately to team 1 and team 2;
  • the team that picks first changes from one role to the next (if attackers start with team 1, defenders start with team 2, goalkeepers with team 1 again).

Run schema.sql (tables and example data), then solution.sql.

Example

Match 1 has 10 players available:

TEAM ROLE       NAME  SCORE
---- ---------- ----- -----
   1 ATTACKER   Ada    17.8
   1 ATTACKER   Dan      15
   1 DEFENDER   Fay    16.2
   1 DEFENDER   Gus     9.8
   1 GOALKEEPER Ivy    15.6
   2 ATTACKER   Bob    16.4
   2 ATTACKER   Cat    13.2
   2 DEFENDER   Eve    17.6
   2 DEFENDER   Hal    12.4
   2 GOALKEEPER Jon    14.2

Match 2 has 8:

ORA-20001: only 8 players available for match 2, at least 10 needed

Approach

This one really is sequential: each player's team depends on the previous assignment, so a cursor loop is the right tool.

  • One cursor over the available players, ordered by role and then score: the role changes mark where alternation restarts.
  • Two variables carry the state: the current role, and the team that picked first for it. On a role change, flip the first team (3 - team turns 1 into 2 and back); after each insert, flip the current team. The same code serves both teams.
  • The availability check runs before anything is written, and a user-defined exception turned into RAISE_APPLICATION_ERROR reports it.
  • Deleting the match's old lineup first makes the procedure safe to run twice.

Lesson: PL/SQL cursors, procedures and exceptions.

Solutions

solution.sql

SET SERVEROUTPUT ON FORMAT WRAPPED
SET FEEDBACK OFF
SET LINESIZE 120 PAGESIZE 100

CREATE OR REPLACE PROCEDURE generate_teams(p_match matches.id%TYPE) IS
    CURSOR c_players IS
        SELECT p.id, p.role
        FROM availability a JOIN players p ON p.id = a.player_id
        WHERE a.match_id = p_match
        ORDER BY p.role, p.technical * 1.2 + p.athletic DESC, p.id;
    v_available  NUMBER;
    v_role       players.role%TYPE;
    v_first_team NUMBER := 2;       -- flipped to 1 for the first role
    v_team       NUMBER;
    e_not_enough EXCEPTION;
BEGIN
    SELECT count(*) INTO v_available FROM availability WHERE match_id = p_match;
    IF v_available < 10 THEN
        RAISE e_not_enough;
    END IF;

    DELETE FROM lineups WHERE match_id = p_match;
    FOR p IN c_players LOOP
        IF v_role IS NULL OR p.role <> v_role THEN     -- a new role starts
            v_role := p.role;
            v_first_team := 3 - v_first_team;          -- the other team picks first
            v_team := v_first_team;
        END IF;
        INSERT INTO lineups VALUES (p_match, p.id, v_team);
        v_team := 3 - v_team;                          -- alternate
    END LOOP;
EXCEPTION
    WHEN e_not_enough THEN
        RAISE_APPLICATION_ERROR(-20001, 'only ' || v_available || ' players available for match ' || p_match
                                        || ', at least 10 needed');
END;
/

EXEC generate_teams(1)
COLUMN role FORMAT A10
COLUMN name FORMAT A5
SELECT l.team, p.role, p.name, p.technical * 1.2 + p.athletic AS score
FROM lineups l JOIN players p ON p.id = l.player_id
WHERE l.match_id = 1
ORDER BY l.team, p.role, score DESC;

EXEC generate_teams(2)

schema.sql

CREATE TABLE players (
    id        NUMBER PRIMARY KEY,
    name      VARCHAR2(40) NOT NULL,
    technical NUMBER NOT NULL,
    athletic  NUMBER NOT NULL,
    role      VARCHAR2(10) NOT NULL CHECK (role IN ('ATTACKER', 'DEFENDER', 'GOALKEEPER'))
);
CREATE TABLE matches (id NUMBER PRIMARY KEY, played_on DATE NOT NULL);
CREATE TABLE availability (
    match_id  NUMBER REFERENCES matches,
    player_id NUMBER REFERENCES players,
    PRIMARY KEY (match_id, player_id)
);
CREATE TABLE lineups (
    match_id  NUMBER REFERENCES matches,
    player_id NUMBER REFERENCES players,
    team      NUMBER NOT NULL CHECK (team IN (1, 2)),
    PRIMARY KEY (match_id, player_id)
);

-- score 17.8
INSERT INTO players VALUES (1,  'Ada', 9, 7, 'ATTACKER');
-- 16.4
INSERT INTO players VALUES (2,  'Bob', 7, 8, 'ATTACKER');
-- 13.2
INSERT INTO players VALUES (3,  'Cat', 6, 6, 'ATTACKER');
-- 15.0
INSERT INTO players VALUES (4,  'Dan', 5, 9, 'ATTACKER');
-- 17.6
INSERT INTO players VALUES (5,  'Eve', 8, 8, 'DEFENDER');
-- 16.2
INSERT INTO players VALUES (6,  'Fay', 6, 9, 'DEFENDER');
-- 9.8
INSERT INTO players VALUES (7,  'Gus', 4, 5, 'DEFENDER');
-- 12.4
INSERT INTO players VALUES (8,  'Hal', 7, 4, 'DEFENDER');
-- 15.6
INSERT INTO players VALUES (9,  'Ivy', 8, 6, 'GOALKEEPER');
-- 14.2
INSERT INTO players VALUES (10, 'Jon', 6, 7, 'GOALKEEPER');
-- not available
INSERT INTO players VALUES (11, 'Kim', 3, 3, 'ATTACKER');
INSERT INTO matches VALUES (1, DATE '2026-12-05');
INSERT INTO matches VALUES (2, DATE '2026-12-12');
INSERT INTO availability SELECT 1, id FROM players WHERE id <= 10;
-- only 8
INSERT INTO availability SELECT 2, id FROM players WHERE id <= 8;
COMMIT;