Ski School Assignment
A ski school runs course types, each for one skill level and an age range, and only if at least min_participants students sign up. Students ask for a week.
course_types(id, name, skill_level, min_age, max_age, min_participants)
students(id, name, age, skill_level, requested_week)
assignments(course_id, student_id)
Write a procedure assign_courses(p_week) that empties assignments and fills it for the given week: a student goes to the course type with their skill level whose age range contains their age. A course that would have fewer than min_participants students is not run, and none of its students is assigned.
Run schema.sql (tables and example data), then solution.sql.
Example
Week 1 has three young beginners (course 1 runs), one teenage beginner alone (course 2 does not run), two teenage advanced students (course 3 runs), and an adult with no matching course. Week 2 is ignored, and so is a leftover row from last week.
COURSE STUDENT
---------------- --------
Kids beginners Ada
Kids beginners Bob
Kids beginners Cat
Teens advanced Eve
Teens advanced Fay
Approach
The obvious version is two nested cursors: for each course, count its matching students, and if there are enough, loop over them and insert each one. It works, and costs a count query per course plus a row-by-row insert.
All of it is one INSERT ... SELECT: join students to courses on level and age, and keep only the courses whose own join has count(*) >= min_participants (a grouped subquery with HAVING). The database does the counting and filtering in one pass.
>= matters: a course with exactly the minimum runs. Writing > silently cancels it.
Lesson: a cursor loop vs one statement.
Solutions
solution.sql
SET SERVEROUTPUT ON FORMAT WRAPPED
SET FEEDBACK OFF
SET LINESIZE 120 PAGESIZE 100
CREATE OR REPLACE PROCEDURE assign_courses(p_week NUMBER) IS
BEGIN
DELETE FROM assignments;
INSERT INTO assignments (course_id, student_id)
SELECT c.id, s.id
FROM students s
JOIN course_types c
ON c.skill_level = s.skill_level AND s.age BETWEEN c.min_age AND c.max_age
WHERE s.requested_week = p_week
AND c.id IN (SELECT c2.id
FROM course_types c2
JOIN students s2
ON s2.skill_level = c2.skill_level AND s2.age BETWEEN c2.min_age AND c2.max_age
WHERE s2.requested_week = p_week
GROUP BY c2.id, c2.min_participants
HAVING count(*) >= c2.min_participants);
END;
/
EXEC assign_courses(1)
COLUMN course FORMAT A16
COLUMN student FORMAT A8
SELECT c.name AS course, s.name AS student
FROM assignments a JOIN course_types c ON c.id = a.course_id JOIN students s ON s.id = a.student_id
ORDER BY c.id, s.id;
schema.sql
CREATE TABLE course_types (
id NUMBER PRIMARY KEY,
name VARCHAR2(40) NOT NULL,
skill_level NUMBER NOT NULL,
min_age NUMBER NOT NULL,
max_age NUMBER NOT NULL,
min_participants NUMBER NOT NULL
);
CREATE TABLE students (
id NUMBER PRIMARY KEY,
name VARCHAR2(40) NOT NULL,
age NUMBER NOT NULL,
skill_level NUMBER NOT NULL,
requested_week NUMBER NOT NULL
);
CREATE TABLE assignments (
course_id NUMBER REFERENCES course_types,
student_id NUMBER REFERENCES students,
PRIMARY KEY (student_id) -- a student takes one course
);
INSERT INTO course_types VALUES (1, 'Kids beginners', 1, 6, 10, 3);
INSERT INTO course_types VALUES (2, 'Teens beginners', 1, 11, 16, 2);
INSERT INTO course_types VALUES (3, 'Teens advanced', 2, 11, 16, 2);
INSERT INTO students VALUES (1, 'Ada', 7, 1, 1);
INSERT INTO students VALUES (2, 'Bob', 9, 1, 1);
INSERT INTO students VALUES (3, 'Cat', 10, 1, 1);
-- alone in course 2: course not run
INSERT INTO students VALUES (4, 'Dan', 12, 1, 1);
INSERT INTO students VALUES (5, 'Eve', 13, 2, 1);
INSERT INTO students VALUES (6, 'Fay', 15, 2, 1);
-- another week
INSERT INTO students VALUES (7, 'Gus', 8, 1, 2);
-- no course for adults
INSERT INTO students VALUES (8, 'Hal', 30, 2, 1);
-- stale row from last week
INSERT INTO assignments VALUES (1, 7);
COMMIT;