Skip to main content
PreparationZone Logo
PreparationZone
Back to Fundamentals

Fundamentals · Medium · Free

Model one-to-many and many-to-many relationships in SQL.

FresherFundamentalsDatabase Fundamentals
Model answer

Relationships in SQL

For Model one-to-many and many-to-many relationships in SQL., interviewers listen for a crisp definition, how the pieces interact in a real system, and one trade-off or failure mode you have seen or would guard against.

sql
-- 1:N departments → employees
CREATE TABLE employees (
  id INT PRIMARY KEY,
  dept_id INT REFERENCES departments(id)
);
-- M:N students ↔ courses via enrollments
M:N always needs junction table with composite key.