Design a University Enrollment Schema, Warehouse, and SQL
SQL & DataModel answerLast updated
Model a university domain in 3NF, build a star schema with SCD Type 2 history, plan partitioning for 100M+ rows, and write two analytic queries.
University Enrollment: End-to-End Data Design
You are designing the data layer for a university registrar system. The domain has five entities: Students, Courses, Departments, Instructors, and Enrollments. Work through each part below and explain your reasoning as you go.
Part 1 — Transactional Schema (OLTP)
Design a normalized (3NF) relational schema for the entities above.
Your schema must enforce:
- A student may not register for two sections of the same course in the same term.
- A grade must be one of:
A,B,C,D,F,Pass,Fail,Incomplete(or NULL while in progress).
For each table, specify primary keys, foreign keys, and any other constraints. Then list the indexes you would create and justify each one.
Part 2 — Analytical Warehouse (OLAP)
Design a star schema for enrollment analytics. At minimum, define a FactEnrollment table with measures like credits_attempted, credits_earned, and grade_points, plus dimension tables for Student, Course, Department, Term, and Instructor.
Address:
- What grain did you choose and why?
- Why use surrogate keys instead of natural keys?
- Apply SCD Type 2 to
DimStudentto track major changes over time. Show the required columns and explain how you guarantee point-in-time correctness when joining.
Part 3 — Physical Design at Scale
The FactEnrollment table has grown past 100 million rows. Propose a partitioning and clustering strategy. Justify your choices by referencing typical query access patterns and maintenance operations.
Part 4 — Write the SQL
Using whichever schema (OLTP or warehouse) is more natural for each query:
Query A. Return the full name of every student who enrolled in both CS101 and MATH201 during the same term.
Query B. For the most recently completed term, compute the top 3 departments by credit-weighted average GPA. Exclude Pass, Fail, and Incomplete grades from the GPA calculation. Break ties deterministically.
Part 5 — Design Trade-offs
In two to four sentences each:
- How do the OLTP and OLAP designs differ in their priorities and trade-offs?
- When would you prefer a snowflake schema over a flat star schema in this domain?
Hints
Hint 1
The 'no two sections of the same course in one term' constraint cannot be enforced with a simple UNIQUE on (student_id, section_id). Think about which columns you need to carry onto the enrollments table and how a composite foreign key locks in their correctness.
Hint 2
GPA is not the average of per-row GPA values. Store grade_points and gpa_credits as additive measures so you can compute SUM(grade_points) / SUM(gpa_credits) at any aggregation level. Pass/Fail grades need a gpa_credits value of 0.
Hint 3
Partition FactEnrollment by term, not by student or course. Almost every analytic query filters by term, and term-level partitions make ETL idempotent: you can overwrite a whole partition instead of merging individual rows.
Model answer
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach and Assumptions
State these up front in an interview — they shape every downstream decision:
- A course has multiple sections per term; enrollment is at the section level.
- A department's GPA is based on the course's department, not the student's declared major.
Pass,Fail, andIncompletecontribute to credits attempted/earned but are excluded from GPA arithmetic.- SCD Type 2 history on
DimStudentis resolved at ETL load time, not at query time, to keep analytic joins simple.
Part 1 — OLTP Schema (3NF)
CREATE TABLE departments (
department_id INT PRIMARY KEY,
department_code VARCHAR(10) NOT NULL UNIQUE,
name VARCHAR(100) NOT NULL
);
CREATE TABLE instructors (
instructor_id INT PRIMARY KEY,
department_id INT NOT NULL REFERENCES departments(department_id),
name VARCHAR(100) NOT NULL
);
CREATE TABLE students (
student_id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
major_department_id INT REFERENCES departments(department_id),
admit_date DATE NOT NULL
);
CREATE TABLE courses (
course_id INT PRIMARY KEY,
course_code VARCHAR(20) NOT NULL UNIQUE, -- e.g. 'CS101'
department_id INT NOT NULL REFERENCES departments(department_id),
credits NUMERIC(3,1) NOT NULL CHECK (credits > 0)
);
CREATE TABLE terms (
term_id INT PRIMARY KEY,
term_code VARCHAR(10) NOT NULL UNIQUE, -- e.g. '2024FA'
start_date DATE NOT NULL,
end_date DATE NOT NULL,
CHECK (end_date > start_date)
);
CREATE TABLE sections (
section_id INT PRIMARY KEY,
course_id INT NOT NULL REFERENCES courses(course_id),
term_id INT NOT NULL REFERENCES terms(term_id),
instructor_id INT REFERENCES instructors(instructor_id),
section_no VARCHAR(5) NOT NULL,
UNIQUE (course_id, term_id, section_no),
UNIQUE (section_id, course_id, term_id) -- target for composite FK below
);
CREATE TABLE enrollments (
enrollment_id BIGINT PRIMARY KEY,
student_id INT NOT NULL REFERENCES students(student_id),
section_id INT NOT NULL,
course_id INT NOT NULL, -- denormalized intentionally
term_id INT NOT NULL, -- denormalized intentionally
grade VARCHAR(10)
CHECK (grade IN ('A','B','C','D','F','Pass','Fail','Incomplete')),
enrolled_at TIMESTAMP NOT NULL,
FOREIGN KEY (section_id, course_id, term_id)
REFERENCES sections(section_id, course_id, term_id),
UNIQUE (student_id, course_id, term_id) -- enforces the one-section rule
);
Why the constraint works. A naive UNIQUE(student_id, section_id) still allows registering in two different sections of the same course. The fix is to carry course_id and term_id onto enrollments and bind them to the section via a composite foreign key — so those columns can never silently disagree with the section they reference. UNIQUE(student_id, course_id, term_id) then enforces the business rule at the database level, which is race-free under concurrent registrations (unlike a trigger).
Indexes and justification:
| Index | Reason |
|---|---|
UNIQUE(student_id, course_id, term_id) | Enforces the constraint; student_id-leading column doubles as a transcript lookup. |
enrollments(section_id) | Class roster queries and FK validation on section deletes. |
enrollments(term_id, course_id) | Per-course, per-term reporting. |
sections(term_id, course_id) | Building the term schedule. |
instructors(department_id), courses(department_id) | FK joins and departmental rollups. |
Avoid over-indexing enrollments — it is write-heavy during registration windows.
Part 2 — Star Schema
Grain: one row per student per section enrollment. This is the most atomic event and supports any rollup.
Measures in FactEnrollment:
credits_attempted: always the course credits.credits_earned: 0 for F, Fail, Incomplete.gpa_credits: 0 for Pass, Fail, Incomplete; otherwise equalscredits_attempted.grade_points:gpa_credits × grade_value(A=4, B=3, C=2, D=1, F=0).
All four are fully additive. GPA at any grain = SUM(grade_points) / SUM(gpa_credits). Never store or average a per-row GPA value.
Why surrogate keys: they are warehouse-owned integers, insulate joins from source key changes, allow multiple historical versions of one natural key (required for SCD2), and support an "Unknown" member (-1) for late-arriving rows.
DimStudent with SCD Type 2:
student_sk BIGINT -- surrogate PK
student_id INT -- natural key (repeats across versions)
name VARCHAR(100)
major_dept_code VARCHAR(10)
major_dept_name VARCHAR(100)
class_year INT
effective_start DATE
effective_end DATE -- default '9999-12-31' for the current row
is_current BOOLEAN
On a major change the ETL closes the active row (effective_end = change_date - 1, is_current = false) and inserts a new row with a new student_sk.
Point-in-time join at load time:
SELECT ds.student_sk
FROM dim_student ds
WHERE ds.student_id = :src_student_id
AND :enrollment_date BETWEEN ds.effective_start AND ds.effective_end;
Stamping the fact with the correct student_sk at load time means analysts use a plain equi-join and always see the major as of that enrollment — no range joins at query time.
The department_sk on the fact table represents the course's department, so department rollups never need a two-hop join.
Part 3 — Partitioning at 100M+ Rows
Partition FactEnrollment by term (via term_sk or term start date):
- Nearly every analytic query filters by term ("current semester," "academic year 2024"). Partition pruning eliminates irrelevant data immediately.
- At 3–4 terms per year, a 100M-row table produces manageable partition sizes (roughly 1–3M rows each).
- ETL becomes idempotent: late grades and corrections only affect the last one or two partitions. You can overwrite a whole partition rather than running row-level merges across the full table.
- Old terms can be archived or tiered to cheaper storage by dropping or detaching partitions.
Clustering within each partition by department_sk, then course_sk: departmental GPA queries scan far fewer blocks because rows for the same department and course sit adjacent on disk.
If the platform supports it (BigQuery, Snowflake), co-locate FactEnrollment and DimTerm on term_sk to minimize shuffle in joins.
Part 4 — SQL
Query A — students in both CS101 and MATH201 in the same term:
SELECT s.name
FROM students s
WHERE s.student_id IN (
SELECT e1.student_id
FROM enrollments e1
JOIN courses c1 ON c1.course_id = e1.course_id AND c1.course_code = 'CS101'
JOIN enrollments e2
ON e2.student_id = e1.student_id
AND e2.term_id = e1.term_id
JOIN courses c2 ON c2.course_id = e2.course_id AND c2.course_code = 'MATH201'
);
Query B — top 3 departments by credit-weighted GPA in the most recent completed term:
WITH grade_map AS (
SELECT 'A' AS grade, 4.0 AS points UNION ALL
SELECT 'B', 3.0 UNION ALL
SELECT 'C', 2.0 UNION ALL
SELECT 'D', 1.0 UNION ALL
SELECT 'F', 0.0
),
latest_term AS (
SELECT term_id
FROM terms
WHERE end_date < CURRENT_DATE
ORDER BY end_date DESC
LIMIT 1
),
dept_gpa AS (
SELECT
d.name AS department_name,
SUM(c.credits * gm.points) / NULLIF(SUM(c.credits), 0) AS weighted_gpa
FROM enrollments e
JOIN latest_term lt ON lt.term_id = e.term_id
JOIN courses c ON c.course_id = e.course_id
JOIN departments d ON d.department_id = c.department_id
JOIN grade_map gm ON gm.grade = e.grade -- excludes Pass/Fail/Incomplete
GROUP BY d.department_id, d.name
)
SELECT department_name, ROUND(weighted_gpa, 4) AS weighted_gpa
FROM dept_gpa
ORDER BY weighted_gpa DESC, department_name ASC -- name breaks ties deterministically
LIMIT 3;
The JOIN to grade_map naturally excludes Pass, Fail, Incomplete, and NULL grades — they have no matching row and are filtered out. The NULLIF guard prevents division by zero if a department has only non-GPA grades. Ties are broken alphabetically by department name, which is deterministic and auditable.
Part 5 — Trade-offs
OLTP vs OLAP. The OLTP schema prioritizes write integrity and correctness: strict normalization eliminates redundancy, constraints enforce rules at the database level, and indexes support low-latency point lookups. The warehouse sacrifices some normalization for read speed — denormalized dimensions, additive pre-computed measures, and columnar storage make aggregations across millions of rows fast without joins through many tables.
Star vs snowflake. A flat star schema is faster to query because dimension lookups are a single join. A snowflake schema makes sense when dimension attributes are large and highly repeated (e.g., a department sub-table reused across both DimCourse and DimInstructor), storage cost is a real constraint, or the ETL tooling benefits from normalized dimension loads. In a university domain with modest cardinality, the star schema wins on query simplicity; snowflaking would only be worth it if dimension storage became measurably expensive or shared sub-dimensions created maintenance problems.