Election Results
An election assigns each district a number of seats, split between parties in proportion to their votes; each party's seats go to its candidates with the most preference votes.
parties(id, name, country)
districts(id, description, seats)
candidates(id, first_name, last_name, party, district, preferences)
votes(district, party, votes)
Write a procedure elected(p_district) that prints the elected candidates of a district. A party's seats are ROUND(party votes / total votes × district seats) (rounding problems can be ignored).
Run schema.sql (tables and example data), then solution.sql.
Example
District 1 has 5 seats and 5,000 votes: Green 2,400 (2.4 → 2 seats), Blue 2,000 (2 seats), Red 600 (0.6 → 1 seat).
District 1: 5 seats, 5000 votes
Green: 2 seats
Eve White (1200)
Dan Green (1000)
Blue: 2 seats
Ada Lovelace (900)
Cat Black (700)
Red: 1 seats
Gus Red (400)
Approach
Read the district's seats and total votes once. Then a cursor over the parties, and a parameterized cursor over each party's candidates ordered by preferences, stopped after the party's seats with EXIT WHEN c_candidates%ROWCOUNT > seats. The %ROWCOUNT attribute counts the rows fetched so far, so no separate counter is needed.
This is a top-N per group, which a single query can also do with ROW_NUMBER() OVER (PARTITION BY party ORDER BY preferences DESC) compared with the party's seats (window functions).
Rounding each party separately can assign more or fewer seats than the district has (here 2 + 2 + 1 = 5, but 3 parties at 1/3 of 5 seats each would get 2 + 2 + 2). Real systems use a largest-remainder or D'Hondt method.
Lesson: PL/SQL cursors.
Solutions
solution.sql
SET SERVEROUTPUT ON FORMAT WRAPPED
SET FEEDBACK OFF
SET LINESIZE 120 PAGESIZE 100
CREATE OR REPLACE PROCEDURE elected(p_district districts.id%TYPE) IS
CURSOR c_parties IS
SELECT p.id, p.name, v.votes
FROM votes v JOIN parties p ON p.id = v.party
WHERE v.district = p_district
ORDER BY v.votes DESC;
CURSOR c_candidates(p_party NUMBER) IS
SELECT first_name, last_name, preferences
FROM candidates
WHERE party = p_party AND district = p_district
ORDER BY preferences DESC;
v_seats NUMBER;
v_total_votes NUMBER;
v_party_seats NUMBER;
BEGIN
SELECT seats INTO v_seats FROM districts WHERE id = p_district;
SELECT sum(votes) INTO v_total_votes FROM votes WHERE district = p_district;
DBMS_OUTPUT.PUT_LINE('District ' || p_district || ': ' || v_seats || ' seats, ' || v_total_votes || ' votes');
FOR p IN c_parties LOOP
v_party_seats := ROUND(p.votes / v_total_votes * v_seats);
DBMS_OUTPUT.PUT_LINE(p.name || ': ' || v_party_seats || ' seats');
FOR c IN c_candidates(p.id) LOOP
EXIT WHEN c_candidates%ROWCOUNT > v_party_seats;
DBMS_OUTPUT.PUT_LINE(' ' || c.first_name || ' ' || c.last_name || ' (' || c.preferences || ')');
END LOOP;
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No district ' || p_district);
END;
/
EXEC elected(1)
schema.sql
CREATE TABLE parties (id NUMBER PRIMARY KEY, name VARCHAR2(40) NOT NULL, country VARCHAR2(20));
CREATE TABLE districts (id NUMBER PRIMARY KEY, description VARCHAR2(40), seats NUMBER NOT NULL);
CREATE TABLE candidates (
id NUMBER PRIMARY KEY,
first_name VARCHAR2(40),
last_name VARCHAR2(40),
party NUMBER REFERENCES parties,
district NUMBER REFERENCES districts,
preferences NUMBER NOT NULL
);
CREATE TABLE votes (
district NUMBER REFERENCES districts,
party NUMBER REFERENCES parties,
votes NUMBER NOT NULL,
PRIMARY KEY (district, party)
);
INSERT INTO districts VALUES (1, 'North-East', 5);
INSERT INTO districts VALUES (2, 'Islands', 3);
INSERT INTO parties VALUES (1, 'Blue', 'IT');
INSERT INTO parties VALUES (2, 'Green', 'IT');
INSERT INTO parties VALUES (3, 'Red', 'IT');
-- district 1: 5000 votes, 5 seats -> Blue 2000 = 2 seats, Green 2400 = 2.4 -> 2, Red 600 = 0.6 -> 1
INSERT INTO votes VALUES (1, 1, 2000);
INSERT INTO votes VALUES (1, 2, 2400);
INSERT INTO votes VALUES (1, 3, 600);
INSERT INTO votes VALUES (2, 1, 900);
INSERT INTO candidates VALUES (1, 'Ada', 'Lovelace', 1, 1, 900);
INSERT INTO candidates VALUES (2, 'Bob', 'Brown', 1, 1, 300);
INSERT INTO candidates VALUES (3, 'Cat', 'Black', 1, 1, 700);
INSERT INTO candidates VALUES (4, 'Dan', 'Green', 2, 1, 1000);
INSERT INTO candidates VALUES (5, 'Eve', 'White', 2, 1, 1200);
INSERT INTO candidates VALUES (6, 'Fay', 'Grey', 2, 1, 100);
INSERT INTO candidates VALUES (7, 'Gus', 'Red', 3, 1, 400);
INSERT INTO candidates VALUES (8, 'Hal', 'Rose', 1, 2, 500);
COMMIT;